Variable in setup being run every loop

Hi Guys,

I'm trying to create an RFID scanner to start a motorbike using a latching relay. Essentially, tag in and it turns the ignition on, tag it again and it turns it off.

I have a variable to get the status of the bike called bikeOn which is set to either true or false So the code should check "if bikeOn=false trigger latch on else if bikeOn=true trigger latch off"

The script initially sets the bikeOn variable to false then when it triggers the relay it then sets bikeOn to true.

This part is working however the initial set of the variable defined in the setup is being run every loop so bikeOn is perpetually set to false.

You'll notice in the code I have a a few prints to give me the output and I was able to determine that changing int bikeOn = false; to true changed which part of the if statement was triggered but that it always went back to whatever the variable set to start off with

Here's the code:

/*

#include <EEPROM.h>     // We are going to read and write PICC's UIDs from/to EEPROM
#include <SPI.h>        // RC522 Module uses SPI protocol
#include <MFRC522.h>  // Library for Mifare RC522 Devices


#define LED_ON HIGH
#define LED_OFF LOW


#define redLed 7    
#define greenLed 6
#define blueLed 9

#define LatchOn 4     
#define LatchOff 10     
#define wipeB 3    

boolean match = false;          
boolean programMode = false;  
boolean replaceMaster = false;
int bikeOn = false;

uint8_t successRead;    

byte storedCard[4];   
byte readCard[4];   
byte masterCard[4];   


#define SS_PIN 53
#define RST_PIN 5
MFRC522 mfrc522(SS_PIN, RST_PIN);

///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
    //Arduino Pin Configuration
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(wipeB, INPUT_PULLUP);  
  pinMode(LatchOn, OUTPUT);
  pinMode(LatchOff, OUTPUT);
  //Be careful how LatchOn circuit behave on while resetting or power-cycling your Arduino
 // digitalWrite(LatchOff, HIGH);    
  delay(1000);                     
 // digitalWrite(LatchOff, LOW);     
  digitalWrite(redLed, LED_OFF);  
  digitalWrite(greenLed, LED_OFF);  
  digitalWrite(blueLed, LED_OFF); 

///////////////////////////////////////// Main Loop ///////////////////////////////////
void loop () {
  do {
    successRead = getID();  // sets successRead to 1 when we get read from reader otherwise 0
    // When device is in use if wipe button pressed for 10 seconds initialize Master Card wiping
    if (digitalRead(wipeB) == LOW) { // Check if button is pressed
      // Visualize normal operation is iterrupted by pressing wipe button Red is like more Warning to user
      digitalWrite(redLed, LED_ON);  // Make sure led is off
      digitalWrite(greenLed, LED_OFF);  // Make sure led is off
      digitalWrite(blueLed, LED_OFF); // Make sure led is off
      // Give some feedback
      Serial.println(F("Wipe Button Pressed"));
      Serial.println(F("Master Card will be Erased! in 10 seconds"));
      bool buttonState = monitorWipeButton(10000); // Give user enough time to cancel operation
      if (buttonState == true && digitalRead(wipeB) == LOW) {    // If button still be pressed, wipe EEPROM
        EEPROM.write(1, 0);                  // Reset Magic Number.
        Serial.println(F("Master Card Erased from device"));
        Serial.println(F("Please reset to re-program Master Card"));
        while (1);
      }
      Serial.println(F("Master Card Erase Cancelled"));
    }
    if (programMode) {
      cycleLeds();              // Program Mode cycles through Red Green Blue waiting to read a new card
    }
    else {
      normalModeOn();     // Normal mode, blue Power LED is on, all others are off
    }
  }
  while (!successRead);   //the program will not go further while you are not getting a successful read
  if (programMode) {
    if ( isMaster(readCard) ) { //When in program mode check First If master card scanned again to exit program mode
      Serial.println(F("Master Card Scanned"));
      Serial.println(F("Exiting Program Mode"));
      Serial.println(F("-----------------------------"));
      programMode = false;
      return;
    }
    else {
      if ( findID(readCard) ) { // If scanned card is known delete it
        Serial.println(F("I know this PICC, removing..."));
        deleteID(readCard);
        Serial.println("-----------------------------");
        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      }
      else {                    // If scanned card is not known add it
        Serial.println(F("I do not know this PICC, adding..."));
        writeID(readCard);
        Serial.println(F("-----------------------------"));
        Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      }
    }
  }
  else {
    if ( isMaster(readCard)) {    // If scanned card's ID matches Master Card's ID - enter program mode
      programMode = true;
      Serial.println(F("Hello Master - Entered Program Mode"));
      uint8_t count = EEPROM.read(0);   // Read the first Byte of EEPROM that
      Serial.print(F("I have "));     // stores the number of ID's in EEPROM
      Serial.print(count);
      Serial.print(F(" record(s) on EEPROM"));
      Serial.println("");
      Serial.println(F("Scan a PICC to ADD or REMOVE to EEPROM"));
      Serial.println(F("Scan Master Card again to Exit Program Mode"));
      Serial.println(F("-----------------------------"));
    }
     else {
      if ( findID(readCard) && (!bikeOn) ) { // If not, see if the card is in the EEPROM and bikeOn is False
        Serial.println(F("Turning Bike On"));
        Serial.println(bikeOn);
        grantedOn(300);         // Open the door lock for 300 ms
      }
      else if ( findID(readCard) && (bikeOn) ) { // If not, see if the card is in the EEPROM and bikeOn is True
        Serial.println(F("Turning Bike Off"));
        Serial.println(bikeOn);
        grantedOff(300);         // Open the door lock for 300 ms
      }
      else {      // If not, show that the ID was not valid
        Serial.println(F("Not authorized"));
        Serial.println(bikeOn);
        denied();
      }
    }
  }
}

/////////////////////////////////////////  Access Granted Turn On    ///////////////////////////////////
void grantedOn ( uint16_t setDelay) {
  digitalWrite(blueLed, LED_OFF);   // Turn off blue LED
  digitalWrite(redLed, LED_OFF);  // Turn off red LED
  digitalWrite(greenLed, LED_ON);   // Turn on green LED
  digitalWrite(LatchOn, HIGH);     // Unlock door!
  delay(3000);          // Hold Relay On for X seconds
  digitalWrite(LatchOn, LOW);    // Turn off relay
  delay(1000);            // Hold green LED on for a second
  int bikeOn = true;
  Serial.println(bikeOn);
}


/////////////////////////////////////////  Access Granted Turn Off   ///////////////////////////////////
void grantedOff ( uint16_t setDelay) {
  digitalWrite(blueLed, LED_OFF);   
  digitalWrite(redLed, LED_OFF);  
  digitalWrite(greenLed, LED_ON);   
  digitalWrite(LatchOff, HIGH);     
  delay(3000);          // Hold door lock open for given seconds
  digitalWrite(LatchOff, LOW);    
  delay(1000);              
  int bikeOn = false;
  Serial.println(bikeOn);
}


///////////////////////////////////////// Access Denied  ///////////////////////////////////
void denied() {
  digitalWrite(greenLed, LED_OFF);  
  digitalWrite(blueLed, LED_OFF);   
  digitalWrite(redLed, LED_ON);   
  delay(1000);
}

How can i get it to stop triggering my variable every time?

If I move it into void (setup) I get the telling me it's not defined:

C:\Users\Mitch\Documents\Arduino\RFID\RFID.ino\RFID_V3\RFID_V3.ino: In function 'void loop()':

RFID_V3:278: error: 'bikeOn' was not declared in this scope

if ( findID(readCard) && (!bikeOn) ) { // If not, see if the card is in the EEPROM and bikeOn is False

Help!

note: I removed a few sections to fit it here (Related to RFID cards)

int bikeOn = true;

declares a new int variable called bikeOn and sets it equal to true.

you now have two bikeOn variables... a global and a local...

BulldogLowell:

int bikeOn = true;

declares a new int variable called bikeOn and sets it equal to true.

you now have two bikeOn variables... a global and a local...

Thanks for the help. I'm not sure what you are saying.

are you saying to add another in? if so where?

or

Are you saying I already have 2 and I need to remove 1?

Remove 'int' from 'int bikeOn = ' for those lines found in loop()