Updated Examples 2 and 3, and added 4

2 and 3 are now working
4 plays mr roboto
This commit is contained in:
Mike S 2014-01-15 17:25:48 -07:00
parent eb44ce666a
commit 4cd74f9a23
4 changed files with 136 additions and 8 deletions

View File

@ -8,7 +8,8 @@ Sparkfun Electronics
Modified by
Prof Mike Soltys
01/15/2013
University of Colorado
01/15/2014
This example code is in the public domain.

View File

@ -6,6 +6,10 @@ Pete Lewis
Sparkfun Electronics
10/13/2010
Modified by
Prof Mike Soltys
University of Colorado
01/15/14
This example code is in the public domain.
//////////////////////////////////////////////////
@ -16,6 +20,13 @@ SETUP & UPLOAD INSTRUCTIONS
4. Click on the "upload button" - it looks like a box with an arrow to the right.
//////////////////////////////////////////////////
//////////////////////////////////////////////////
DESCRIPTION
Pressing the buttion will change the state of the LED from on to off or off to on.
Note: these buttions are SUPER sensitive (freakishly so). The simon says code usese
a delay and check process called debouncing to fix this.
//////////////////////////////////////////////////
*/
@ -33,7 +44,8 @@ int buttonPin = 2; // The simon board has 4 BUTTONS on it.
int button_state; // This variable will be used to "store" the state of the button.
// It will allow us to know whether the button is pressed or not.
int led_state = 0; // This variable will be used to "store" the state of the LED.
// It will allow us to know whether the LED is on or off.
// The setup() funtion runs once, when the sketch starts
void setup() {
@ -61,9 +73,17 @@ void loop()
// The second step in the loop is to actually do something with this variable.
// In this next "if statement" we are going to decide to do something. Here we are going to turn on the ledPin for a second.
if(button_state == 1){
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
// If the LED is off, turn it on
if (led_state == 0 ){
digitalWrite(ledPin, HIGH); // set the LED on
led_state = 1;
}
// If the LED is on, turn it off
else if (led_state == 1 ) {
digitalWrite(ledPin, LOW); // set the LED off
led_state = 0;
}
delay(1000); // wait for a second
}
}

View File

@ -1,12 +1,25 @@
/*
Simon Experiments #2
Simon Experiments #3
Buzzer
Pete Lewis
Sparkfun Electronics
10/13/2010
Updated by
Prof Mike Soltys
University of Colorado
01/15/2014
This example code is in the public domain.
//////////////////////////////////////////////////
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.
//////////////////////////////////////////////////
*/
@ -19,7 +32,6 @@ int button_state; // This variable will be used to "store" the state of the b
/// 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);
@ -37,6 +49,7 @@ void setup() {
void loop()
{
int button_state = digitalRead(buttonPin);
if(button_state == 1){
@ -51,7 +64,6 @@ void loop()
}
//////////////////////////////////////////////////////////////////////////////////////
void buzz(){
/// this function makes the buzzer pin move and crease a sound.

View File

@ -0,0 +1,95 @@
/*
Simon Experiments #4
Mr Roboto
Prof Mike Soltys
University of Colorado
01/15/2014
This example code is in the public domain.
//////////////////////////////////////////////////
DESCRIPTION
Pressing the buttion will play the song Mr Roboto
Note: these buttions are SUPER sensitive (freekishly so). The simon says code usese
a delay and check process called debouncing to fix this.
//////////////////////////////////////////////////
*/
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;
/* Here are some variables we'll use to make a little jingle.
First we'll define frequencies of a few notes to use in the jingle */
const int NOTE_F4 = 349; // F4 (F above middle c)
const int NOTE_DS4 = 311; // D-sharp 4
const int NOTE_REST = 0; // Rest, no tone
const int jingleLength = 12; // This is the length of the jingle - 12 notes
/* This array contains the note played, in order */
const int jingleNote[jingleLength] = {
NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_DS4, NOTE_REST,
NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_DS4};
/* jingleDuration contains the length of each note played
8 = 1/8 note, 4 = 1/4 note, 32 = 1/32 note, etc.*/
const int jingleDuration[jingleLength] = {
8, 8, 8, 8, 8, 4, 32,// do-mo-ar-i-ga-to-(rest)
4, 4, 8, 8, 4 }; // mis-ter-ro-bot-o
const int jingleBPM = 60; // Jingle beats-per-minute = 60 bpm
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);
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(){
for (int i=0; i<jingleLength; i++)
{ // Run through this for loop once for each note
/* First lets calculate how long to play the note
this value will be in miliseconds, so we divide 60000 miliseconds
by our beats per minute, and divide that by the note's duration */
int noteDuration = (60000/jingleBPM)/jingleDuration[i];
/* Now we'll play the tone using the tone(pin, frequency, duration)
function. The pin will always be nosePin, the note is defined above,
and we just calculated the duration in milliseconds */
tone(buzzer_1, jingleNote[i], noteDuration);
/* Now just a short delay, so you can distinguish between the notes.
This calculation is mostly arbitrary, but it sounds good. */
delay(noteDuration * 1.3);
}
}