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.h>
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);
}
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.
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.
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 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.
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'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.