User:Laurier Rochon/prototyping/clapping music button: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
This is going to need a bit more wiring with a button and resistor...
'''SPEAKER'''
 
*One side to pin 13
*One side to ground
 
'''Button'''
 
*One side to pin 8
*One side to 10K resistor, then to ground


<source lang="C">
<source lang="C">
Line 6: Line 14:


//pin for button
//pin for button
const int buttonPin = 9;
const int buttonPin = 8;
//original button state
//original button state
int buttonState = 0;
int buttonState = 0;
Line 28: Line 36:
         //if there's a X, play
         //if there's a X, play
         if(pat.charAt(p) == 'X'){
         if(pat.charAt(p) == 'X'){
           tone(8, 800, 10);
           tone(13, 800, 10);
         }
         }
         delay(150);
         delay(150);

Revision as of 15:55, 26 October 2010

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);
      }
    
    }
}