Problem with virtual wire and accelerometer

I’ve spent almost 4 hours going back and forth between these two codes, trying to find out why the robot stops receiving data when the Z-axis puts out 1 - 14. And it only happens at that range, does it stop receiving and/or transmitting, I don’t know which exactly.

I send out an array of such 25525548, array[0] = 255(y-axis), array[1] = 255(z-axis), array[2] = ‘0’(state).
It will go from 0 - 255 for y-axis, with no problem, but it will only go from 0 to 130, and 255 for z-axis. BUT… if I try to go past 130 or lower than 255, the robot stops receiving, and won’t startup again until the power is recycled. It’s driving me nuts!

I made a simple code for both sending and receiving and it works fine. So after much frustration, I found out that if I comment out my move function, it works great. But with it uncommented, it does not work. WTF!

Transmitting code:

#include <VirtualWire.h>
char Array[12];

void setup()
{
  Serial.begin(9600);	  // Debugging only
  Serial.println("Sending"); // Debugging only

  // Initialise the IO and ISR
  //vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);	 // Bits per sec
  vw_set_tx_pin(33); //RF sending pin
  pinMode(37, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(53, OUTPUT);
  digitalWrite(37, LOW); // RF ground
  digitalWrite(35,HIGH); // RF Vcc
  digitalWrite(53,HIGH); // accelormeter Vcc

  pinMode(13, INPUT); //accel Z
  pinMode(14, INPUT); //accel Y
  pinMode(15, INPUT); //accel X
}

void loop()
{
  Array[0] = map(analogRead(14),390,612,0,255); // Y raw data to 0 - 255
  Array[1] = map(analogRead(13),420,600,0,255); // Z raw data to 0 - 255
  Array[2] = '0'; //state

  vw_send((uint8_t*)Array, strlen(Array));
  vw_wait_tx(); 
}

Receiving code:

/*
Robot code
 */
#include <VirtualWire.h>
int DRV1,DRV2,STRR,STRL;
int y = 0;
int z = 0;
int s = 0;
void move(int z, int y, int s);

int currentCommand = 0;

//int ledpin = 13;
byte Mopen = 4;
byte Mclosed = 2;
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM
// LED connected to pin 2 (on-board LED)

void setup()
{
  //pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);                                
  pinMode(Mclosed, OUTPUT);
  pinMode(M1L, OUTPUT);                                
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                                
  pinMode(M2R, OUTPUT);
  pinMode(13, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps
  Serial.println("Receiving");
  vw_setup(2000);	 // Bits per sec
  vw_set_rx_pin(8);
  vw_rx_start();
}

void loop() {
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen)) // Non-blocking
    {
      digitalWrite(13,HIGH);
      //x=map(buf[0],13,118,10,0);
       y=map(buf[0],0,255,-10,10);
       z=map(buf[1],0,255,-15,15);
      
      //y= buf[0];
      //z= buf[1];
      s= buf[2];
      //vw_wait_rx();
         
     //Serial.print(x);
     //Serial.print(", ");
      Serial.print(y);
      Serial.print(", ");
      Serial.print(z);
      Serial.print(", ");
      Serial.print(s);
      Serial.println();
      //move(z,y,s);
   }
  else digitalWrite(13, LOW);
}  
    
void move(int z, int y, int s)
{  
  //Movement variables 
  int DRV2 = map(z, -15, 0, 255, 0);
  int DRV1 = map(z, 0, 15, 0, 255);
  int STRL = map(y, -10, 0, 255, 0);
  int STRR = map(y, 0, 10, 0, 255);

  if(z > 0)//forwards               
  {
    //Serial.println("Forward"); 
    analogWrite(M1L, constrain(abs(DRV1 - STRL),0,255)); analogWrite(M1R, constrain(abs(DRV1 - STRR),0,255));   
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);   
  }
  
  else if(z < 0)//backwards               
  {
    //Serial.println("Reverse"); 
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);   
    analogWrite(M2L, constrain(abs(DRV2 - STRL),0,255)); analogWrite(M2R, constrain(abs(DRV2 - STRR),0,255));   
  }
  
   else if(z < 4 && z > -4 && y > 0)//Right               
  {
    //Serial.println("Forward with turning"); 
    digitalWrite(M2L, LOW); analogWrite(M2R, STRR);   
    analogWrite(M1L, STRR); digitalWrite(M1R, LOW);
  }
  
  else if(z < 4 && z > -4 && y < 0)//Left              
  {
    //Serial.println("Reverse with turning"); 
    analogWrite(M2L, STRL); digitalWrite(M2R, LOW);   
    digitalWrite(M1L, LOW); analogWrite(M1R, STRL);   
  }

  else //full stop
  { 
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);        
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);    
  }

