Stop the light arcade game. Why is my ISR (interrupt Service Routine) not working?

I have written the following code in hopes of running an arcade style game.
The idea of the code is that 2 cases will run the first being the lights tracking back and forth before the button has been used to stop them. The second case happens when the button is pressed, and flashes on the light stopped on untill the button is pressed again.
Also, a potentiometer is employed to change the speed of the lights.
The 4th light is the only red one the one you are trying to "catch" if caught "Winner" is printed... if not "try again"

problems with code that i need help  on

1. the ISR isn't functioning
2. How can i have "winner" or "try again" print only once not with every blinking of the light
3. i think in case 2 its only using the case of light 4 even if it is not light 4... why is this?

THANKS!!

int Button=2; //button assigned to pin 2 just changed because of INT0 havent adjusted lights yet
volatile int Buttonstate= LOW;
int ledPins[] = {
2,3,4,5,6,7,8,9,10 }; // an array of pin numbers to which LEDs are attached
int pinCount = 9; // the number of pins (i.e. the length of the array)
int potpin=A1;
int potscale=8;
int oldpot=0;
int var=1;

void setup() {
  pinMode(Button, INPUT);
  pinMode(potpin,INPUT);
  attachInterrupt(0,button,RISING);//INT0
  Serial.begin(9600);
  int thisPin;
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);
    }
  }

void loop() {
  // reads pot for changing speed of the lights
  int potvalue = analogRead(potpin);
  int difficult= (1023- potvalue)/100;
  // print the difficulty if it changes
  if (difficult != difficult) {
  Serial.print(" difficulty = " );
  Serial.println(difficult);
  oldpot = potvalue;
  }
  switch (var)
    {
    case 1:
        //lights incrementing right to left
         for (int thisPin = 0; thisPin < pinCount; thisPin++) {
          digitalWrite(ledPins[thisPin], HIGH);
          delay(potvalue/2);
          digitalWrite(ledPins[thisPin], LOW);
         }
         //lights decrementing left to right
        for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
          digitalWrite(ledPins[thisPin], HIGH);
          delay(potvalue/2);
          digitalWrite(ledPins[thisPin], LOW);
        }
        break;
     case 2:
       //winner case if stopped on red light
        if (int thisPin=4){
          Serial.print("Winner!");
          digitalWrite(ledPins[thisPin], HIGH);
          delay(100);
          digitalWrite(ledPins[thisPin], LOW);
          delay(100);
        }
        //loser stopped on some other light
        else{
          Serial.print("try again");
          digitalWrite(ledPins[thisPin],HIGH);
          delay (100);
          digitalWrite(ledPins[thisPin],LOW);
          delay (100);
       }
        break;
  }
}
//ISR-Button has gone HIGH
void button()
{
//change buttonstate again
  Buttonstate=digitalRead(Button);
    if (var==1){var==2;}
    else if (var==2){var==1;}
  }

sort by: active | newest | oldest
vandygirl2013 (author) 5 years ago
Got it to work guys thanks so much!!!

any reason why it wont compile with the poteniometer code inside case 1 as shown below... its highlighting case 2: and saying "jump to case label"

Thanks so much for the help everyone!!


int ButtonPin=2; //button assigned to pin 2
volatile int Buttonstate=LOW;
int ledPins[] = {
3,4,5,6,7,8,9,10 }; // an array of pin numbers to which LEDs are attached
int pinCount = 8; // the number of pins (i.e. the length of the array)
int potpin=A1;
int potscale=8;
int oldpot=0;
int var=1;
int gPIN;


