Difference between revisions of "0xchristmas"

From Randomdata wiki
Jump to: navigation, search
m (code)
m (code)
Line 114: Line 114:
  
 
== code ==
 
== code ==
the arduino code for this project:
+
the arduino code for Candle:
 
<pre>
 
<pre>
 
       /*
 
       /*
   Flame
+
   Candle
 
   Turns on an LED randomly with values from 50 - 255. This gives a flame like effect.
 
   Turns on an LED randomly with values from 50 - 255. This gives a flame like effect.
 
   
 
   
Line 145: Line 145:
 
     analogWrite(led1, random(205)+50); //120)+135
 
     analogWrite(led1, random(205)+50); //120)+135
 
     delay(random(100));  
 
     delay(random(100));  
 +
}
 +
</pre>
 +
 +
The arduino code for Jingle Bells:
 +
<pre>
 +
/**
 +
* Arduino playing 'Good Times' Bassline by Chic
 +
* Some code used and repurposed from:
 +
* http://rob.gubler.net/devel/ArduinoJingleBells/jinglebells.pde
 +
*
 +
* Hook up a Piezo Buzzer or small speaker's ground (-) to pin 13, and (+) to 5v
 +
*
 +
* Note frequencies courtesy of:
 +
* http://www.phy.mtu.edu/~suits/notefreqs.html
 +
*
 +
*/
 +
 +
#define FREQ_C2 65.41
 +
#define FREQ_CS2 69.30
 +
#define FREQ_D2 73.42
 +
#define FREQ_DS2 77.78
 +
#define FREQ_E2 82.41
 +
#define FREQ_F2 87.31
 +
#define FREQ_FS2 92.5
 +
#define FREQ_G2 98
 +
#define FREQ_GS2 103.83
 +
#define FREQ_A2 110.00
 +
#define FREQ_B2 123.47
 +
 +
#define FREQ_C3 130.81
 +
#define FREQ_CS3 138.59
 +
#define FREQ_D3 146.83
 +
#define FREQ_DS3 155.56
 +
#define FREQ_E3 164.81
 +
#define FREQ_F3 174.61
 +
#define FREQ_FS3 185
 +
#define FREQ_G3 196
 +
#define FREQ_GS3 207.65
 +
#define FREQ_A3 220.00
 +
#define FREQ_AS3 233.08
 +
#define FREQ_B3 246.94
 +
 +
#define FREQ_C4 261.63 //Middle C
 +
#define FREQ_CS4 277.18
 +
#define FREQ_DF4 277.18
 +
#define FREQ_D4 293.66
 +
#define FREQ_DS4 311.13
 +
#define FREQ_E4 329.63
 +
#define FREQ_F4 349.23
 +
#define FREQ_FS4 369.99
 +
#define FREQ_G4 392.00
 +
#define FREQ_GS4 415.30
 +
#define FREQ_A4 440.00
 +
#define FREQ_B4 493.88
 +
 +
#define FREQ_C5 523.25
 +
#define FREQ_D5 587.33
 +
 +
#define WHOLE_NOTE_TIME 2000
 +
 +
 +
#define OUTPIN 2
 +
 +
void setup() {
 +
}
 +
void loop() {
 +
/*  char music[] =
 +
    "ee eee eee q seeesfegeaebecedeEeae eae eae q saeasFsasGsasFeaeasbsesge e";
 +
  PlayMusicString(music,OUTPIN);
 +
*/ 
 +
    //Jingle Bells translated from sheet music at
 +
  char music[] =
 +
    "D4"            "D4b4a4g4"    "D2 4D4"    "D4b4a4g4"
 +
    "e2 4e4"        "e4c4b4a4"    "f2 4d4"    "d4d4c4a4"
 +
    "b2g4D4"      "D4b4a4g4"    "D2 4D4"    "D4b4a4g4"
 +
    "e2 4e4"        "e4c4b4a4"    "d4d4d4d4" "e4d4c4a4"
 +
    "g2g4 4"        "b4b4b2"      "b4b4b2"    "b4d4g4a8" "b2"
 +
    "c4c4c4c8"    "c4b4b4b8b8" "b4a4a4b4" "a2d2"
 +
    "b4b4b2"      "b4b4b2"      "b4d4g4a8" "b2 4"        "c4c4"
 +
    "c4b4b4b8b8" "d4d4c4a4"    "g2";
 +
 +
  PlayMusicString(music,OUTPIN);
 +
 +
 
 +
}
 +
 +
