//Connect the Receiver data pin to Arduino pin 11
#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
#define ep 4
#define tp 3
int en1=9;
int en2=10;
int in1=5;
int in2=6;
int in3=7;
int in4=8;
int buzzerPin=2;
int ledPin=13;//led on pin 13 is ON except when bot is stationary
long duration, distance;
void setup()
{
Serial.begin(9600);//Initialise theserial connection debugging
pinMode(tp,OUTPUT);
pinMode(ep,INPUT);
pinMode(ledPin,OUTPUT);
pinMode(buzzerPin,OUTPUT);
pinMode(en1,OUTPUT);
pinMode(en2,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
digitalWrite(tp,LOW);
delayMicroseconds(2);
digitalWrite(tp,HIGH);
delayMicroseconds(10);
digitalWrite(tp,LOW);
duration=pulseIn(ep,HIGH);
distance=duration/58.2;
Serial.println(distance);
if (distance < 10 )
{
Serial.print("Obstacle detected\n");
digitalWrite(buzzerPin,HIGH);
}
else
{
Serial.print("Obstacle not detected\n");
digitalWrite(buzzerPin,LOW);
}
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
Serial.print("Got: ");//debugging
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i],HEX);//You may also use integer values debugging
Serial.print(' ');// debugging
if (buf[i]==0x6F)//Stationary
{
digitalWrite(en1,LOW);
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(en2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
Serial.print("Stationary");
digitalWrite(ledPin,LOW);
}
else
{
if(buf[i]==0x68)//Forward
{
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(en2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.print("Forward");
digitalWrite(ledPin,HIGH);
}
if (buf[i]==0x69)//Backward
{
digitalWrite(en1,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(en2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.print("Backward");
digitalWrite(ledPin,HIGH);
}
if (buf[i]==0x62)//Right
{
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(en2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.print("Right");
digitalWrite(ledPin,HIGH);
}
if (buf[i]==0x65)//Left
{
digitalWrite(en1,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(en2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
Serial.print("Left");
digitalWrite(ledPin,HIGH);
}
}
}
Serial.print("\n");// debugging
}
delay(100);
}