Hello, everyone! I've been lurking the forums for a while, and decided to make an account so I could ask for some help.
For a project, I'm trying to make a program that plays one song when one button is pressed, another when the other button is pressed, and a third when both buttons are pressed. It's confusing reading it like that, but I hope someone understands.
Sometimes the project plays randomly. Sometimes it doesn't play. The third song only plays the first two notes. I have no idea what I'm doing wrong.
I've put my code in below. Sorry it's a huge wall of code. Thanks!
// Setup pin numbers and names, make things easy
const int buttonPin2 = 11;
const int buttonPin = 12;
int Buzzer1 = 13;
const int redLight1 = 1;
const int redLight2 = 2;
const int redLight3 = 3;
const int yellowLight1 = 4;
const int yellowLight2 = 5;
const int yellowLight3 = 6;
const int blueLight1 = 7;
const int blueLight2 = 8;
const int blueLight3 = 9;
const int greenLight1 = 10;
// variables will change:
int buttonState = 0; // Variable for reading the button status
void setup() {
// Buzzer is an output
pinMode(Buzzer1, OUTPUT);
// Buttons are inputs
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
// All the lights are outputs
pinMode(redLight1, OUTPUT);
pinMode(redLight2, OUTPUT);
pinMode(redLight3, OUTPUT);
pinMode(yellowLight1, OUTPUT);
pinMode(yellowLight2, OUTPUT);
pinMode(yellowLight3, OUTPUT);
pinMode(blueLight1, OUTPUT);
pinMode(blueLight2, OUTPUT);
pinMode(blueLight3, OUTPUT);
pinMode(greenLight1, OUTPUT);
}
void loop(){
// Read the button states
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
// Initialize the first song when only the first button is pressed.
if (buttonState == HIGH && buttonState2 == LOW)
{
// Play the music. One light plays per note, some notes play two notes.
// The song is a simple C scale up. Nothing fancy.
tone(Buzzer1,262, 200);
digitalWrite(redLight1, HIGH);
digitalWrite(redLight2, HIGH);
delay(500);
digitalWrite(redLight1, LOW);
digitalWrite(redLight2, LOW);
tone(Buzzer1,294, 200);
digitalWrite(redLight3, HIGH);
delay(500);
digitalWrite(redLight3, LOW);
tone(Buzzer1,330, 200);
digitalWrite(yellowLight1, HIGH);
digitalWrite(yellowLight2, HIGH);
delay(500);
digitalWrite(yellowLight1, LOW);
digitalWrite(yellowLight2, LOW);
tone(Buzzer1,349, 200);
digitalWrite(yellowLight3, HIGH);
delay(500);
digitalWrite(yellowLight3, LOW);
tone(Buzzer1,392, 200);
digitalWrite(blueLight1, HIGH);
delay(500);
digitalWrite(blueLight1, LOW);
tone(Buzzer1,440, 200);
digitalWrite(blueLight2, HIGH);
delay(500);
digitalWrite(blueLight2, LOW);
tone(Buzzer1,494, 200);
digitalWrite(blueLight3, HIGH);
delay(500);
digitalWrite(blueLight3, LOW);
digitalWrite(greenLight1, HIGH);
tone(Buzzer1,523, 200);
delay(500);
digitalWrite(greenLight1, LOW);
}
// Play the second song when the second button is pressed.
// Song is a C scale down. Opposite of the first song.
if (buttonState2 == HIGH && buttonState == LOW) {
// Play the music.
// Same as before. One light turns on for most notes, some notes have two lights.
tone(Buzzer1,523, 200);
digitalWrite(greenLight1, HIGH);
delay(500);
digitalWrite(greenLight1, LOW);
tone(Buzzer1,494, 200);
digitalWrite(blueLight3, HIGH);
digitalWrite(blueLight2, HIGH);
delay(500);
digitalWrite(blueLight3, LOW);
digitalWrite(blueLight2, LOW);
tone(Buzzer1,440, 200);
digitalWrite(blueLight1, HIGH);
delay(500);
digitalWrite(blueLight1, LOW);
tone(Buzzer1,392, 200);
digitalWrite(yellowLight3, HIGH);
delay(500);
digitalWrite(yellowLight3, LOW);
tone(Buzzer1,349, 200);
digitalWrite(yellowLight2, HIGH);
delay(500);
digitalWrite(yellowLight2, LOW);
tone(Buzzer1,330, 200);
digitalWrite(yellowLight1, HIGH);
delay(500);
digitalWrite(yellowLight1, LOW);
tone(Buzzer1,294, 200);
digitalWrite(redLight3, HIGH);
digitalWrite(redLight2, HIGH);
delay(500);
digitalWrite(redLight3, LOW);
digitalWrite(redLight2, LOW);
tone(Buzzer1,262, 200);
digitalWrite(redLight1, HIGH);
delay(500);
digitalWrite(redLight1, LOW);
}
// Establish that nothing happens when neither button is pressed. Green light is just a placeholder.
if (buttonState2 == LOW && buttonState == LOW) {
digitalWrite(greenLight1, LOW);
}
// Play the third song when both buttons are pressed.
// Song is supposed to be 'All Star' by Smash Mouth.
if (buttonState2 == HIGH && buttonState == HIGH) {
// This one has no lights, at least not yet. We'll get those in later.
tone(Buzzer1,294, 100);
tone(Buzzer1,294, 100);
tone(Buzzer1,330, 200);
tone(Buzzer1,294, 200);
tone(Buzzer1,392, 200);
tone(Buzzer1,349, 400);
delay(200);
tone(Buzzer1,294, 100);
tone(Buzzer1,294, 100);
tone(Buzzer1,330, 200);
tone(Buzzer1,294, 200);
tone(Buzzer1,440, 200);
tone(Buzzer1,392, 400);
delay(200);
}
// That's all, folks!
}