User:Laurier Rochon/prototyping/clapping music button

From XPUB & Lens-Based wiki

SPEAKER

  • One side to pin 13
  • One side to ground

BUTTON

  • One side to pin 8
  • One side to 10K resistor, then to ground
//pattern
String pat = "XXX XX X XX ";

//pin for button
const int buttonPin = 8;
//original button state
int buttonState = 0;

void setup() {
  //setup mode for button
  pinMode(buttonPin, INPUT);  
}

void loop() {   
  //get the button state
  buttonState = digitalRead(buttonPin);
  
    //if it's pushed
    if (buttonState == HIGH) {     
      //get the length of the pattern
      int patlen = pat.length();
      
      //loop
      for (int p=0; p<patlen; p++) {
        //if there's a X, play
        if(pat.charAt(p) == 'X'){
           tone(13, 800, 10);
        }
        delay(150);
      }
    
    }
}