Hoping someone can help. I'm trying to build a gesture controlled robot and I'm having issues with the transmitting and receiving.
I'm using the FS1000A 433MHz transmitter/receiver with the ADXL345 accelerometer and my code is;
TRANSMITTER CODE
//Connect the Transmitter data pin to Arduino pin 12
//If you don't want to use pin 12 use vw_set_tx_pin(transmit_pin), with transmit_pin being desired pin ,in void setup
//Refer to the documentation in link for more details
#include<Wire.h>
// ADXL345 I2C address is 0x53(83)
#define Addr 0x53
#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()
{
vw_setup(2000);//Bits per second
pinMode(ledPin,OUTPUT);
//Serial.begin(9600);//Initialise the serial connection debugging
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select bandwidth rate register
Wire.write(0x2C);
// Normal mode, Output data rate = 100 Hz
Wire.write(0x0A);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select power control register
Wire.write(0x2D);
// Auto-sleep disable
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data format register
Wire.write(0x31);
// Self test disabled, 4-wire interface, Full resolution, Range = +/-2g
Wire.write(0x08);
// Stop I2C transmission
Wire.endTransmission();
delay(300);
}
void loop()
{
unsigned int data[6];
for(int i = 0; i < 6; i++)
{
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write((50 + i));
// Stop I2C transmission
Wire.endTransmission();
// Request 1 byte of data
Wire.requestFrom(Addr, 1);
// Read 6 bytes of data
// xAccl lsb, xAccl msb, yAccl lsb, yAccl msb, zAccl lsb, zAccl msb
if(Wire.available() == 1)
{
data[i] = Wire.read();
}
}
// Convert the data to 10-bits
int xval = (((data[1] & 0x03) * 256) + data[0]);
if(xval > 511)
{
xval -= 1024;
}
int yval = (((data[3] & 0x03) * 256) + data[2]);
if(yval > 511)
{
yval -= 1024;
}
int zval = (((data[5] & 0x03) * 256) + data[4]);
if(zval > 511)
{
zval -= 1024;
}
// Output data to serial monitor
Serial.print("Acceleration in X-Axis is : ");
Serial.println(xval);
Serial.print("Acceleration in Y-Axis is : ");
Serial.println(yval);
Serial.print("Acceleration in Z-Axis is : ");
Serial.println(zval);
delay(300);
//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 ((0<xval<10) && (zval>100)) //stationary or stop(transmitter parallel to ground)
{
digitalWrite(ledPin,LOW);
send("s");
Serial.println("s");
}
else if ((xval<0) && (zval>50)) //forward(transmitter tilted forward)
{
digitalWrite(ledPin,HIGH);
send("f");
}
else if ((xval>100) && (zval>0)) //backward(transmitter tilted backward)
{
digitalWrite(ledPin,HIGH);
send("b");
}
else if ((yval<0) && (zval>0)) //left(transmitter tilted to left)
{
digitalWrite(ledPin,HIGH);
send("l");
}
else if ((yval>200) && (zval>10))//right(transmitter tilted to right)
{
digitalWrite(ledPin,HIGH);
send("r");
}
}
//delay(1000);
void send(char *message)//send function definition
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}
RECEIVER CODE
#include <VirtualWire.h>
//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
//L293D
//Motor A
const int motorPin1 = 5; // Pin 14 of L293
const int motorPin2 = 6; // Pin 10 of L293
//Motor B
const int motorPin3 = 10; // Pin 7 of L293
const int motorPin4 = 9; // Pin 2 of L293
const int ledPin=13;//led on pin 13 is ON except when bot is stationary
const int transmit_pin=12;
const int receive_pin=11;
void setup()
{
Serial.begin(9600);//Initialise the serial connection debugging
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
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;
char c = char(i);
Serial.print("Got: ");//debugging
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i],HEX);//You may also use integer values debugging
Serial.println(i + " : " + c);// debugging
if (buf[i]==73)//Stationary
{
digitalWrite(motorPin1,LOW);
digitalWrite(motorPin2,LOW);
digitalWrite(motorPin3,LOW);
digitalWrite(motorPin4,LOW);
digitalWrite(ledPin,LOW);
Serial.println("Stationary");//debugging
}
else if(buf[i]==66)//Forward
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
digitalWrite(ledPin,HIGH);
Serial.println("Forward");//debugging
}
else if (buf[i]==0x62)//Backward
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
digitalWrite(ledPin,HIGH);
Serial.println("Backward");//debugging
}
else if (buf[i]==0x72)//Left
{
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
digitalWrite(ledPin,HIGH);
Serial.println("Left");//debugging
}
else if (buf[i]==0x6C)//Right
{
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW );
digitalWrite(ledPin,HIGH);
Serial.println("Right");//debugging
}
}
}
delay(1000);
}
When the acceleromter is held flat the serial monitor on the receiver circuit is repeatedly showing 73 and the wheels on the robot remain stationary, however when tilted backwards the serial monitor then displays 62 and prints "backwards", the wheels on the robot go backwards but the serial monitor does not print any more lines. The circuit seems stop looping when a direction is detected.
Any ideas on where I'm going wrong ?