Wireless control system problem

So im using two xbees. i need this code to work for a few hours so it needs to be solid. Let me know if you have any questions. Here is what i have:

Send:

int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12

int Finger1 = 2;
int Finger2 = 3;
int Finger3 = 4;
int Finger4 = 5;
int Rotation =  0;


void setup()
{
   Serial.begin(9600);
  pinMode(pwm_a, OUTPUT);  //Set control pins to be outputs
  pinMode(dir_a, OUTPUT);
  digitalWrite(dir_a, LOW);
  
}

void loop()
{  
    while((char)Serial.read() !='.');
    
    byte val = Serial.read();
     
    analogWrite(pwm_a, val);  
    
   int FingerV1 = analogRead(Finger1);
   int FingerV2 = analogRead(Finger2);
   int FingerV3 = analogRead(Finger3);
   int FingerV4 = analogRead(Finger4);
   int RotationV1 = analogRead(Rotation);
   
   if (FingerV1 < 30) FingerV1 = 30;
   else if (FingerV1 > 80) FingerV1 = 80;
   
   if (FingerV2 < 45) FingerV2 = 45;
   else if (FingerV2 > 69) FingerV2 = 69;
   
   if (FingerV3 < 22) FingerV3 = 22;
   else if (FingerV3 > 87) FingerV3 = 87;
   
   if (FingerV4 < 12) FingerV4 = 12;
   else if (FingerV4 > 62) FingerV4 = 62;
   
   if (RotationV1 < 300) RotationV1 = 300;
   else if (RotationV1 > 600) RotationV1 = 600;
  
   byte servoVal1 = map(FingerV1,30, 80, 0, 255);//middle
   byte servoVal2 = map(FingerV2,69, 45, 0, 100);//thumb
   byte servoVal3 = map(FingerV3,87, 22, 0, 255);//ring
   byte servoVal4 = map(FingerV4,12, 62, 0, 255);//pointer
   byte servoVal5 = map(RotationV1,300, 600, 0, 255);//Rotation
   
   Serial.print(".");
   Serial.print(servoVal1);
   Serial.print(",");
   Serial.print(servoVal2);
   Serial.print(",");
   Serial.print(servoVal3);
   Serial.print(",");
   Serial.print(servoVal4);
   Serial.print(",");
   Serial.print(RotationV1);
   

   delay(10);
}

Receive:

#include 

Servo myservo1;  // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;
Servo myservo7;

int potpin = A0;
int val;

void setup()
{
   Serial.begin(9600);
   
   myservo1.attach(2); //middle
   myservo2.attach(3); //thumb
   myservo3.attach(4); //ring
   myservo4.attach(5); //pinky
   myservo5.attach(6); //pointer
   myservo7.attach(7); //swivel
   
   Serial.print(".");
}

void loop()
{

   while((char)Serial.read() !='.');
   
   byte vals[5];
   vals[0] = Serial.read();
   Serial.read();
   vals[1] = Serial.read();
   Serial.read();
   vals[2] = Serial.read();
   Serial.read();
   vals[3] = Serial.read();
   Serial.read();
   vals[4] = Serial.read();
   
    myservo1.write(vals[0]);
    myservo2.write(vals[1]);
    myservo3.write(vals[2]);
    myservo4.write(vals[2]);
    myservo5.write(vals[3]);
    myservo7.write(vals[4]);
    
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  byte val = map(val, 0, 950, 0, 255);     // scale it to use it with the servo (value between 0 and 180)                 // sets the servo position according to the scaled value 
  if (val>250){val=250;}
  Serial.print(".");
  Serial.print(val);
   
}

Let me know if you have any questions.

What question are you asking?

In both sketches, you are doing Serial.reads, without checking that there is anything to read.

dxw00d:

Let me know if you have any questions.

What question are you asking?

In both sketches, you are doing Serial.reads, without checking that there is anything to read.

So do I just use if serial available? If so where would I put it?

Before the reads?

http://gammon.com.au/serial

njkl44: Let me know if you have any questions.

Does it compile?

Does it do what you intend?

Does it do it reliably for as long as you need?

PeterH:

njkl44: Let me know if you have any questions.

Does it compile?

Does it do what you intend?

Does it do it reliably for as long as you need?

it does compile. Other than that nothing works.

So im using two xbees.

About which you have told us what? Nothing. Nothing about the type. Nothing about how they are attached to the Arduino. Nothing about how they are configured. Nothing about whether you know that they are communicating.

PaulS:

So im using two xbees.

About which you have told us what? Nothing. Nothing about the type. Nothing about how they are attached to the Arduino. Nothing about how they are configured. Nothing about whether you know that they are communicating.

Well that shouldnt matter to much should it? they are just standard xbees attached with a shield. I have config them with a custom channel so only they can talk to eachother.

