zondag 22 april 2012

Arduino - Experiment 4 - Melodie

Het vierde experiment heeft twee nieuwe elementen: een input (drukknop) en geluid!

Ik heb geprobeerd een melodietje te programmeren, helaas ben ik niet zo goed in muzieknoten lezen :-)

Voor degene die het herkent en muzikaal is; verbeteringen zijn welkom!

De code:

*
  Melody

 Plays a melody when you push a button

 circuit:
 * piezo speaker on digital pin 8
 * pus botton on digital pin 2


 */
 #include "pitches.h"


// notes in the melody:
int melodystart[] = { 
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_F4, NOTE_G4};


int melody[] = {
  NOTE_G4, NOTE_G4, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_F4, NOTE_B4, NOTE_G4, 
  NOTE_G4, NOTE_G4, NOTE_B4, NOTE_D5, NOTE_C5, NOTE_F5, NOTE_D5, NOTE_D5, NOTE_D5, NOTE_B4, NOTE_F5, 
  NOTE_A5, NOTE_F5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_G4, NOTE_F4, NOTE_E4, 
  NOTE_C6, NOTE_A5, NOTE_F5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_F4, NOTE_G4, 
  NOTE_D5, NOTE_D5, NOTE_C5, NOTE_F4, NOTE_G4};


// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurationsstart[] = {
  8, 8, 8, 4, 2 };
  
int noteDurations[] = {
  8, 8, 8, 8, 2, 2, 8, 8, 8, 8, 2,
  8, 8, 8, 8, 2, 2, 8, 8, 8, 8, 2,
  8, 8, 8, 8, 8, 8, 8, 2,
  8, 8, 8, 8, 8, 4, 8, 2,
  8, 8, 8, 4, 2 };


const int buttonPin = 2;
int buttonState = 0;


void setup() {
  pinMode(buttonPin, INPUT);
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 5; thisNote++) {


    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDurationstart = 1000/noteDurationsstart[thisNote];
    tone(8, melodystart[thisNote],noteDurationstart);


    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDurationstart * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }
}


void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);


  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    for (int thisNote = 0; thisNote < 43; thisNote++) {


    // to calculate the note duration, take one second 
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000/noteDurations[thisNote];
    tone(8, melody[thisNote],noteDuration);


    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(8);
  }  
  } 
  else {
    }
}


De input button is een schakelaar die met één pootje aan de + zit en het andere pootje aan de  input. Om te zorgen dat in de 'open' stand de input 'laag' is, is de input met een pull-down weerstand verbonden met de -.




Wanneer de Arduino opstart hoor je een kort melodietje, wanneer je op de knop drukt speelt het hele melodietje.


En dat klinkt dan als volgt: