2014-01-15 15:30:31 -07:00
|
|
|
/*
|
2014-01-15 17:25:48 -07:00
|
|
|
Simon Experiments #3
|
2014-01-15 15:30:31 -07:00
|
|
|
Buzzer
|
|
|
|
Pete Lewis
|
|
|
|
Sparkfun Electronics
|
|
|
|
10/13/2010
|
|
|
|
|
2014-01-15 17:25:48 -07:00
|
|
|
Updated by
|
|
|
|
Prof Mike Soltys
|
|
|
|
University of Colorado
|
|
|
|
01/15/2014
|
|
|
|
|
2014-01-15 15:30:31 -07:00
|
|
|
This example code is in the public domain.
|
|
|
|
|
2014-01-15 17:25:48 -07:00
|
|
|
//////////////////////////////////////////////////
|
|
|
|
DESCRIPTION
|
|
|
|
Pressing the buttion will flash the LED for 1 second and play a tone.
|
|
|
|
|
|
|
|
Note: these buttions are SUPER sensitive (freekishly so). The simon says code usese
|
|
|
|
a delay and check process called debouncing to fix this.
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2014-01-15 15:30:31 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ledPin = 3; // LEDs are on pins 3,5,10 and 13.
|
|
|
|
int buttonPin = 2; // BUTTONS are on pins 2,6,9 and 12.
|
|
|
|
int button_state; // This variable will be used to "store" the state of the button.
|
|
|
|
|
|
|
|
/// These next two definitions are setting up the buzzer pins.
|
|
|
|
/// By sending these HIGH/LOW we can create a sound from the buzzer.
|
|
|
|
int buzzer_1 = 4;
|
|
|
|
int buzzer_2 = 7;
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
pinMode(ledPin, OUTPUT);
|
|
|
|
|
|
|
|
digitalWrite(buttonPin, HIGH);
|
|
|
|
pinMode(buttonPin, INPUT);
|
|
|
|
|
|
|
|
pinMode(buzzer_1, OUTPUT);
|
|
|
|
pinMode(buzzer_2, OUTPUT);
|
|
|
|
digitalWrite(buzzer_1, LOW); // buzzer_1 will toggle HIGH/LOW to create the sound - see buzz() function below.
|
|
|
|
digitalWrite(buzzer_2, LOW); // buzzer_2 will toggle as well (to create more volume).
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
int button_state = digitalRead(buttonPin);
|
2014-01-15 17:25:48 -07:00
|
|
|
|
2014-01-15 15:30:31 -07:00
|
|
|
|
|
|
|
if(button_state == 1){
|
|
|
|
|
|
|
|
digitalWrite(ledPin, HIGH); // set the LED on
|
|
|
|
delay(1000); // wait for a second
|
|
|
|
digitalWrite(ledPin, LOW); // set the LED off
|
|
|
|
|
|
|
|
// Call the "buzz()" funtion. See below to know what this does.
|
|
|
|
buzz();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
void buzz(){
|
|
|
|
/// this function makes the buzzer pin move and crease a sound.
|
|
|
|
/// By writing the pin HIGH/LOW in a pattern we can create a frequency.
|
|
|
|
/// this FOR LOOP is used to repeat the pattern and let us hear the note for second.
|
|
|
|
for(int i = 0; i < 100; i++){
|
|
|
|
digitalWrite(buzzer_1, HIGH);
|
|
|
|
digitalWrite(buzzer_2, LOW);
|
|
|
|
delay(1);
|
|
|
|
digitalWrite(buzzer_1, LOW);
|
|
|
|
digitalWrite(buzzer_2, HIGH);
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|