global variables and if (irrecv.decode(&results))

I am using global variables to hold the result of a comparison of an IR keypad button selection.

Within my get_key() function, the variables do not get changed according to the
if (irrecv.decode(&results))
Always true, set to 1, even when no key pressed.

Not sure why, if turning LED on and off WITHIN the function does work according to the same compare test.

[code]
#include <IRremote.h> // use the library for IR

const int receiver = 3; // pin 1 of IR receiver to Arduino digital pin 3
const int sounder =  2;
const int opto = 4;
const int mode = 5;

IRrecv irrecv(receiver); // create instance of 'irrecv'

decode_results results;
int array1[264];
int array2[264];
int key = 0;
int ok = 0;
int up = 0;
int down = 0;
//unsigned long last1 = millis();

void setup()

{  
  pinMode(sounder, OUTPUT);digitalWrite(sounder, LOW);
  pinMode(opto, INPUT_PULLUP);
  pinMode(mode, INPUT_PULLUP);
  irrecv.enableIRIn();
  Serial.begin(9600);
}
void beep()
{
  digitalWrite(sounder,HIGH);delay(100);digitalWrite(sounder,LOW);
}
  
void get_key()
{
  if (irrecv.decode(&results))
  {
   Serial.print (results.value);//debug
   Serial.print ("\n");
   if (results.value == 0xFF4AB5)key = 0;
   if (results.value == 0xFF6897)key = 1;
   if (results.value == 0xFF9867)key = 2;
   if (results.value == 0xFFB04F)key = 3;
   if (results.value == 0xFF02FD){ok = 1;} else {ok = 0;}
   if (results.value == 0xFF629D){up = 1;} else {up = 0;}
   if (results.value == 0xFFA857){down = 1;}else {down = 0;}
   
  irrecv.resume();
  }
}

void loop()
{
  int val = digitalRead(mode);
  while (val = HIGH)
  {
    int val2 = digitalRead(opto);
    while (val2 = LOW)
    {delay(1000);}//temp reverse assume beam interrupted debug
    //stop_motor()
    
    for (int pointer = 0;pointer<264;pointer++)
    {
      ok = 0;up = 0;down = 0;
      get_key();
      array1[pointer] = key;
      delay (300);
      if (ok = 1)//debug test
      {
      Serial.print ("ok = 1");//debug
      Serial.print ("\n");
      }
      if (ok = 1)beep();
      ok = 0;up = 0;down = 0;
      get_key();
      if (up = 1)
      {
      Serial.print ("up = 1");//debug
      Serial.print ("\n");
      array2[pointer] += array2[pointer];}//also echo to UART
      if (down = 1)
      {
      Serial.print ("down = 1");//debug
      Serial.print ("\n");
      array2[pointer] -= array2[pointer];}//also echo to UART
      if (ok = 1){beep();delay(200);beep();}
    }
  }

    Serial.print(array1[1]);//debug
    Serial.print(array2[1]);
    Serial.print("/n");
}

[/code]

irrx.ino (1.24 KB)

      if (ok = 1)//debug testDid you perhaps mean      if (ok == 1)//debug test? :wink:

yes, thanks.
that old 'assignment' = instead of compare ==
fool I am!

but why doesn't the 'while' need the same true/false ==?
while (val = HIGH)

val isn't assigned to HIGH..

simplystupid:
yes, thanks.
that old 'assignment' = instead of compare ==
fool I am!

but why doesn't the 'while' need the same true/false ==?
while (val = HIGH)

val isn't assigned to HIGH..

True. HIGH is assigned to val. Your terminology is backwards. But if you want a possible reason, it could be that line never gets executed. Better just put it aside for a while and then proof read the whole program again. Which of course, I don't have time to do.

The while loop needs the same as the if. The statement "while (val = HIGH)" is translated as assignment the HIGH constant to the val and then the val is tested to nonzero. In this case it is infinity loop.

val isn't assigned to HIGH..

How do you know ?

I just glanced at the rest of your code, and the same error is all over the place.