I am doing a project that combines hand gesture control (using an accelerometer) with obstacle avoidance (using an ultrasonic sensor)
So far, I successfully linked the hand gesture control with the dc motors powering the robot and I am able to control it remotely (though with the help of a code I found online that already had four messages sent through the rf module that I managed to utilize for forward, backward, left and right)
The issue I am having currently is how to add the obstacle avoidance capability of the robot to the code.
The concept is that the robot should be controllable remotely and when a push button is pressed; it performs its operation automatically
The transmitter code is shown below:
#include <VirtualWire.h>
int xPin=0;
int yPin=1;
int zPin=2;
int ledPin=13;//led on pin 13 is ON except when transmitter is parallel to the ground
void setup()
{
Wire.begin();//Initialise the serial connection debugging
lcd.begin(16, 2);
vw_setup(2000);//Bits per second
pinMode(ledPin,OUTPUT);
Serial.begin(9600);//Initialise the serial connection debugging
}
void loop()
{
int xval=analogRead(xPin);
int yval=analogRead(yPin);
int zval=analogRead(zPin);
Serial.print("xval=");
Serial.println(xval);
Serial.print("yval=");
Serial.println(yval);
Serial.print("zval=");
Serial.println(zval);
delay(1000); //used to display values after 1s delay
Serial.print("\n");
if ((xval>330 && xval<370) && (yval>330 && yval<370)) //stationary or stop(transmitter parallel to ground)
{
digitalWrite(ledPin,LOW);
send("s");
}
else
{
if ((xval>371 && xval<420) && (yval>330 && yval<370)) //forward(transmitter tilted forward)
{
digitalWrite(ledPin,HIGH);
send("f");
}
if ((xval>280 && xval<329) && (yval>330 && yval<370)) //backward(transmitter tilted backward)
{
digitalWrite(ledPin,HIGH);
send("a");
}
if ((xval>330 && xval<370) && (yval>371 && yval<420)) //left(transmitter tilted to left)
{
digitalWrite(ledPin,HIGH);
send("l");
}
if ((xval>330 && xval<370) && (yval>280 && yval<329))//right(transmitter tilted to right)
{
digitalWrite(ledPin,HIGH);
send("r");
}
}
//delay(1000);
if ((zval>280 && zval<310) )//right(transmitter tilted to right)
{
digitalWrite(ledPin,HIGH);
send("z");
}
}
void send(char *message)//send function definition
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}
and the receiver code is
#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 relay=12;
int lm = 4;
int lmr = 5;
int rm = 6;
int rmr = 7;
int ledPin = 13; //led on pin 13 is ON except when bot is stationary
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(lm, OUTPUT);
pinMode(lmr, OUTPUT);
pinMode(rm, OUTPUT);
pinMode(rmr, OUTPUT);
pinMode(relay, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
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] == 0x73) //Stationary
{
digitalWrite(lm, LOW);
digitalWrite(lmr, LOW);
digitalWrite(rm, LOW);
digitalWrite(rmr, LOW);
delay(5000);
if (buf[i] == 0x73)
digitalWrite(ledPin, LOW);
}
else
{
if (buf[i] == 0x66) //Forward
{
digitalWrite(lm, LOW);
digitalWrite(lmr, HIGH);
digitalWrite(rm, HIGH);
digitalWrite(rmr, LOW);
digitalWrite(relay, HIGH);
digitalWrite(ledPin, HIGH);
}
if (buf[i] == 0x61) //Backward
{
digitalWrite(lm, HIGH);
digitalWrite(lmr, LOW);
digitalWrite(rm, LOW);
digitalWrite(rmr, HIGH);
digitalWrite(ledPin, HIGH);
}
if (buf[i] == 0x72) //Left
{
digitalWrite(lm, LOW);
digitalWrite(lmr, HIGHE);
digitalWrite(rm, LOW);
digitalWrite(rmr, LOW);
digitalWrite(ledPin, HIGH);
}
if (buf[i] == 0x6C) //Right
{
digitalWrite(lm, LOW);
digitalWrite(lmr, LOW);
digitalWrite(rm, HIGH);
digitalWrite(rmr, LOW);
digitalWrite(ledPin, HIGH);
}
}
}
Serial.print("\n");// debugging
}
//delay(1000);
}
Can someone explain to me the relationship between the "send s" from the transmitter code and the 0X73 in the receiver.
Thank you.
your quick responses would be appreciated