// Claw movement
   if(s == '1')
    {
      Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }

    if(s == '2')
    {
      Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }

    if(s == '0')
    { 
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    }
}
  Array[0] = map(analogRead(14),390,612,0,255); // Y raw data to 0 - 255
  Array[1] = map(analogRead(13),420,600,0,255); // Z raw data to 0 - 255
  Array[2] = '0'; //state

  vw_send((uint8_t*)Array, strlen(Array));

I don't get that bit. Can you explain it? You are setting up 3 bytes of Array, and then doing an strlen? Why?

That's apparently how it works, I never used virtual wire before and all the examples have it set up that way. What would be the correct way to do this then?

Are you sending characters or an analog voltage ?

I can send and receive the data with no problems until I uncomment out move(). I already tried itoa and that did not work properly, and yes I did include a NULL char.

HazardsMind: That's apparently how it works, I never used virtual wire before and all the examples have it set up that way. What would be the correct way to do this then?

What examples?

Even without seeing them, you do strlen on a "string". Not binary data.

  Array[2] = '0'; //state

If Array is a char array, assigning the character '0' to the element is NOT the same as assigning the value 0 (or '\0', also known as NULL) to it.

Afternoon. Ok I went back to using itoa and alone, that somewhat worked. I then tried to use strcat to add a null character, and that gave me crazy data, so that did not work.

I even tried to send out this, int K=0; itoa(K++,msg,10);

I send msg out, and get back 48 - 57, but then is just stops and does not send any more. At this point I have tried everything I can think of, and even searched the forum for projects simular to mine, but none were help full. Is there something i'm just not seeing here?

All I want to do is send out an array of ints (255,255,255), convert them to a string (itoa), send / receive them and get back (255,255,255), what is so difficult about that? It seems the deeper I get into this, the further way I get.

This was one example program that allowed me to send data to the robot, http://arduino.cc/forum/index.php/topic,44240.0.html This kinda works, I can get the robot to go in reverse and turn, but it won't go forward. I don't understand what the problem is. How can it go reverse just fine, but not forward?

HazardsMind: Afternoon. Ok I went back to using itoa and alone, that somewhat worked. I then tried to use strcat to add a null character, and that gave me crazy data, so that did not work.

You might want to look up "sprintf".

Ok, I try it, but it won't explain why the robot goes in reverse with full speed control, but stops sending when I want it to go forward.

Perhaps post the amended sketches. I believe in solving one problem at a time.

The two I originally posted are what gives me full reverse control, but not forward. I’m not home right now to try any.

Ok, sprintf works like a charm, but now i'm working on spliting it into the correct variables.

strtok() and atoi() perform the reverse of sprintf().

I finally got it. It took me a while to figure out that the Virtual wire library takes over timer1, (pins 9 and 10) and one of my motors was connected to pin 9. Once I switched the motor from pin 9 to pin 11 it worked beautifully. I got to tweak the sending rate, but overall it works.

I took a simple, rather messy route but it works.

Transmitting code:

#include <VirtualWire.h>

char Array[20];
int X,Y,Z,State;

