Hello,
I'm trying to win uncle of the year and make my nephew a gift for his 1st birthday, I'm having a couple issues with my code. Thanks in advance anyone who looks
Project:
As you can see in the picture below this contraption is 4 small arcade buttons, 1 big arcade button and 2 joysticks. In the window is a string of RGB LEDs and there are 2 more windows on the other side with the same thing. The joysticks aren't potentiometer based but rather 4 switches (up,down,left,right.) There is also a small speaker mounted inside.
This project should have 2 separate functions: 1) the joysticks positions should control LED's and mixed (say UP LEFT) should create a combination color aside from standard RGB 2)A mini game that does the following: phase0: blue and white switches lit, player pushes button 6 times phase1: blue and white switches shut off, green switch turns on, when green switch is pressed the pirate of the Caribbean theme plays from the speaker.
as far as coding goes in the loop I wanted the main joystick functionality to happen, and then the game portion to be interrupt based. I'm not sure if this is a good way of doing it or a silly way, but when I tried putting everything in the loop de-bouncing and other delays made the program pretty clunky.
One of my first issues is:the blue and white switches are tied together while the green switch is separate, in arduino how do you create an interrupt routine that can be triggered by 2 separate switches?
second issue: before I had this interrupt based everything worked except after the song played it would never reset the "game" mode.
third issue: right now I'm getting a compile issue for the "game" function in my attach interrupt statement. I get this
'game' was not declared in this scope
I've tried to comment things to let you know what I was doing. Thank you so much for the help! This is one of the more complicated arduino projects I've ever mustered even though when it boils down to it its just a bunch of switches. Thanks again
CODE:
int bwswitch = 8; //pin names: blue and white switch
int gswitch = 9; // green switch
int joystick1ud = 10; //joystick up down
int joystick1lr = 11; // joystick left right
int joystick2ud = 12; // joystick 2 up down
int joystick2lr = 13; // joystick 2 left right
int rled = 3; // window red led
int gled = 4; // window green led
int bled = 5; // window blue led
int spkr = 2; // speaker
int bwswled = 7 ; //blue and white switch LED
int gswled = 6; //green switch LED
int songplays=0; //Variables for game portion of code
int buttonState = 0; //reads blue and white switches for count
int count = 0; //variable that counts blue and white switch pushes from read buttonState
int ph1 = 0; //2 functions: when it = 0 is in blue and white button count mode when = 1 green is lit and is ready to play song
int ph2 = 0; //when this is enabled it resets all variables resetting the game
int songcue = 0; //reads green switch to start song
int reset=0; // When this variable is HIGH the game mode resets
/*all the song playing note information is here. omitted because I was over 9000 character post cap and this portion works.*/
void setup() {
pinMode(bwswitch , INPUT_PULLUP ); //pinout assignment
pinMode(gswitch ,INPUT_PULLUP );
pinMode(joystick1ud ,INPUT_PULLUP);
pinMode(joystick1lr ,INPUT_PULLUP );
pinMode(joystick2ud ,INPUT_PULLUP );
pinMode(joystick2lr ,INPUT_PULLUP );
pinMode(rled ,OUTPUT );
pinMode(gled ,OUTPUT );
pinMode(bled ,OUTPUT );
pinMode(spkr ,OUTPUT );
pinMode(bwswled ,OUTPUT );
pinMode(gswled ,OUTPUT );
attachInterrupt(bwswitch,game,RISING); //Interrupt assignment **********ERROR1: How to make interrupt fire on bwswitch pin OR gswpin and both go to 'game' function
}
void loop(){
if(digitalRead(joystick1ud) == HIGH){ //4 x IF statements and 1 x else statement controlling joystick to LED control. I tried doing this with a case statement initially but it did not work
digitalWrite(rled, HIGH);
}
if(digitalRead(joystick1lr) == HIGH){
digitalWrite(gled,HIGH);
}
if(digitalRead(joystick2ud) == HIGH){
digitalWrite(rled,HIGH);
}
if(digitalRead(joystick2lr) == HIGH){
digitalWrite(bled,HIGH);
digitalWrite(gled,HIGH);
}
else{
digitalWrite(rled, LOW);
digitalWrite(gled, LOW);
digitalWrite(bled, LOW);
}
if(ph1 == 0){ //if ph = 0 which is should int he beginning of the program turn on 4 outside buttons
digitalWrite(bwswled, HIGH);
digitalWrite(gswled,LOW);
}
if(ph1 == 0){ //if ph = 0 read outside 4 buttons w/debounce inside the interrupt these button pushes get incremented until count = 6
buttonState = digitalRead(bwswitch);// this turns off blue/white outside switches and turns on green switch (that plays song)
delay(200);
if(ph1 == 1){
songcue = digitalRead(gswitch);
delay(100);
}
if (ph2 >= 1 ){
ph1 == 0;
count == 0;
songcue == 0;
ph2 == 0;
buttonState = 0;
digitalWrite(gswled,LOW);
}
void game(){
if(ph1 == 1){
digitalWrite(rled,HIGH);
delay(500);
digitalWrite(bled,HIGH);
delay(500);
digitalWrite(gled,HIGH);
delay(500);
digitalWrite(rled,LOW);
delay(500);
digitalWrite(bled,LOW);
delay(500);
digitalWrite(gled,LOW);
}
if (ph1 == 0 && buttonState == HIGH) { //This if statement counts button pushes of outside 4 switches
digitalWrite(bwswled, LOW);
delay(50);
count++ ;
}
else{
digitalWrite(bwswled,HIGH);
}
if(count >= 6 && songcue >=0){ //if count is greater then or = to 6 pushes turn off outside switch LEDS and turn on GREEN Led
digitalWrite(gswled,HIGH);
digitalWrite(bwswled,LOW);
ph1++; //this variable changes to 1 which initiates play song portion of the game.
}
if(ph1 == 1 && songcue == HIGH){ //this function cues pirates of the carribean theme
songplays ++;
for (int i=0;i<203;i++){
int wait = duration[i] * songspeed;
tone(spkr,notes[i],wait);
delay(wait);
}
ph2++
}
if (ph2 >= 1 ){ //this resets all parameters which should reset "game" portion of the program back to counting button pushes
ph1 == 0;
count == 0;
songcue == 0;
ph2 == 0;
buttonState = 0;
digitalWrite(gswled,LOW);
}