void setup() {
pinMode(ButtonPin, INPUT);
pinMode(potpin,INPUT);
attachInterrupt(0,button,RISING);//INT0
Serial.begin(9600);
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
switch (var)
{
case 1:
// reads pot for changing speed of the lights
int potvalue = analogRead(potpin);
int difficult= (1020- potvalue)/100;
// print the difficulty if it changes
if (difficult != difficult) {
Serial.print(" difficulty = " );
Serial.println(difficult);
oldpot = potvalue;
}
//lights incrementing right to left
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
Buttonstate=digitalRead(ButtonPin);
if(Buttonstate==HIGH){break;}
gPIN = thisPin;
digitalWrite(ledPins[thisPin], HIGH);
delay(potvalue/2);
digitalWrite(ledPins[thisPin], LOW);
}
//lights decrementing left to right
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
Buttonstate=digitalRead(ButtonPin);
if(Buttonstate==HIGH){break;}
gPIN = thisPin;
digitalWrite(ledPins[thisPin], HIGH);
delay(potvalue/2);
digitalWrite(ledPins[thisPin], LOW);
}
break;
case 2:
//winner case if stopped on red light
if (gPIN==3){
Serial.print("Winner! ");
digitalWrite(ledPins[gPIN], HIGH);
delay(100);
digitalWrite(ledPins[gPIN], LOW);
delay(100);
}
//loser stopped on some other light
else{
Serial.print("try again! landed on light: ");
Serial.println(gPIN+1);
digitalWrite(ledPins[gPIN],HIGH);
delay (100);
digitalWrite(ledPins[gPIN],LOW);
delay (100);
}
break;
}
}
//ISR-Button has gone HIGH
void button()
{
//change buttonstate again
if (var==1){var=2;}
else if (var==2){var=1;}
Buttonstate=LOW;
}
oldmicah5 years ago
Not familiar with the system you are using, but from a c language perspective, two things to check:

  if (difficult != difficult) {. //. Seems to be a no op

    if (var==1){var==2;}
    else if (var==2){var==1;}. // neither of these is changing a variable
//. I.e. == is a comparator. = is an assignment

Hope this helps
vandygirl2013 (author)  oldmicah5 years ago
Thanks! my ISR is working now, but a little glitchy probably meaning i need a new button...

another question if you have time...
Why is it always going to the thisPin=4 on case 2 even when not stopped on that pin?

your a life saver!!
Not sure what you intended in Case 2.
This is what you're doing:
...
    if (int thisPin=4){
          Serial.print("Winner!");
          digitalWrite(ledPins[thisPin], HIGH);
          delay(100);
          digitalWrite(ledPins[thisPin], LOW);
          delay(100);
...

The 'if (int thisPin=4){'
is defining a new variable called thisPin and is setting that variable equal to the value 4.  The IF is then really asking 'if (4) {'.  if (4) is true since the 'if' returns false if the inside part is 0, otherwise it is true.

I'm thinking you really want to not include the 'int' part within the FOR and IF blocks (so you don;t create a new variable each time) and you probably want a '==' like so:

int Button=2; //button assigned to pin 2 just changed because of INT0 havent adjusted lights yet
volatile int Buttonstate= LOW;
int ledPins[] = {
2,3,4,5,6,7,8,9,10 }; // an array of pin numbers to which LEDs are attached
int pinCount = 9; // the number of pins (i.e. the length of the array)
int potpin=A1;
int potscale=8;
int oldpot=0;
int var=1;
int thisPin;///////////////////////////////// I added this

void setup() {
  pinMode(Button, INPUT);
  pinMode(potpin,INPUT);
  attachInterrupt(0,button,RISING);//INT0
  Serial.begin(9600);
  thisPin = 0; ///////////////////////////  <<<< changed
  // the array elements are numbered from 0 to (pinCount - 1).
  // use a for loop to initialize each pin as an output:
  for (thisPin = 0; thisPin < pinCount; thisPin++) {  ///////////<<<<< changed
    pinMode(ledPins[thisPin], OUTPUT);
    }
  }

void loop() {
  // reads pot for changing speed of the lights
  int potvalue = analogRead(potpin);
  int difficult= (1023- potvalue)/100;
  // print the difficulty if it changes
  if (difficult != oldpot) {//////////////////////////<<<<< changed
  Serial.print(" difficulty = " );
  Serial.println(difficult);
  oldpot = difficult;//////////////////////////<<<< changed
  }
  switch (var)
    {
    case 1:
        //lights incrementing right to left
         for (thisPin = 0; thisPin < pinCount; thisPin++) {//////<<<< changed
          digitalWrite(ledPins[thisPin], HIGH);
          delay(potvalue/2);
          digitalWrite(ledPins[thisPin], LOW);
         }
         //lights decrementing left to right
        for (thisPin = pinCount - 1; thisPin >= 0; thisPin--) {////<<<changed
          digitalWrite(ledPins[thisPin], HIGH);
          delay(potvalue/2);
          digitalWrite(ledPins[thisPin], LOW);
        }
        break;
     case 2:
       //winner case if stopped on red light
        if (thisPin==4){                             ////////<<< changed
          Serial.print("Winner!");
          digitalWrite(ledPins[thisPin], HIGH);
          delay(100);
          digitalWrite(ledPins[thisPin], LOW);
          delay(100);
        }
        //loser stopped on some other light
        else{
          Serial.print("try again");
          digitalWrite(ledPins[thisPin],HIGH);
          delay (100);
          digitalWrite(ledPins[thisPin],LOW);
          delay (100);
       }
        break;
  }
}
//ISR-Button has gone HIGH
void button()
{
//change buttonstate again
  Buttonstate=digitalRead(Button);
    if (var==1){var=2;} ///////////////////////////////////// <<<< changed
    else if (var==2){var=1;}  ///////////////////////////////////// <<<< changed
  }
frollard5 years ago
http://arduino.cc/playground/Code/Interrupts

your code seems to make sense --confirm your button is connecting to VCC not ground, otherwise you won't get a rising signal.

I'd also recommend changing a few variable names to be more specific, 'buttonPin' compared to a variable named Button and a void button();

Lastly, you can make var a boolean;
in void button()
say
var = !var;

then adjust your other logic to use boolean instead of 1 or 2.