/*
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on inerrupt 0 => that is pin #2
}
void loop() {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else {
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.println( mySwitch.getReceivedProtocol() );
if (value == 22369749) {
Serial.print("Button1");
} else {
Serial.print("Button2");
}
}
mySwitch.resetAvailable();
}
}
Using RC Switch library
22369749 Is the RF signal from my remote (Button1).
However, when i press button 1 :
Received 22369749Button2 (From Serial Monitor)
When i press button 2 :
Received 22369653Button2 (From Serial Monitor)
The RF signal is showing up correctly according to button
However, the button statement is just wrong.
The statement is just to monitor things and I have more action for the arduino to do.
I know this is a really basic but I am new to these.
Thanks for your help!
1. You have declared the variable value as type int. This means that the variable can hold 16-bit data. The range in hex is: range: 0x0000 - 0xFFFF; in decimal, the range is: -32768 to 32767.
2. In your above quoted control structure, you are comparing value with 22369749 ( 15555D5 in hex; it is 25-bit). If you think that the said control structure is correct in your program, then you need to change the data type of your variable (the value) from int to the next higher one in order to accommodate 22369749 which is an integer (without decimal point) number' and not a 'floating point (with decimal point and fractional part) number'. The keyword float is used to declare a floating point number like: float x = 1.23;.
Is is appropriate for the OP to know the reason/rationality of why should he change his data type from int to unsigned long?
The original data type was int and that could mean that the OP might receive negative valued data. In such case, unsigned long could be a problem for him. Therefore, the logical advice could be like this: change the data type from int to long.
...or you just let the compiler choose the right type for you with the magic auto keyword. Very convenient when you're assigning directly from a function return.
Jiggy-Ninja:
...or you just let the compiler choose the right type for you with the magic auto keyword. Very convenient when you're assigning directly from a function return.
auto value = mySwitch.getReceivedValue();
Assuming that the function has returned FF (0xFF) via the variable auto value; I am executing this instruction: Serial.print(value);. What will I see on the display -- is it -1(minus 1) or 255?
GolamMostafa:
Assuming that the function has returned FF (0xFF) via the variable auto value; I am executing this instruction: Serial.print(value);. What will I see on the display -- is it -1(minus 1) or 255?
If .getReceivedValue() returns type 'char' you would see a 'y' with an umlaut over it (character whose code is 0xFF). If it returns byte, int, unsigned, long, or unsigned long you would see '255'.
Jiggy-Ninja:
...or you just let the compiler choose the right type for you with the magic auto keyword. Very convenient when you're assigning directly from a function return.
auto value = mySwitch.getReceivedValue();
johnwasser:
If .getReceivedValue() returns type 'char' you would see a 'y' with an umlaut over it (character whose code is 0xFF). If it returns byte, int, unsigned, long, or unsigned long you would see '255'.
But, the top post is saying that the auto keyword will (automatically) choose the right type (scope, size. magnitude, and sign). When I execute the following codes in my UNO, I see 255 on the Serial Monitor. How does the keyword (auto) know that the return value should be considered as unsigned? FF could have been taken as -1 (minus 1) due to 'HIGH' at its MS-bit position (2's complement form); but, it has not happened.
auto x = 0xFF; //mySwitch.getReceivedValue();
Serial.print(x); //shows: 255
GolamMostafa:
But, the top post is saying that the auto keyword will (automatically) choose the right type (scope, size. magnitude, and sign). When I execute the following codes in my UNO, I see 255 on the Serial Monitor. How does the keyword (auto) know that the return value should be considered as unsigned? FF could have been taken as -1 (minus 1) due to 'HIGH' at its MS-bit position (2's complement form); but, it has not happened.
auto x = 0xFF; //mySwitch.getReceivedValue();
Serial.print(x); //shows: 255
When used to declare a variable, auto will pick whatever type is used to initialize it (whatever's on the right-hand side of the equals sign). When assigning from a variable or function return it uses whatever the declared type of that value is. When assigning a literal value, there are various rules the compiler uses to decide what type the number should be. 0xFF would be considered an int.
Apart from all that, I would rarely use ‘auto’ in code I was going to show anyone else!
It’s a convenience - and for any ‘professional’ use i’d need to typeof() after using it, because I don’t want clever, but arbitrary assignment of variables wandering around my code!
lastchancename:
Apart from all that, I would rarely use ‘auto’ in code I was going to show anyone else!
It’s a convenience - and for any ‘professional’ use i’d need to typeof() after using it, because I don’t want clever, but arbitrary assignment of variables wandering around my code!
It's not arbitrary at all. In fact it is superior in some ways because if you ever change the return value of a function, using auto on the receiving variable will automatically adjust it without you having to manually change the type.