Code Advice- Switch-Case Command

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();
}}

You have to pass a value to 'switch'. You probably want to get that value from your message. Use one 'switch' containing all of the cases.

switch(value) {
case 1:
    foo();
    break
case 2:
    bar();
    break;
.
.
.
default:
    baz();
    break;
}

is just shorthand for:

if (value == 1) {
    foo();
    } else if (value == 2) {
    bar();
    } else if (value == ...) {
.
.
.
    } else {
    baz();
    }

It's easier to read because you know that each integer is being compared to 'value'.

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;
}

.. 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).

The expression "0,1" means 0. See: Comma operator - Wikipedia

Since you are giving 'switch' the value 0 it will always execute only case 0.

Are you still not doing anything with the contents of your message?

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.

I tried to fix my code and set value, but it still didn't work. any advice?

#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
int value = (1,0);

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(value)
{case 0:
      lcd.setCursor(0, 1);
      lcd.print("O.O");
      break;}
  switch(value)
{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();
}}

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.

Thank You, but it's still not working? could how i wrote "int variable=(1,0)" be wrong?

Yes very wrong.
Which of the two values did you intend to be assigned to the variable "variable"?

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.

oops! i meant "int value= (1,0);"

No, you didn't.

huh? Yes, at first i put "variable" but its "value" that i put into my code.

In all the examples provided with the IDE, have you ever seen one that has a construct like int value= (1,0);?

no but it wont accept it with out the parenthesis

OK, tell me what you think it does, and why you feel it necessary.

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.

Just leave off the "int".
value = (1, 0) is a python tuple. You need to come back to C/C++.

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.

thank you, so how would i get it to execute the second branch along with the first branch?