Help with project code

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);

  
}

Do a Tools > Auto Format on your sketch. Now look at your game function. Do you see how it's indented? The automatic indentation of Auto Format is a very useful tool for finding bugs in your code because it allows you to easily visualize how everything is nested. If your code was correctly written the line:

void game() {

should not be indented at all. The last brace in your code should also never be indented. These things show you are missing some braces. This sort of thing is why you should always auto format your code.

Please always remove unnecessary blank lines from your code before posting it on the forum. One or two to break the code into logical sections is fine but any more than that just makes us have to scroll more to read through it.

Orchid0:
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.)

In the text of your message you write about four switches with each joystick,
so two joysticks should provide eight switches in total?

But in your codes I see just a total of four switches representing two joysticks?!

Which is correct:

-either each joystick has four switches (up,down,left,right)?
or each joystick has two switches (ud,lr)?

BTW: Reading manual switches with the help of interrupts is nonsense.
Use polling to do a "state change detection" for all of the buttons used in your sketch!

If you need it, I could provide a "readButtons() function which could return a unique character for each button pressed. Maybe like that:

  • blue switch pressed, function returns 'b'
  • white switch pressed, function returns 'w'
  • greeen switch pressed, function returns 'g'
    1st joystick up returns 'u'
    1st joystick down returns 'd'
    1st joystick left returns 'l'
    1st joystick right returns 'r'

2nd joystick up returns 'U'
2snd joystick down returns 'D'
2nd joystick left returns 'L'
2nd joystick right returns 'R'

So that each button or switch returns an unique letter when pressed/switched.
The loop() function would simply read the returned character from the readButtons() function and do what has to be done.

What do you think?

Please tell me about all pin numbers to use with your buttons/switches and the assigned letter codes for each, then I can provide a buttonPressed() function for your sketch!

In case you are using 4 small buttons and one big arcade button and two joysticks with 4 switches each, and one pin for the speaker,this should be a total of 14 pins you will need. You could use the analog pins as digital pins as well.
If you want to use Serial for debug output using Serial.print() or Serial.println() during development, do not use the Serial RX/TX pins 0 or 1, maybe use pins 2,3,4,5,6,7,8,9,10,11,12,13,A0 and A1.

Wiring hardware: connect one side of each button or switch to GND, the other side to its pin number.

Thanks for the tip PERT that is a very helpful debug tool that I never thought of using.

JURS - The joysticks do have 4 switches but they are wired together in pairs. Hence joystick 1 UP/Down, Joystick 1 Left/Right, Joystick 2 Up/Down, Joystick 2 Left/Right. Same with the blue white switches are all together.

Blue white switches together
green switch is by itself
joystick up down together
joystick left right together

I paired them together exactly for the reasons you stated, too many pins for the UNO im using. Also the intention is blinking lights and sound. my Nephew is 1, the game doesn't need to be over complicated yet.

if you have the time, It could be really helpful to me. I'm not all too familiar with polling. But i am going to look it up.

Thanks for the help

If your program is organized properly there should be no need for using interrupts. Humans are very slow by Arduino standards. Just create a function to poll each of the switches on every iteration of loop() and save the values for use by other functions in your program.

Have a look at Several Things at a Time and the more extensive Planning and Implementing a Program

...R

Thanks robin!

Hey,
So I took all of your input and tried to make my code more logical.
-I scrapped the interrupts
-I took robins amazing posts tips and arranged the loop into function calls that break up what Im trying to do

  • I took Jurs tip of deleting spaces when posting for efficiency.
  • I took Perts tip of auto-formatting so I can see how everything is nested.

The code looks much sharper and more readable then it used too. Thanks for all the help. This is definitely going to make my coding procedure better in the future.

However, Somehow I still can't figure out why it won't reset the game function..... Ill post it one more time and maybe somebody can spot it?

int bwswitch = 8;  //pin names
int gswitch = 9;
int joystick1ud = 10;
int joystick1lr = 11;
int joystick2ud = 12;
int joystick2lr = 13;
int rled = 3;
int gled = 4;
int bled = 5;
int spkr = 2;
int bwswled = 7 ;
int gswled = 6;

int songplays = 0; //Variable decleration
int buttonState = 0;
int count = 0;
int ph1 = 0;
int ph2 = 0;
int songcue = 0;
int reset = 0;

/* All the music information is right here. Omitted because its irrelevant to my problem. The song plays
*/

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 );
}
void loop() {
  JOYSTICKS(); //Joystick Function to LEDS
  PH1();      //Beginning of game mode, push 6 buttons and green switch LED lights up
  SONGPLAY(); // When green switch is pushed song plays, variables incremented for reset
  RESET();  //resets variables which should restart "game"
}

void JOYSTICKS () {                            //This function takes Joystick positions and maps them to LED Outputs *This works*
  if (digitalRead(joystick1ud) == HIGH) {
    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);
  }
}

