0
#include <IRremote.h>

int RECV_PIN = 11; // IR Receiver - Arduino Pin Number 11
IRrecv irrecv(RECV_PIN);
decode_results results;

int led = 12;

void setup() {                
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver     
}

void loop() {
  if(irrecv.decode(&results)) //this checks to see if a code has been received
{
    if(results.value == FF6897) //if the button press equals the hex value FF6897
    {
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);               // wait for a second
        digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
        delay(1000);               // wait for a second//do something useful here
    }
    irrecv.resume(); //receive the next value
}
  }

The issue is in the second if in the void loop the error I get is

exit status 1 'FF6897' was not declared in this scope

why does he do this? Why doesnt it just accept my IRcode for comparison.

  • I think this is a good question, because it isn't obvious why its failing unless you know C/C++, the numbering prefix is not intuitive. Maybe a little research before asking would have helped, but... – Code Gorilla Jun 15 '17 at 6:59
0

Nothing here that hasn't been covered in comments already but just putting it in the form of an answer to save digging...

How is the compiler supposed to know that a number is hex rather than decimal?

Sure 1234beef is obviously hex but what about 12345678? Is that hex or decimal?

You need some method for the compiler to know.

You also need some way for the compiler to know if something is a number or a variable. Take the code if (x == deadbeef) {...} is deadbeef a hex number or a variable name?

C uses a very simple convention. If it starts with a number it's a number, if it starts with a letter it's a variable. This is why variable names can contain numbers but can't start with them.

If an integer starts with an 0 e.g. 0123 then it's in octal.
If an integer starts with 0x e.g. 0x123 then it's in hex.
If an integer starts with something other than a 0 e.g. 123 then its in decimal.
e.g.

printf("123 = %d\r\n0x123 = %d\r\n0123 = %d\r\n", 123 , 0x123 , 0123);

returns

123 = 123
0x123 = 291
0123 = 83

This convention, especially the 0x prefix for hexadecimal, is very widely used in other programming languages.

-1

By comparing my code to another one, I found out that putting 0x in front of the hex will fix it, I don't have a clue why this is though.

  • 5
    Hexadecimal literals are typically preceded by 0x, else how would the compiler distinguish the variable name deadbeef from the hexadecimal value deadbeef? – TisteAndii Jun 14 '17 at 20:27
  • There is also the prefix 0 and b for declaring Octal (base 8) and binary. Octal can cause you issues when declaring decimal numbers that you want to pad with zeros, i.e. 00100 is 64 not 100 with to leading zeros. – Code Gorilla Jun 15 '17 at 6:57
  • @CodeGorilla the 0b prefix for binary isn't very standard yet. I believe C++ 14 makes it standard but c++ 11 is only just becoming common so that's at least a few years off general usage. Until then its down to individual compilers and extensions whether it's supported. Personally right now I'd avoid it even if the tool I was using supported it, the lack of portability isn't worth the minimal benefits it gives. – Andrew Jun 15 '17 at 8:44
  • @Andrew - The only place I had seen it was here :) I agree about avoiding binary, but mainly because hex is much easier. – Code Gorilla Jun 16 '17 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.