Daniel darko
Daniel darko

Reputation: 47

Arduino can't get my boolean to work

I cant get my boolean to work I don't know what I'm doing wrong with it. Could anyone take a look at the code and give me a hint on what is wrong with it? I have tested different ways to write it but without success. The only time the boolean worked was when I put the code under void loop. But I can't use it there.

    #include <RemoteReceiver.h>
boolean statusLed1 = false; 
void setup() {
  Serial.begin(115200);

  // Initialize receiver on interrupt 0 (= digital pin 2), calls the callback "showCode"
  // after 3 identical codes have been received in a row. (thus, keep the button pressed
  // for a moment)
  //
  // See the interrupt-parameter of attachInterrupt for possible values (and pins)
  // to connect the receiver.
  RemoteReceiver::init(0, 3, showCode);
}

void loop() {
}

// Callback function is called only when a valid code is received.
void showCode(unsigned long receivedCode, unsigned int period) {
  // Note: interrupts are disabled. You can re-enable them if needed.

  // Print the received code.
  Serial.print("Code: ");
  Serial.print(receivedCode);
  Serial.print(", period duration: ");
  Serial.print(period);
  Serial.println("us.");

  if (receivedCode == 353805)
  {

    statusLed1 = true;
  }
  if (receivedCode == 352829)
  {
    statusLed1 = false;

  }
  if (statusLed1 = true) {
    Serial.print("on");
  } 
  if (statusLed1 = false){
    Serial.print("off");
  }
}

Upvotes: 0

Views: 300

Answers (2)

anilkunchalaece
anilkunchalaece

Reputation: 378

change

 if (statusLed1 = true) {
    Serial.print("on");
  } 
  if (statusLed1 = false){
    Serial.print("off");
  }
}

to

 if (statusLed1 == true) {
    Serial.print("on");
  } 
  if (statusLed1 == false){
    Serial.print("off");
  }
}

or

 if (statusLed1) {
    Serial.print("on");
  } 
  if (!statusLed1){
    Serial.print("off");
  }
}

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

if (statusLed1 = true) {

Oldest gotcha in the book. = is assignment, == is equality comparison.

Also, don't compare against a boolean like this regardless.

if (statusLed1) {

Upvotes: 2

Related Questions