Jingle Bells – Arduino Sketch

Just making some tests before this blog going live

int speakerPin = 3;
int length = 26;
char notes[] = "eeeeeeegcde fffffeeeeddedg";
int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};

int tempo = 300;
void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(tone);
  }
}
void playNote(char note, int duration) {
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  
  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}
void setup() {
  pinMode(speakerPin, OUTPUT);
}
void loop() {
  for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo); // rest
    } else {
      playNote(notes[i], beats[i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2); 
  }
}

 

About Emmanuel

Hello, I am a passionate tech freak for more than 20 years now. I own a Master degree in History of Art of Nanterre X University but the world of technology attracts me more. I worked in various enterprises as IT business manager and in 2005 I created my own business with a small retail shop "repairmypc". As I live in Greece and the economy struggles all the professionals I decided to close my firm after 10 years. Now I work as IT manager again and in my free time I dig into the world of DIY. Arduino, Raspberry Pi, 3d printing and all this stuff occupies me for long long very long hours so the few minutes I have free I decided to spend them blogging yeahhh!!! So I hope I could give you some tips and mostly I am going to share with you my passion about everything.

View all posts by Emmanuel →

Leave a Reply