I want to receive two messages at receiver end that is one after other. And want to blink led according to it. The code is compiling but it doesnot work according to it.What is the problem in code? Here is
Receiver code
#include <VirtualWire.h>
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 LED =13;
int pin=11;
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
pinMode(LED,OUTPUT);
}
void loop()
{
char message[VW_MAX_MESSAGE_LEN + 1];
if (vw_get_message(message, &messageLength)) // Non-blocking
{ // Existing start of if body
message[messageLength] = '\0';
if(strcmp(message [0], "obstacle absent") == 0)
{
digitalWrite(LED,HIGH);
}
if(strcmp(message [0], "obstaclepresent") == 0)
{
digitalWrite(LED,LOW);
}
if(strcmp(message [1], "value is less") == 0)
{
digitalWrite(LED,HIGH);
}
if(strcmp(message [1], "value is more") == 0)
{
digitalWrite(LED,LOW);
};
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
Serial.println("");
delay(200);
}
}
}
transmitter code
#include <VirtualWire.h>
int ObstaclePin = 7; // This is our input pin
int Obstacle = HIGH; // HIGH MEANS NO OBSTACLE
const int AOUTpin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin=8;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
int limit;
int value;
void setup()
{
pinMode(ObstaclePin, INPUT);
pinMode(DOUTpin, INPUT);
Serial.begin(9600);
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
}
void loop()
{
Obstacle = digitalRead(ObstaclePin);
if (Obstacle == LOW)
{
Serial.println("obstacle absent");
send("message [0]=obstacle absent");
}
else
{
Serial.println("obstacle present");
send("message [0]=obstaclepresent ");
}
value= analogRead(AOUTpin);//reads the analaog value from the alcohol sensor's AOUT pin
limit= digitalRead(DOUTpin);//reads the digital value from the alcohol sensor's DOUT pin
Serial.print("Alcohol value: ");
Serial.println(value);//prints the alcohol value
Serial.print("Limit: ");
Serial.print(limit);//prints the limit reached as either LOW or HIGH (above or underneath)
delay(100);
if (limit == HIGH){
Serial.println("value is less");//if limit has been reached, LED turns on as status indicator
send("message [1]=value is less");
}
else{
Serial.println("value is more");//if threshold not reached, LED remains off
send("message [1]=value is more");
}
delay(200);
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message has gone
}