I am receiving message using rf 433mhz. But I am receiving an repeating infinite messages on serial monitor, but I want only two message on serial monitor. What should be added to code for this.
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);
vw_set_tx_pin(12); // Pin data
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
}
void loop()
{
irsensor();
alcoholsensor();
}
void irsensor()
{
Obstacle = digitalRead(ObstaclePin);
if (Obstacle == LOW)
{
Serial.println("obstacle absent");
send("message =Abse");
}
else
{
Serial.println("obstacle present");
send("message =Pres");
}
}
void alcoholsensor()
{
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 =Less");
}
else {
Serial.println("value is more");//if threshold not reached, LED remains off
send("message =More");
}
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message has gone
}
Receiver code
/*
2 SimpleReceive
3 This sketch displays text strings received using VirtualWire
4 Connect the Receiver data pin to Arduino pin 11
5 */
#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;
void setup()
{
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_set_rx_pin(11);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
pinMode(Led, OUTPUT);
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print("Received: ");
for (int i = 0; i <= messageLength; i++)
{
Serial.write(message[i]);
}
Serial.println();
}
}