void setup()
{
  Serial.begin(9600);	  // Debugging only
  Serial.println("Sending"); // Debugging only

  // Initialise the IO and ISR
  //vw_set_ptt_inverted(true); // Required for DR3100
  vw_setup(2000);	 // Bits per sec
  vw_set_tx_pin(33); //RF sending pin
  pinMode(37, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(53, OUTPUT);
  digitalWrite(37, LOW); // RF ground
  digitalWrite(35,HIGH); // RF Vcc
  digitalWrite(53,HIGH); // accelormeter Vcc

  pinMode(13, INPUT); //accel Z
  pinMode(14, INPUT); //accel Y
  pinMode(15, INPUT); //accel X
}

void loop()
{ 
  X = 255; // dummy data
  Y = map(analogRead(14),390,612,0,255); // Y raw data to 0 - 255
  Z = map(analogRead(13),420,600,0,255); // Z raw data to 0 - 255
  State = '0'; // 48
  
  sprintf(Array, "%d,%d,%d,%d ",X,Y,Z,State);
  vw_send((uint8_t*)Array, strlen(Array));
  vw_wait_tx(); 
}

Receiving code:

/*
simple LED test
 */
#include<string.h>
#include <VirtualWire.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int z = 0;
int s = 0;
char c[30];
void move(int z = 0,int y = 0,int s = 0);

byte ledpin = 13;
byte Mopen = 4;
byte Mclosed = 2;
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 11;// PWM
byte M2R = 6;// PWM
// LED connected to pin 2 (on-board LED)

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);                                
  pinMode(Mclosed, OUTPUT);
  pinMode(M1L, OUTPUT);                                
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                                
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps
  vw_setup(2000);	 // Bits per sec
  vw_set_rx_pin(8);
  vw_rx_start();
}

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;
     // Message with a good checksum received, dump it.
	Serial.print("Got: ");
	
	for (i = 0; i < buflen; i++){
           digitalWrite(ledpin, HIGH);
           c[i] = buf[i];
         }
         
        x=atoi(strtok(c,","));
        y=atoi(strtok(NULL,","));
        z=atoi(strtok(NULL,","));
        s=atoi(strtok(NULL," "));

       /* Serial.print(x);
        Serial.print(",");
        Serial.print(y);
        Serial.print(",");
        Serial.print(z);
        Serial.print(",");
        Serial.print(s);
        Serial.println();*/
        move(z, y, s);  
   }
} 
   
  void move(int z, int y, int s)
  {  

  //Movement varibles
  
  int DRV2 = map(z, 0, 135, 255, 0);
  int DRV1 = map(z, 140, 255, 0, 255);
  int STRL = map(y, 0, 135, 255, 0);
  int STRR = map(y, 140, 255, 0, 255);
  
  /*
  Serial.println(DRV1);
  Serial.print(",");
  Serial.print(DRV2);
  Serial.print(",");
  Serial.print(STRL);
  Serial.print(",");
  Serial.print(STRR);
  Serial.println();
  */
  if(z > 137)//forwards               
  {
    //Serial.println("Forward with turning"); 
    analogWrite(M1L, constrain(abs(DRV1 - STRL),0,255)); analogWrite(M1R, constrain(abs(DRV1 - STRR),0,255));   
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);   
  }

  else if(z < 137)//backwards               
  {
    //Serial.println("Reverse with turning"); 
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);   
    analogWrite(M2L, constrain(abs(DRV2 - STRL),0,255)); analogWrite(M2R, constrain(abs(DRV2 - STRR),0,255));   
  }

   else if(z < 141 && z > 133 && y > 137)//Right               
  {
    //Serial.println("360 left"); 
    digitalWrite(M2L, LOW); analogWrite(M2R, STRR);   
    analogWrite(M1L, STRR); digitalWrite(M1R, LOW);
  }

  else if(z < 141 && z > 133 && y < 137)//Left              
  {
     //Serial.println("360 right"); 
    analogWrite(M2L, STRL); digitalWrite(M2R, LOW);   
    digitalWrite(M1L, LOW); analogWrite(M1R, STRL);   
  }

  else //full stop
  { 
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);        
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);    
  }

   if(s == 1)
    {
      Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }

    if(s == 2)
    {
      Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }

    if(s == 0)
    { 
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    } 

   else{
    digitalWrite(ledpin, LOW); 
    x=0; y=0; z=0; s=0;
  } 
}