void PlayMusicString(char* music, int outputPin){
 +
  int noteCount=strlen(music);
 +
  float freq;
 +
  int duration;
 +
  for (int i=0; i < noteCount; i += 2) {
 +
    switch(music[i]){
 +
      case ' ' :
 +
        freq=0;
 +
        break;
 +
      case 'D' :
 +
        freq=FREQ_D2;
 +
        break;
 +
      case 'e' :
 +
        freq=FREQ_E2;
 +
        break;
 +
      case 'f' :
 +
        freq=FREQ_FS2;
 +
        break;
 +
      case 'g' :
 +
        freq=FREQ_G2;
 +
        break;
 +
      case 'a' :
 +
        freq=FREQ_A2;
 +
        break;
 +
      case 'b' :
 +
        freq=FREQ_B2;
 +
        break;
 +
      case 'c' :
 +
        freq=FREQ_CS3;
 +
        break;
 +
      case 'd' :
 +
        freq=FREQ_D3;
 +
        break;
 +
      case 'E' :
 +
        freq=FREQ_E3;
 +
        break;
 +
      case 'F' :
 +
        freq=FREQ_FS3;
 +
        break;
 +
      case 'G' :
 +
        freq=FREQ_G3;
 +
        break;
 +
       
 +
    }
 +
    //Note Timing
 +
    switch(music[i+1]){
 +
      case 'w' :
 +
        duration=WHOLE_NOTE_TIME;
 +
        break;
 +
      case 'h' :
 +
        duration=WHOLE_NOTE_TIME/2;
 +
        break;
 +
      case 'q' :
 +
        duration=WHOLE_NOTE_TIME/4;
 +
        break;
 +
      case 'e' :
 +
        duration=WHOLE_NOTE_TIME/8;
 +
        break;
 +
      case 's' :
 +
        duration=WHOLE_NOTE_TIME/16;
 +
        break; 
 +
    }
 +
    Sound(freq,duration,outputPin);
 +
    delay(WHOLE_NOTE_TIME/64);
 +
  }
 +
}
 +
 +
void Sound(float freq, int durationMS, int outputPin) {
 +
  int halfPeriod;
 +
  float period;
 +
  int durationCycles;
 +
  //Check for rest, 0 frequency is a rest for durationMS.
 +
  if(freq==0.0) {
 +
    //0 frequency so we stay quiet for duration
 +
    delay (durationMS);
 +
  }
 +
  else { //Frequency is not zero so we have work to do
 +
    // turn on output pin
 +
    pinMode(outputPin, OUTPUT);
 +
    //calculate the period or cycle time for the given frequency
 +
    period=1/freq; //Take the reciprocal to get time in seconds
 +
    period=period*1.0E6; //to covert seconds to uS.
 +
    //divide that by 2 to get the 1/2 cycle time. convert to int at the same time
 +
    halfPeriod = (int)(period/2.0) - 7; // subtract 7 us to make up for digitalWrite overhead
 +
 +
    // calculate cycles for duration.
 +
    durationCycles = (int)(((float)durationMS*1000.0)/period); // play note for duration ms
 +
    for (int i=0; i<durationCycles; i++){
 +
      digitalWrite(outputPin, HIGH);
 +
      delayMicroseconds(halfPeriod);
 +
      digitalWrite(outputPin, LOW);
 +
      delayMicroseconds(halfPeriod - 1); // - 1 to make up for fractional microsecond                                              in digitaWrite overhead
 +
    }
 +
    // shut off pin to avoid noise from other operations
 +
    pinMode(outputPin, INPUT);
 +
  }
 
}
 
}
 
</pre>
 
</pre>

Revision as of 10:58, 21 December 2013

Summary

A special edition christmas arduino workshop. We'll be using a new very small arduino clone called ["Trinket"].
We can make a christmas star/tree with sound effects.
Ardillo did some research and collected a list of parts which can be bought for a fair price.

Who

research: Ardillo
Workshop: Fish_ && Ardillo

Christmas kit

The Trinket is one of the cheapest clone (unless you tinker around yourself).
We've made a DIY christmas kit which consists of the following:

Parts 0xchristmas

amount what price url
1 trinket 5V Logic €8,00 [floris.cc-trinket]
1 breadboard €4,00 [floris.cc-breadboard]
1 USB - mini USB cable €2,08 [Farnell-usb cable]
1 Piezo buzzer €1,00 [Farnell-piezo buzzer]
1 Light Dependant Resistor €0,77 [Farnell-LDR]
2 high power LED's €0,60 [Farnell-LED's]
 ? red/green LED's €?? Randomdata's own supply
 ? jumpers/cables/wire €?? Randomdata's own supply
1 splitted shipping costs €0,56 Farnell & Floris.cc shipping divided by amount of kits
Total €17,01 aproximitly €20

The url's of Farnell are without tax, our price is with 21% tax.

trinket HowTo

