Variable Doesnt Stay Set - Puts itself back starting state

Hi All,

i have used some of my own code plus some examples out there to achieve:

  1. Read room temp (working)
  2. Display it on an LCD display (working)
  3. Change and RGB LED based on temp (working)
  4. Be able to put the above 3 in a suspended state, i.e. the code for it isnt running when the boolean is set to false. (not working)

in the debug screen i can see it sets the boolean to 0 for false and 1 for true. the problem is it resets back to 1/true on each loop and didnt set it to do that.

the debug screen prints:
TEMPRATURE = 22.95*C
Status: 1

debug screen when button to turn on and off are pressed:
for on
FFE21D
CH+
1

for off
FFA25D
CH-
0

so from that i can see it changes the boolean variable but then immediately after it sets back to default

Here is the code:

int redPin = 6; // set red rgb pin
int greenPin = 5; // set green rgb pin
int bluePin = 3; //set blue rgb pin
int val; // value variable for reading temprature
int tempPin = 0; // set therm pin
int infrapin=11; // set infrared reciever pin
boolean vrbStatus = true; // variable for system on or off

#include <IRremote.h>
#include <IRremoteInt.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9,8, 10, 4, 12, 13); // set pins for LCD display

IRrecv irrecv(infrapin); 
decode_results results;



void setup() {

  pinMode(redPin, OUTPUT); // set red pin as output
  pinMode(greenPin, OUTPUT); // set green pin as output
  pinMode(bluePin, OUTPUT);  // set bluepin as output
  lcd.begin(16, 2); // declare LCD columns and rows
  lcd.print("Current Temp:"); // Statis message on top row of LCD
  Serial.begin(9600); // start serial for data
  irrecv.enableIRIn(); // Start the infrared receiver
  

}

void loop() {

  if (irrecv.decode(&results)) // have we received an IR signal?

  {
   Serial.println(results.value, HEX); 
    translateIR(); 
    irrecv.resume(); // receive the next value
  }  
  
  if (vrbStatus = false)
  {
    Serial.println("System Suspended");
    Serial.println(vrbStatus);
  
  }
  else if (vrbStatus = true)
  {
    switchedon();
  }
  
  
}


/*-----( Declare User-written Functions )-----*/
void translateIR() // takes action based on IR code received

// describing Car MP3 IR codes 

{

  switch(results.value)

  {

  case 0xFFA25D:  
    vrbStatus = false;
    Serial.println(" CH-            "); 
    Serial.println(vrbStatus);
    delay(1000);
    break;

  case 0xFF629D:  
    Serial.println(" CH             "); 
    break;

  case 0xFFE21D:  
    Serial.println(" CH+            "); 
    vrbStatus = true;
    Serial.println(vrbStatus);
    delay(1000);
    break;

  case 0xFF22DD:  
    Serial.println(" PREV           "); 
    break;

  case 0xFF02FD:  
    Serial.println(" NEXT           "); 
    break;

  case 0xFFC23D:  
    Serial.println(" PLAY/PAUSE     "); 
    break;

  case 0xFFE01F:  
    Serial.println(" VOL-           "); 
    break;

  case 0xFFA857:  
    Serial.println(" VOL+           "); 
    break;

  case 0xFF906F:  
    Serial.println(" EQ             "); 
    break;

  case 0xFF6897:  
    Serial.println(" 0              "); 
    break;

  case 0xFF9867:  
    Serial.println(" 100+           "); 
    break;

  case 0xFFB04F:  
    Serial.println(" 200+           "); 
    break;

  case 0xFF30CF:  
    Serial.println(" 1              "); 
    break;

  case 0xFF18E7:  
    Serial.println(" 2              "); 
    break;

  case 0xFF7A85:  
    Serial.println(" 3              "); 
    break;

  case 0xFF10EF:  
    Serial.println(" 4              "); 
    break;

  case 0xFF38C7:  
    Serial.println(" 5              "); 
    break;

  case 0xFF5AA5:  
    Serial.println(" 6              "); 
    break;

  case 0xFF42BD:  
    Serial.println(" 7              "); 
    break;

  case 0xFF4AB5:  
    Serial.println(" 8              "); 
    break;

  case 0xFF52AD:  
    Serial.println(" 9              "); 
    break;

  default: 
    Serial.println(" other button   ");

  }

  delay(500);


}

// code for proper RGB control using variables. (need to look into fade)
void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

void switchedon()
{
//Read Temp From Sensor:
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
//float mv = ( 5*val*100/1024 );
float cel = mv/10;
float farh = (cel*9)/5 + 32;

// Print Temp to Serial for Debugging
Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
Serial.print("Status: ");
Serial.print(vrbStatus);
Serial.println();
delay(100);

//Set and Print to LCD Display
lcd.setCursor(0, 1);
lcd.print(cel);
lcd.print(" Degrees");

if (cel < 20) // check if less than 20 degrees
{
//Set Blue
setColor(0, 0, 255);  // blue
}
else if (cel > 23) // check if over 23 degrees
{
//Set Red

setColor(255, 0, 0); // red


}

else
//Green for between 20 and 23 degrees (indicating optimal temp)

setColor(0, 255, 0);  // green
}

i would appreciate any help or guidance.

Many Thanks

Paul

Your ifs need == not =.

Maybe more, but I didn't look further.

edit: As shown here in the man pages.

yep i changed to "==" and it works perfectly.

Many thanks for the help, and so quickly! :slight_smile: