I have a sketch that interprets IR signals from a remote to toggle and dim/brighten LEDs. I'd like to be able control the system with a second, different, remote.
I'm using a case statement to toggle the pins which looks like this
case 0x153350A5: // button #1
// toggle first LED
Serial.println("1 button");
toggleLed(0);
break;
Next I try to add a second IR code using an or || statement
case (0x153350A5 || 0x50C5D1F): // button #1
// toggle first LED
Serial.println("1 button");
toggleLed(0);
break;
This compiles but neither remote toggles the pin. I've also tried writing this way, it too compiles but doesn't work
case 0x153350A5 || 0x50C5D1F: // button #1
// toggle first LED
Serial.println("1 button");
toggleLed(0);
break;[code]
I can get it to work by duplicating the whole case statement but I'm trying not to bloat up the code.
case 0x153350A5: // button #1
// toggle first LED
Serial.println("1 button");
toggleLed(0);
break;
case 0x50C5D1F: // button #1
// toggle first LED
Serial.println("1 button");
toggleLed(0);
break;
I'd also like to be able to send serial signals which would be another or statement.
Is it possible to use or statements with case statements?
Thanks
Rich
case 0x153350A5 || 0x50C5D1F
Both values are non-zero, so the result is effectively case 1:
case 0x153350A5 :
case 0x50C5D1F:
is probably what you want
That was it! Thanks so much!
So on to my next question. How can I do a serial.Read as part of this?
Can you explain what you want to do?
It's part of a home automation project. I'll be controlling LEDs around the house using the IR remotes and sending serial signals from a Raspberry Pi and or my laptop. I'll also want to use buttons and potentiometers.
No, that's a wish-list.
Can you explain what you want to do with the "Serial.read"s ?
I want to read integers 0-9 from the serial port. Each integer would correspond to the numbers on the remote.
So, read them - just make sure there's something to read first, by using Serial.available().
ok, I'm working on getting this to work but no luck. I'm able to get it to read the serial signals but when that's working it doesn't receive and interpret the IR signals.
I can't post the code in it's entirety because of restrictions on this forum. See attached. But the lines in question are as follows.
I can read from the serial monitor as follows
if (Serial.available() > 0) {
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
I'm assuming this is how to say look at the 2 varialbes, code and incomingByte
switch (code, incomingByte) // is this correct?
How can I write this sketch to read both IR signals (code) and serial signals (incomingByte)?
Thanks
Rich
_20160302-irremote-functions_4.ino (10.1 KB)
I'm assuming this is how to say...
Instead of "assuming", wouldn't it be much better to spend a little time to do some reading about proper c/c++ syntax, and actually learn the language so you don't keep making up things that are plainly illegal? In programming, you can't just make up your own ridiculous syntax.....
Regards,
Ray L.
Think about the switch() statement. It takes one variable at the top and compares it to a list. If it finds a match, it does that action.
You have two variables. Are they both on the same list?
if(GotAnIRCommand) decodeCommand(code);
if(GotASerialCommand) decodeCommand(incomingByte);
If the incoming commands have different lists or different meanings then you will need more than one decoder.