During the workshop we tell about arduino's and why they are awesome.
The Trinket is a little different but still easy to work with. It is important to know that your trinket is a 5V Logic version (not the 3.3V). This important when connecting your trinket to sensors, they have to be of the same voltage as your trinket.
Adafruit made a good tutorial on how to use this trinket.

0xchristmas workshop

So for making a Christmas tree/star with sound effects we made a guidance workshop where you are doing all the work and we are just helping.
For connecting everything together we need some jumper wires, there are a couple of ready-to-use cables available or you can make your own from some scrap electronics, especially solid core network cable works fine.



...more to come, still work in progress...

Components explained

trinket

0xchristmas trinket2.jpg
source: [adafruit]
The Trinket is a smal microcomputer. It has a processor of the same family as the arduino The Trinket is designed by Adafruit. You can use a small computer like this for turn on LED's or make music with it. It's also possible to connect sensors to it, so you can for example monitor temperature.

breadbord

0xchristmas breadboard.jpg0xchristmas breadboard2.jpg
source: [floris.cc]
A breadboard is a nifty thing for experimenting with electronic circuits. You can easily make a circuit without soldering, as long as you don't tinker around with high frequency's (like wifi) a breadboard will succeed. The holes in a breadboard are arranged in such a way that they are connected in groups of five.
This way we can connect other parts to each other without having to solder, but just to put the contacts in a hole which is grouped to the part you want it connected to.

cable

0xchristmas cable.jpg
source: [farnell]
Just an ordinary cable nothing more to say. One side has a USB connection for your laptop and the other side has te be connected on your trinket.

Resistor

no image yet

Light Dependant Resistor

0xchristmas ldr.jpg
source: [farnell]
a Light Dependant Resistor is a special resistor. Its resistance is dependant on the amount of light that his sensor has. This component can be used in automatic lights, which turn on when it's getting dark.

Light Emitting Diode

0xchristmas led.jpg0xchristmas led2.jpg
source: [farnell]
Ligh Emitting Diodes or LED's as we call them are just lights. The more you have, the brighter and happier your project looks. A LED has a + and a - contact. So if you change the polarity the light will not work. So if it's not working, try to switch the legs. You can see the + and - if you look good at the picture. The + contact is always longer. and the - contact has a little flat side on the bottom of the LED bulb.

Piezo buzzer

0xchristmas piezo2.gif0xchristmas piezo.jpg
source: [farnell

Piezo is a strange material which has the ability to transform a bit in shape when a current is going through it. While doing this it makes a specific sound. Therefor Piezo material can be used as a small speaker, or the other way around. Because when touching it Piezo material generates a small current.



...more to come, still work in progress...

What should it look like

For the christmastree look here: Tinkercad - project: 0xchristmastree
Image of breadboard with LED,s and piezo connected.

code

the arduino code for Candle:

      /*
  Candle
  Turns on an LED randomly with values from 50 - 255. This gives a flame like effect.
 
  This example code is in the public domain.

  To upload to your Gemma or Trinket:
  1) Select the proper board from the Tools->Board Menu
  2) Select USBtinyISP from the Tools->Programmer
  3) Plug in the Gemma/Trinket, make sure you see the green LED lit
  4) For windows, install the USBtiny drivers
  5) Press the button on the Gemma/Trinket - verify you see
     the red LED pulse. This means it is ready to receive data
  6) Click the upload button above within 10 seconds
*/
 
int led1 = 0; // blink 'digital' pin 1 - AKA the built in red LED

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led1, OUTPUT);

}

// the loop routine runs over and over again forever:
void loop() {
    analogWrite(led1, random(205)+50); //120)+135
    delay(random(100)); 
}

The arduino code for Jingle Bells:

/**
 * Arduino playing 'Good Times' Bassline by Chic
 * Some code used and repurposed from:
 * http://rob.gubler.net/devel/ArduinoJingleBells/jinglebells.pde
 * 
 * Hook up a Piezo Buzzer or small speaker's ground (-) to pin 13, and (+) to 5v
 *
 * Note frequencies courtesy of:
 * http://www.phy.mtu.edu/~suits/notefreqs.html
 *
 */

#define FREQ_C2 65.41
#define FREQ_CS2 69.30 
#define FREQ_D2 73.42
#define FREQ_DS2 77.78
#define FREQ_E2 82.41
#define FREQ_F2 87.31
#define FREQ_FS2 92.5
#define FREQ_G2 98
#define FREQ_GS2 103.83
#define FREQ_A2 110.00
#define FREQ_B2 123.47

#define FREQ_C3 130.81 
#define FREQ_CS3 138.59 
#define FREQ_D3 146.83
#define FREQ_DS3 155.56
#define FREQ_E3 164.81
#define FREQ_F3 174.61
#define FREQ_FS3 185
#define FREQ_G3 196
#define FREQ_GS3 207.65
#define FREQ_A3 220.00
#define FREQ_AS3 233.08
#define FREQ_B3 246.94