void PH1() {                   //Phase 1 reads the blue and white switches and counts them. Once 6 or more pushes have happened it increments Phase 1 variable for next function
  if (ph1 == 0) {              // This portion works
    digitalWrite(bwswled, HIGH);
  }
  if (ph1 == 0) {             //Maps button state to variable
    buttonState = digitalRead(bwswitch);
    delay(200);
  }
  if (ph1 == 0 && buttonState == HIGH) { //if button is pushed flickers switch LED's and increments count
    digitalWrite(bwswled, LOW);
    delay(50);
    count++ ;
  }
  else {                                //else the LED's stay high
    digitalWrite(bwswled, HIGH);
  }
  if (count >= 6 && songcue >= 0 && ph1 == 0) {     // Once 6 button pushes happen it increments phase 1 variable and resets count
    digitalWrite(gswled, HIGH);
    digitalWrite(bwswled, LOW);
    ph1++;
    count == 0;
  }
}

void SONGPLAY() {       //this is second phase of game mode. blue and white LEDS turn off green switch LED turns on when pushed song plays
  if (ph1 == 1) {       //maps switch state to variable
    songcue = digitalRead(gswitch);
  }
  if (ph1 == 1) {       //just some flashy stuff, After the song plays the code just does this portion, nothing else, button pushes do nothing
    digitalWrite(rled, HIGH);
    delay(200);
    digitalWrite(bled, HIGH);
    delay(200);
    digitalWrite(gled, HIGH);
    delay(200);
    digitalWrite(rled, LOW);
    delay(200);
    digitalWrite(bled, LOW);
    delay(200);
    digitalWrite(gled, LOW);
  }
  if (ph1 == 1 && songcue == HIGH) {  //if phase variable is 1 and green switch is high song plays
    songplays ++;
    ph2++;
    ph1 = 0;
    digitalWrite(bwswled, LOW);
    digitalWrite(gswled, LOW);
    for (int i = 0; i < 203; i++) {       // This for loop plays pirates of the carribean song
      int wait = duration[i] * songspeed;
      tone(spkr, notes[i], wait);
      delay(wait);
    }
    delay(50);
    reset++;      //Increment reset value
  }
}

void RESET() {
  if (reset == HIGH || ph2 == HIGH || songplays >= HIGH) { //reset, ph2 and songplays are all kind of redundant. Was just trying to force a reset
    ph1 == 0;
    count == 0;
    songcue == 0;
    ph2 == 0;
    buttonState = 0;
    reset == 0;
    digitalWrite(gswled, LOW);
  }
}

I am not even going to look, but suggest you debug the program using the Serial.write and Serial.print commands to track down you problem. When you put the Serial.write commands with appropriate text and the Serial.prints commands with values you are using, they will lead you to the errors.

I am building a program to control a drying oven with temperature and time control. Inputs are via a 16 key keyppad. I don't have the keypad, so I had to use input from the pc monitor. Don't have the temp sensor, yet.

This morning I had to add many Serial. commands. Perhaps 50. Eventually, I found all the current bugs and slowly began removing the Serial. commands. then I had to put some back. Now all are removed.

You can do the same and learn to debug your own project. One step at a time.

Paul

void loop() {
  JOYSTICKS(); //Joystick Function to LEDS

Good function names describe what the function does. We can't begin to guess what JOYSTICKS() does.

 if (digitalRead(joystick1ud) == HIGH) {

I've never seen a joystick that wasn't an analog device.

   count == 0;

If you care whether count is 0 or not, why do you not do anything if it is, or isn't?

   reset++;      //Increment reset value
  }
}

void RESET() {
  if (reset == HIGH ||

Just what IS the purpose of reset?

   ph1 == 0;
    count == 0;
    songcue == 0;
    ph2 == 0;
    buttonState = 0;
    reset == 0;

More useless comparisons.

-Thanks for the serial tip paul_kd7hb

PaulS:

  • Joystick just controls LEDS from a joystick. They are not potentiometer based but rather 4 switches, UP DOWN LEFT RIGHT. My thinking was if its 0 its low if its anything else it is high. If it was potentiometers then I would have to use the ADC analog read.

-The count is supposed to count 6 button pushes before changing the blue and white LEDS off and turning on the green one. When the green switch is lit and you push it the song plays.

-The purpose of the reset is to start the count of 6 buttons again before the song can be played. currently it stays where I marked it in the comment section which is basically just a bunch of LEDS flashing.

  • you are correct there is some redundancy in those. but I was trying to move different variables into different spots in my code to see if it would reset. Unfortunately it didn't work. I should have deleted those before posting. My apologies.

You completely missed the point that == is the comparison operator, not the assignment operator. If you want to assign a value to a variable, use =.

Yes! thanks paul. I tried this a bit earlier and it worked..
problem solved.

Thanks for all the help everybody, Appreciate it alot!