I am using a transmitter and receiver to send signals to each other. When my transmitter sends a signal i want my receiver to recognize it and print a certain message onto my LCD screen. However its only recognizing one command. Can you help me fix my code?
Edit: I also combined my switch command into one before and it still doesn't work.
Reciever Code:
#include <LiquidCrystal.h>
#include <VirtualWire.h>
LiquidCrystal lcd(12, 8, 5, 4, 3, 2);
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
void setup()
{
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
lcd.begin(16, 2);
// Print a message to the LCD.
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
switch(1,0)
{case 0:
lcd.setCursor(0, 1);
lcd.print("O.O");
break;}
switch(1,0)
{
case 1 :
lcd.setCursor(0, 1);
lcd.print("Hi! :)");
break;}
}
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
Serial.println();
}}
.. just as @johnwasser said:
you have to write your if statement such, that if "your if statement" is true - then your value = 0; else your value = 1.
Then your switch case either processes the "0" or the "1" value(s).
Sr654407:
my values are 1 and 0. so if i write it like this... what needs to be fixed now because its still not working?
switch(1,0)
{
case 0:
lcd.setCursor(0, 1);
lcd.print("O.O");
break;
case 1 :
lcd.setCursor(0, 1);
lcd.print("Hi! :)");
break;
}
The statement "switch(1,0)" is what needs to be fixed now. The switch takes a single value as an argument, and you are giving it two. I can't tell from your code where the 0 and the 1 are coming from. Are they a character that you are receiving from the virtual wire communication? If so, put them into a variable (call it "value") and then do "switch(value)", with the rest of the code as you have it written in reply #2.
You still don't have the syntax for switch correct - too many braces. Try it like this:
switch(value)
{
case 0:
lcd.setCursor(0, 1);
lcd.print("O.O");
break;
case 1 :
lcd.setCursor(0, 1);
lcd.print("Hi! :)");
break;
default:
break;
}
If the variable named "value" is zero, your lcd.print("O.O") code will run. If "value" is 1, then lcd.print("Hi! :)") will run. If it's anything else, nothing will happen.
oops! i meant "int value= (1,0);"
but i have my transmitter sending 0 when a button is not pushed and a 1 when i push the button. I want the code to be able to tell the difference between messages 1 and 0, and act accordingly.
I don't understand what you want to do with int value=(0,1);
After that statement, the variable 'value' contains what? Please answer this and AWOL's questions before moving on, so we can get an idea of where you are going with this.
Going way off track here but (as the OP found) the line
int value=(0,1);
Is actually valid C code although it doesn't do what the OP thinks it does..
The comma operator in C evaluates the first expression, discards it then evaluates the second expression and returns it. Thus in the above code 0 gets evaluated and discarded then 1 is evaluated and returned to initialize value.
So it resolves to
int value=1;
Which is why the OP's program only ever executed one branch.
Another case of "what were they thinking?" on the part of the language designers.