I wrote this code for a robot I'm building for class. The idea is when you type in a 'W', the robot executes the winSequence function, when you type in a 'L', the robot executes the lossSequence function, and when you type in anything else, it executes a warning. However, no matter what I type in, it always executes the winSequence function. Any help is appreciated!
//TagBot - Win/Loss Sequence
int Buzzer = 13;
int blue1 = 4;
int blue2 = 5; //lights and buzzer pins
int green1 = 8;
int green2 = 9;
const int sadTone1 = 523;
const int sadTone2 = 493; //sad notes (in Hertz)
const int sadTone3 = 466;
const int happyTone1 = 880;
const int happyTone2 = 740; //happy notes (in Hz)
const int happyTone3 = 988;
int sadTime = 800; //each note is .8 seconds long
int happyTime = 500; //each not ie .5 seconds long
void setup() {
pinMode(Buzzer, OUTPUT);
pinMode(blue1, OUTPUT);
pinMode(blue2, OUTPUT); //designate as outputs
pinMode(green1, OUTPUT);
pinMode(green2, OUTPUT);
Serial.begin(9600);
}
void loop() {
char ch;
if(Serial.available())
{ //start reading serial monitor
ch = Serial.read();
if (ch = 'W')
{
winSequence(); //if input is a W, run win sequence
}
else if (ch = 'L')
{
lossSequence(); //if input is an L, run loss sequence
}
else if (ch != 'W' || ch != 'L')
{
digitalWrite(blue1, HIGH);
digitalWrite(green1, HIGH); //otherwise, run this warning
digitalWrite(Buzzer, HIGH);
tone(Buzzer, happyTone3);
}
}
}
void winSequence() {
digitalWrite(green1, HIGH);
digitalWrite(green2, LOW);
digitalWrite(Buzzer, HIGH); //note 1
tone(Buzzer, happyTone1);
delay(happyTime);
digitalWrite(Buzzer, LOW); // pause
noTone(Buzzer);
delay(50);
digitalWrite(green1, LOW);
digitalWrite(green2, HIGH); //note 2
digitalWrite(Buzzer, HIGH);
tone(Buzzer, happyTone1);
delay(happyTime);
digitalWrite(green1, HIGH);
digitalWrite(green2, LOW); //note 3
digitalWrite(Buzzer, HIGH);
tone(Buzzer, happyTone2);
delay(happyTime);
digitalWrite(green1, LOW);
digitalWrite(green2, HIGH);
digitalWrite(Buzzer, HIGH); //note 4
tone(Buzzer, happyTone3);
delay(happyTime);
digitalWrite(green1, HIGH);
digitalWrite(green2, LOW);
digitalWrite(Buzzer, HIGH); //note 5
tone(Buzzer, happyTone1);
delay(happyTime * 1.5);
digitalWrite(green1, LOW);
digitalWrite(green2, HIGH);
digitalWrite(Buzzer, HIGH); //note 6
tone(Buzzer, happyTone2);
delay(happyTime * 2);
digitalWrite(green1, LOW);
digitalWrite(green2, LOW);
digitalWrite(Buzzer, LOW); //off
noTone(Buzzer);
}
void lossSequence() {
digitalWrite(blue1, HIGH);
digitalWrite(blue2, LOW);
digitalWrite(Buzzer, HIGH); //note 1
tone(Buzzer, sadTone1);
delay(sadTime);
digitalWrite(blue1, HIGH);
digitalWrite(blue2, LOW);
digitalWrite(Buzzer, HIGH); //note 2
tone(Buzzer, sadTone2);
delay(sadTime);
digitalWrite(blue1, HIGH);
digitalWrite(blue2, LOW); //note 3
digitalWrite(Buzzer, HIGH);
tone(Buzzer, sadTone3);
delay(sadTime);
digitalWrite(blue1, LOW);
digitalWrite(blue2, LOW);
digitalWrite(Buzzer, LOW); //off
noTone(Buzzer);
}