Interrupts?

I'm trying to create a small game using the 16x2 LCD display.

I have created an interrupt routine in my code, so far I haven't had it respond to any input, and seemingly just randomly activates, even though there isn't any change in state whatsoever. Am I doing something wrong?

The rest of it works just fine, although unfinished. In my code i have marked interrupt related stuff with "//XXXXXXXXX". All i want it to do is to change the variable from a zero to a one!

Space_Game.ino (2.52 KB)

Why not just post the code so that everyone can see it?

//speceship game

int i=0;
int pos=0;
int posx=0;
int sound=0;
int posxb=1;
int posship=0;
volatile int fired=LOW;                                                     //XXXXXXXXX

#include <LiquidCrystal.h>
//pretty standard LCD 16x2 display connections....GOOGLE IT!
LiquidCrystal lcd(12, 11, 5, 4, 3, 7);

byte ship[8] = {
  B00000,
  B11000,
  B11110,
  B01001,
  B11110,
  B11000,
  B00000,
};

byte enemy[8] = {
  B00000,
  B10101,
  B01110,
  B01010,
  B01110,
  B10101,
  B00000,
};

byte bullet[8] = {
  B00000,
  B00000,
  B00000,
  B11111,
  B00000,
  B00000,
  B00000,
};

byte explode[8] = {
  B01010,
  B10101,
  B01010,
  B10101,
  B01010,
  B10101,
  B01010,
};

byte explode1[8] = {
  B10101,
  B01010,
  B10101,
  B01010,
  B10101,
  B01010,
  B10101,
};

byte blank[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};
int shoot=digitalRead(13);

void setup(){
  attachInterrupt(0,fire,FALLING);                                    //XXXXXXXXX
  pinMode(shoot,INPUT);
  lcd.createChar(1,ship);
  lcd.createChar(2,enemy);
  lcd.createChar(3,bullet);
  lcd.createChar(4,explode);
  lcd.createChar(5,explode1);
  lcd.createChar(6,blank);
  lcd.begin(16, 2);  
  lcd.write(byte(0));
  lcd.clear();
  lcd.setCursor(5,0);
  lcd.print("Fight!");
  digitalWrite(2,HIGH);
}

void loop(){
  pos=random(0,2);                 //enemy beginning position
  posx=16;
  lcd.setCursor(0,posship);
  lcd.write(1);
  delay(1000);
  lcd.setCursor(5,0);
  i=6;
  while(i>0){
    lcd.write(6);                  //clear "fight!"
    i=i-1;
  }

  i=16;
  while(i>0){                      //moving enemy...
    lcd.setCursor(posx,pos);
    lcd.write(2);
    delay(300);
    if(fired=1){
      lcd.setCursor(posxb,posship);
      lcd.write(3);
      delay(100);
      lcd.setCursor(posxb,posship);
      lcd.write(6);
      posxb=posxb+1;
    }
    lcd.setCursor(posx,pos);
    lcd.write(6);
    posx=posx-1;
    i=i-1;
  }
  fired=0;
  
  delay(500);                     //failure conditions
  lcd.clear();
  delay(100);  
  lcd.setCursor(4,0);
  lcd.print("Mission");
  lcd.setCursor(5,1);
  delay(100);
  lcd.print("Failed");
  delay(1000);
  lcd.clear();
  lcd.setCursor(6,0);
  lcd.print("New");                //prompt newgame...
  delay(100);
  lcd.setCursor(6,1);
  lcd.print("Game?");
  delay(100);
  while(digitalRead(13)==HIGH){     //wait for newgame response...
    delay(1);
  }
  lcd.clear();
}
void fire(){                                                               //XXXXXXXXX
  fired=1;                          //bullet has been fired
}

Do you have a pull down resistor on the shoot switch?

//if(fired=1)
if(fired == 1)

As well as what @cattledog said

I wonder if fired = 0 is in the correct place in this code?
Describe the circumstances in which it should be reset back to 0.

i=16;
  while(i>0){                      //moving enemy...
    lcd.setCursor(posx,pos);
    lcd.write(2);
    delay(300);
    if(fired=1){
      lcd.setCursor(posxb,posship);
      lcd.write(3);
      delay(100);
      lcd.setCursor(posxb,posship);
      lcd.write(6);
      posxb=posxb+1;
    }
    lcd.setCursor(posx,pos);
    lcd.write(6);
    posx=posx-1;
    i=i-1;
  }
  fired=0;

...R

groundfungus:
Do you have a pull down resistor on the shoot switch?

Yes, I do.

I have removed the interrupt just to see what will happen, and I have found that the variable "fired" is persistently 1. I can even make it low and it is still read as a 1. I have used the serial monitor to see this. I can even ground the pin and it still reads a 1.

What is connected to pin 2 (interrupt 0 pin)?

groundfungus:
What is connected to pin 2 (interrupt 0 pin)?

A push switch with a pull down resistor.

EvilDuck95:
Yes, I do.

I have removed the interrupt just to see what will happen, and I have found that the variable "fired" is persistently 1. I can even make it low and it is still read as a 1. I have used the serial monitor to see this. I can even ground the pin and it still reads a 1.

Stop setting it to 1 then:

    if(fired=1){

is an assignment, not a comparison.

More whitespace in your code would make it more readable (to yourself and us). If in doubt
use whitespaceotherwiseyouexpectpeopletohavetheenergytowadethroughyourcodetryingtomakesenseofit.

Please reply to PM.
Just a causal observation of some inconsistencies:

Pin 2 is set high
Pin 13 which is build- in LED , output, is read as input.
Pin 2 is pulled down and the interrupt is active FALLING

Robin2:
I wonder if fired = 0 is in the correct place in this code?

Just to be clear (referring to Reply #4)

I was wondering about the location of fired = 0 and NOT about if (fired=0)
the latter should, of course, be if (fired == 0)

...R

MarkT:
Stop setting it to 1 then:

    if(fired=1){

is an assignment, not a comparison.

More whitespace in your code would make it more readable (to yourself and us). If in doubt
use whitespaceotherwiseyouexpectpeopletohavetheenergytowadethroughyourcodetryingtomakesenseofit.

Ah! I did not know that worked like that! I didn't know that you could alter a variable in that situation.

Thanks, I shall amend that!