[quote author=Nick Gammon link=topic=89212.msg669284#msg669284 date=1327742140] Before the reads?

http://gammon.com.au/serial [/quote] Something like this?

void loop()
{
  if (Serial.available ())

   while((char)Serial.read() !='.');
   
   byte vals[5];
   vals[0] = Serial.read();

Well that shouldnt matter to much should it?

I guess that it doesn't matter that they are compatible. Doesn't matter that they might be attached to shields that are configured to use something other than the Serial port. Doesn't matter that they might not be configured correctly.

You have two Arduinos that are failing to properly exchange data. Ignoring the mechanism that is used to exchange the data hardly seems like a good idea.

PaulS:

Well that shouldnt matter to much should it?

I guess that it doesn't matter that they are compatible. Doesn't matter that they might be attached to shields that are configured to use something other than the Serial port. Doesn't matter that they might not be configured correctly.

You have two Arduinos that are failing to properly exchange data. Ignoring the mechanism that is used to exchange the data hardly seems like a good idea.

I have tested those radios before and they can talk to each other and communicate. The shields are xbee shields and those have worked before also. Its not a problem with the radios or anything physical i have had all of it working before. Its just code issues.

njkl44: Something like this?

No. Did my site have code like that there? Look,you have this:

   while((char)Serial.read() !='.');

   byte vals[5]; 
   vals[0] = Serial.read();
   Serial.read();
   vals[1] = Serial.read();
   Serial.read();
   vals[2] = Serial.read();
   Serial.read();
   vals[3] = Serial.read();
   Serial.read();
   vals[4] = Serial.read();

If there is no data Serial.date will return -1, so the "while" part will work OK. It will sit there until a dot arrives. Then, very quickly, you grab 5 more bytes, which since your processor is much faster than a serial port, will almost certainly be -1. So you have filled vals array with -1 (turned into 0xFF since vals is not an int).

Look at the code on the link I posted. First you just read and fill an array with data (using Serial.available() for every byte). Then when some delimiter comes, you go back and process it.

I've added some more examples to the link above.

In particular, this shows how fast you are reading at 9600 baud compared to how fast the data arrives:

See that line at the bottom? That is all 5 reads executing (in my test code). Even at a much faster baud rate, it can't even read a second byte:

[quote author=Nick Gammon link=topic=89212.msg669796#msg669796 date=1327782578]

njkl44: Something like this?

No. Did my site have code like that there? Look,you have this:

   while((char)Serial.read() !='.');

   byte vals[5]; 
   vals[0] = Serial.read();
   Serial.read();
   vals[1] = Serial.read();
   Serial.read();
   vals[2] = Serial.read();
   Serial.read();
   vals[3] = Serial.read();
   Serial.read();
   vals[4] = Serial.read();

If there is no data Serial.date will return -1, so the "while" part will work OK. It will sit there until a dot arrives. Then, very quickly, you grab 5 more bytes, which since your processor is much faster than a serial port, will almost certainly be -1. So you have filled vals array with -1 (turned into 0xFF since vals is not an int).

Look at the code on the link I posted. First you just read and fill an array with data (using Serial.available() for every byte). Then when some delimiter comes, you go back and process it. [/quote] Are you talking about something like this?

void loop()
{

   while((char)Serial.read() !='.');
   
   if (Serial.available() >0)
   {
   byte vals[5];
   vals[0] = Serial.read();
   Serial.read();
   vals[1] = Serial.read();
   Serial.read();
   vals[2] = Serial.read();
   Serial.read();
   vals[3] = Serial.read();
   Serial.read();
   vals[4] = Serial.read();
   }
   
    myservo1.write(vals[0]);
    myservo2.write(vals[1]);
    myservo3.write(vals[2]);
    myservo4.write(vals[2]);
    myservo5.write(vals[3]);
    myservo7.write(vals[4]);

njkl44: Are you talking about something like this?

Why do you keep ignoring what Nick Gammon tells you? You code looks nothing like what he is suggesting.

PeterH:

njkl44: Are you talking about something like this?

Why do you keep ignoring what Nick Gammon tells you? You code looks nothing like what he is suggesting.

I'm trying to understand what he's saying and learn. I need to start somewhere

I'm trying to understand what he's saying and learn.

He's telling you that you can't check to see that there is at least one character available to read, and then assume that that means you can read as many as you want. What's so hard to understand about that?

What do you suggest about doing it a different way? I need this to work for a few hours or so. I had code that worked before but after a few minutes it got all weird.

but after a few minutes it got all weird.

Because you are not checking that there IS something to read before EVERY read.

PaulS:

but after a few minutes it got all weird.

Because you are not checking that there IS something to read before EVERY read.

So something like this?

   if (Serial.available())
   vals[0] = Serial.read();
if (Serial.available())
   Serial.read();
if (Serial.available())
   vals[1] = Serial.read();