#define FREQ_C4 261.63 //Middle C
#define FREQ_CS4 277.18
#define FREQ_DF4 277.18
#define FREQ_D4 293.66
#define FREQ_DS4 311.13
#define FREQ_E4 329.63
#define FREQ_F4 349.23
#define FREQ_FS4 369.99
#define FREQ_G4 392.00
#define FREQ_GS4 415.30
#define FREQ_A4 440.00
#define FREQ_B4 493.88

#define FREQ_C5 523.25
#define FREQ_D5 587.33

#define WHOLE_NOTE_TIME 2000


#define OUTPIN 2

void setup() {
}
void loop() {
/*  char music[] =
    "ee eee eee q seeesfegeaebecedeEeae eae eae q saeasFsasGsasFeaeasbsesge e";
  PlayMusicString(music,OUTPIN);
*/  
    //Jingle Bells translated from sheet music at
  char music[] =
    "D4"             "D4b4a4g4"    "D2 4D4"    "D4b4a4g4"
    "e2 4e4"        "e4c4b4a4"    "f2 4d4"     "d4d4c4a4"
    "b2g4D4"       "D4b4a4g4"    "D2 4D4"    "D4b4a4g4"
    "e2 4e4"        "e4c4b4a4"    "d4d4d4d4" "e4d4c4a4"
    "g2g4 4"        "b4b4b2"       "b4b4b2"    "b4d4g4a8" "b2"
    "c4c4c4c8"    "c4b4b4b8b8" "b4a4a4b4" "a2d2"
    "b4b4b2"       "b4b4b2"       "b4d4g4a8" "b2 4"        "c4c4"
    "c4b4b4b8b8" "d4d4c4a4"    "g2";

  PlayMusicString(music,OUTPIN);

  
}

void PlayMusicString(char* music, int outputPin){
  int noteCount=strlen(music);
  float freq;
  int duration;
  for (int i=0; i < noteCount; i += 2) {
    switch(music[i]){
      case ' ' :
        freq=0;
        break;
      case 'D' :
        freq=FREQ_D2;
        break;
      case 'e' :
        freq=FREQ_E2;
        break;
      case 'f' :
        freq=FREQ_FS2;
        break;
      case 'g' :
        freq=FREQ_G2;
        break;
      case 'a' :
        freq=FREQ_A2;
        break;
      case 'b' :
        freq=FREQ_B2;
        break;
      case 'c' :
        freq=FREQ_CS3;
        break;
      case 'd' :
        freq=FREQ_D3;
        break;
      case 'E' :
        freq=FREQ_E3;
        break;
      case 'F' :
        freq=FREQ_FS3;
        break;
      case 'G' :
        freq=FREQ_G3;
        break;
        
    }
    //Note Timing
    switch(music[i+1]){
      case 'w' :
        duration=WHOLE_NOTE_TIME;
        break;
      case 'h' :
        duration=WHOLE_NOTE_TIME/2;
        break;
      case 'q' :
        duration=WHOLE_NOTE_TIME/4;
        break;
      case 'e' :
        duration=WHOLE_NOTE_TIME/8;
        break;
      case 's' :
        duration=WHOLE_NOTE_TIME/16;
        break;  
    }
    Sound(freq,duration,outputPin);
    delay(WHOLE_NOTE_TIME/64);
  }
}

void Sound(float freq, int durationMS, int outputPin) {
  int halfPeriod;
  float period;
  int durationCycles;
  //Check for rest, 0 frequency is a rest for durationMS.
  if(freq==0.0) {
    //0 frequency so we stay quiet for duration
    delay (durationMS);
  }
  else { //Frequency is not zero so we have work to do
    // turn on output pin
    pinMode(outputPin, OUTPUT);
    //calculate the period or cycle time for the given frequency
    period=1/freq; //Take the reciprocal to get time in seconds
    period=period*1.0E6; //to covert seconds to uS.
    //divide that by 2 to get the 1/2 cycle time. convert to int at the same time
    halfPeriod = (int)(period/2.0) - 7; // subtract 7 us to make up for digitalWrite overhead

    // calculate cycles for duration.
    durationCycles = (int)(((float)durationMS*1000.0)/period); // play note for duration ms
    for (int i=0; i<durationCycles; i++){
      digitalWrite(outputPin, HIGH);
      delayMicroseconds(halfPeriod);
      digitalWrite(outputPin, LOW);
      delayMicroseconds(halfPeriod - 1); // - 1 to make up for fractional microsecond                                              in digitaWrite overhead
    }
    // shut off pin to avoid noise from other operations
    pinMode(outputPin, INPUT);
  }
}

Pictures