Issue with 433 MHz Transmitter/Receiver

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 ?

I'm using a 433MHz transmitter/receiver to send accelerometer data to control a robot.

When the accelerometer is held parallel to the ground the serial monitor shows the correct information looping, however when I tilt the accelerometer the new direction is shown once in the serial monitor then nothing after that. It appears to stop looping and the robot becomes stuck performing the same action.

I'm assuming the problem is in the receiver code as the transmitter serial monitor shows that it still continuously transmits data.

Both codes below. I have also attached a screenshot of what the receiver serial monitor looks like. As you can see when stationary the sketch loops but as soon as a direction is sent the loop stops. Hope someone can help !

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 



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.print(i + " : " + c);// debugging 


          if (buf[i]==0x73)//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)
          else if (buf[i]==0x66)//Forward 
         { 

                digitalWrite(motorPin1, LOW); 
            
                digitalWrite(motorPin2, HIGH); 
            
                digitalWrite(motorPin3, HIGH); 
            
                digitalWrite(motorPin4, LOW); 


               //digitalWrite(ledPin,HIGH); 
               Serial.println("Forward");//debugging 


          } 
           if (buf[i]==0x62)//Backward 
          { 

              digitalWrite(motorPin1, HIGH); 
          
              digitalWrite(motorPin2, LOW); 
          
              digitalWrite(motorPin3, LOW); 
          
              digitalWrite(motorPin4, HIGH); 
          

              //digitalWrite(ledPin,HIGH); 
              Serial.println("Backward");//debugging 


          } 
           if (buf[i]==0x6C)//Left  
          { 

              digitalWrite(motorPin1, LOW); 

              digitalWrite(motorPin2, HIGH); 

              digitalWrite(motorPin3, LOW); 

              digitalWrite(motorPin4, HIGH); 

             // digitalWrite(ledPin,HIGH); 
              Serial.println("Left");//debugging 


            }
             if (buf[i]==0x72)//Right  
            { 

    digitalWrite(motorPin1, HIGH); 

    digitalWrite(motorPin2, LOW); 

    digitalWrite(motorPin3, HIGH); 

    digitalWrite(motorPin4, LOW  ); 

             // digitalWrite(ledPin,HIGH); 
              Serial.println("Right");//debugging 


            } 

              

    } 

    //Serial.print("/n");// debugging 

        } 

        //delay(1000); 

}

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");
      Serial.println("f");
    }
    else if ((xval>100) && (zval>0)) //backward(transmitter tilted backward)
    {
      digitalWrite(ledPin,HIGH);
      send("b");
      Serial.println("b");
    }
   else if ((yval<0) && (xval>0)) //left(transmitter tilted to left)
    {
      digitalWrite(ledPin,HIGH);
      send("l");
      Serial.println("l");
     }
   else if ((yval>200) && (zval>10))//right(transmitter tilted to right)
    {
      digitalWrite(ledPin,HIGH);
      send("r");
      Serial.println("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
}

As I cannot find an apparent mistake in the (horribly formatted) code I guess you have a problem in the hardware. As motors are part of that hardware my first guess would be a missing fly-back diode but as you didn't provide a wiring diagram or schematics I might be miserably wrong.

Apologies for the state of the code it's my first project, still a lot to learn !

As for the hardware, I tested it with 2 new motors and the same problem occurs. When I run some test code the motors run fine, it's just with this code that when one action is executed the serial monitor stops printing. It's as if the loop just stops.

     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)

That comment looks kind of silly, doesn't it?

     if(Wire.available() == 1)   
     {     
        data[i] = Wire.read();   
     }

Why do you need an array to store one byte?

 int xval = (((data[1] & 0x03) * 256) + data[0]);

Why do you assume that the other element(s) in the array contain anything other than garbage?

  if ((0<xval<10) && (zval>100)) //stationary or stop(transmitter parallel to ground)

That is NOT how to test that a value is in a range. There are NO shortcuts. The value must be compared to the low end and then compared to the upper end.

  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");
     
    }

You are reading WAY to much into the one byte of good data and 5 bytes of garbage.

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages

byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message 

    uint8_t buf[VW_MAX_MESSAGE_LEN];

    uint8_t buflen = VW_MAX_MESSAGE_LEN;

Explain why you need a global array and global variable that you don't use.

  int i;

char c = char(i);

So, i contains some random garbage, and you cast it to a char. Why do you think you can do that? Why do you think you need to do that?

      Serial.println(i + " : " + c);// debugging

Printing c seems completely pointless, since I know, and you do, too, that it is garbage.

          if (buf[i]==73)//Stationary

Why should I have to go look up what a 73 means?

          if (buf[i] == 'I') //Stationary

Now, I don't need to go look up what an 'I' means. Except that I have to wonder why 'I' means stationary. Must be a language thing...

        delay(1000);

There is NO good excuse for this.

As for the hardware, I tested it with 2 new motors and the same problem occurs. When I run some test code the motors run fine, it's just with this code that when one action is executed the serial monitor stops printing. It's as if the loop just stops.

Why did you still not post a wiring diagram of your setup?

I never wrote that I think the motors are broken. Motors contain coils and these tend to have spikes of return current if they are turned on and off. Without fly-back diodes or other means to solve this problem your power supply might show a voltage curve that is not appropriate for the circuit.

Apologies for the state of the code it's my first project, still a lot to learn !

The IDE has an "Auto Format" option in the "Tools" menu.