IR remote while break

Hi!

I'm using single code and ! would like to use two buttons. 1 to start while loop and 2 to stop it. I tried some code but I can't figure out what I did wrong.

#include <IRremote.h>

#define key1 0xFFA25D
#define key2 0xFF629D

#define ledpin 5

#define RECV_PIN 7
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;

void setup() {

  Serial.begin(9600);
  irrecv.enableIRIn();

  pinMode(ledpin, OUTPUT);
}

void loop() {

  if (irrecv.decode(&results)) {

    if (results.value == 0XFFFFFFFF)
      results.value = key_value;

    switch (results.value) {
      case  key1:
        inf_loop1();
        break;

      case key2:
        inf_loop2();
        break;
    }
    key_value = results.value;
    irrecv.resume();
  }
}

void inf_loop1(void)
{
  while (results.value = key2) {

    for (int i = 0; i <= 255; i++)
    {
      analogWrite(ledpin, i);
      Serial.println(i);
    }
    for (int i = 255; i >= 0; i--)
    {
      analogWrite(ledpin, i);
      Serial.println(i);
    }
    break;
  }
}
void inf_loop2(void)
{
  digitalWrite(ledpin, 0);
  Serial.println(0);
}

Please clean up that mess by putting the code inside code tags. Ah, thanks, you beat me to it...

Just one thing... you didn't actually say what is wrong.

Since you never update results.value, but you have a break statement, this boils down to:

if (results.value = key2) {

    for (int i = 0; i <= 255; i++)
    {
      analogWrite(ledpin, i);
      Serial.println(i);
    }
    for (int i = 255; i >= 0; i--)
    {
      analogWrite(ledpin, i);
      Serial.println(i);
    }
  }

In other words, it doesn't function as a loop at all. Is it your intention?

You probably meant:
while (results.value == key2) {
You have to call irrecv.decode(&results) or results.value will never change.

This might work:

void loop() {

  if (irrecv.decode(&results))
  {
    if (results.value != 0XFFFFFFFF) // Repeat code
      key_value = results.value;  // New value
    irrecv.resume();
  }

// Repeat the function for key_value until a new key arrives
  switch (key_value) {
    case  key1:
      inf_loop1();
      break;

Then you don't need the infinite loop in each function.

While realizing there are other issues I would point out this is an assignment and not a comparison.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.