Request help, Arduino and XBee and servo-motor

Hello everyone,

As part of a project for school, I'll make a hand and a robotic arm, the arm and hand are controlled with motion sensors. The sensors will be sewn on two gloves, and to control the other hand to the arm, I will use voltage dividers on the motion sensors, the value for the change in trying upon movement of the finger.

To order and connect the arm and hand gloves, I will use three XBee modules, to send and receive data using WiFi, and three Arduino for control and management of each signal.

I already do all the mechanical, and do the gloves, I still have to do the code, and that's my problem: /.

I made a code but not work when I just use a sensor to a must this works, but if I put it all together nothing function, hand works all alone, it moves randomly.

I apologize for my English, I use google translation because I'm french

here is the code for the hand, the arm is the same problem:

To send hand :

int position0 = 0;
int position1 = 0;
int position2 = 0;
int position3 = 0;
int position4 = 0;
 
 void setup() {
   Serial.begin(9600);
 }
 void loop() {
   
     // put your main code here, to run repeatedly:
 int flex0 = analogRead(A0);
 int flex1 = analogRead(A1);
 int flex2 = analogRead(A2);
 int flex3 = analogRead(A3);
 int flex4 = analogRead(A4);
 
 int position0 = map(flex0, 400, 700, 0, 255);
 Serial.write(position0); 
 delay(10);
 
 int position1= map(flex1, 410, 740, 0, 255);
 Serial.write(position1);
 delay(10);
 
 int position2 = map(flex2, 760, 420, 0, 255);
 Serial.write(position2);
 delay(10);
 
 int position3 = map(flex3, 700, 400, 0, 255);
 Serial.write(position3);
 delay(10);
 
 int position4 = map(flex4, 700, 400, 0, 255);
 Serial.write(position4);
 delay(10); 
}

Here is the one of the reception:

#include <Servo.h>
//Creation des l'objets de la classe servo
Servo MonServo0, MonServo1, MonServo2,MonServo3, MonServo4;

int doigtPin0 = 7;
int doigtPin1 = 6;
int doigtPin2 = 5;
int doigtPin3 = 4;
int doigtPin4 = 3;

 int flex0 = 0;
 int flex1 = 0;
 int flex2 = 0;
 int flex3 = 0;
 int flex4 = 0;

int position0 = 0;
int position1 = 0;
int position2 = 0;
int position3 = 0;
int position4 = 0;

 
void setup()  {
  Serial.begin(9600);
   Serial.println( "Serial Start" );
  
   // put your setup code here, to run once:
  MonServo0.attach(doigtPin0);
  MonServo1.attach(doigtPin1);
  MonServo2.attach(doigtPin2);
  MonServo3.attach(doigtPin3);
  MonServo4.attach(doigtPin4);
  
  MonServo0.write( position0 );
  MonServo1.write( position1 );
  MonServo2.write( position2 );
  MonServo3.write( position3 );
  MonServo4.write( position4 );
}
  
void loop()  {
  if (Serial.available() > 0) {
     
    flex0 = Serial.read(); 
 int position0 = map(flex0, 0, 255, -360, 180);
 MonServo0.write(position0);
 delay(10);
 
 flex1 = Serial.read();
 int position1= map(flex1, 0, 255, -360, 180);
 MonServo1.write(position1);
 delay(10);
 
  flex2 = Serial.read();
 int position2 = map(flex2, 0, 255, 0, 500);
 MonServo2.write(position2);
 delay(10);
 
 flex3 = Serial.read();
 int position3 = map(flex3, 0, 255, 0, 500);
 MonServo3.write(position3);
 delay(10);
 
  flex4 = Serial.read();
 int position4 = map(flex4,0, 255, 0, 500);
 MonServo4.write(position4);
 
delay(10);
         
  }
}

thank you for your help :wink:

Do you KNOW what Serial.write() does? Do you KNOW what kind of argument(s) it takes? If not, you need to hit the reference page and learn what it does and what arguments it takes. Here's a hint. It does not take ints.

Why do you have local variables and global variables OF THE SAME NAME in the sender code?

Why are you delaying between sending values?

  if (Serial.available() > 0) {
     
    flex0 = Serial.read(); 
 flex1 = Serial.read();
  flex2 = Serial.read();
 flex3 = Serial.read();
  flex4 = Serial.read();
}

If there is one byte to be read, it is NOT okay to read all 5 of them.

 int position0 = map(flex0, 0, 255, -360, 180);
 MonServo0.write(position0);

Are your servos really capable of moving to -360 degrees?

Hello, thank you for your help PaulS.

I do not really know how "Serial.write () Serial.read ()" it works, I watch a video on youtube, how it operated a servo motor with a potentiometer, and I did the rest by inference

For the variable declaration, There are two statements is a mistake on my by :confused:

For this code :

int position0 = map(flex0, 0, 255, -360, 180);
 MonServo0.write(position0);

I put -360 to 180 so that it starts at zero. Otherwise, if I put 0-180, does not start at zero.

For this code :

int position0 = map(flex0, 0, 255, 0, 500);
 MonServo0.write(position0);

I want to go from 180 to 0, but it does not work, so I used this method and it works, without really understanding the operation.

What is the steps I need to take to resolve my problem.

thank you :slight_smile:

You can't begin to make use of the data on the receiving end until you know that you are getting good data, so I'm forced to discount all your statement about what happens on the receiving end.

On the sender, the variables in the calls to Serial.write() need to be of type byte, not int.

On the sender, send all the values THEN delay(). Do NOT delay() between sending values.

On the receiver, wait for 5 bytes to be available, then read, and use, all 5 without any delay()s.

It works, I followed your instructions and everything works, there are some settings :slight_smile:

I do not understand why we should use bytes for Serial.write () ?.

And why should I use " if (Serial.available ()> 5) " and not zero ?

I do not understand why we should use bytes for Serial.write () ?.

Because it is the correct type. You want to send one byte (a value between 0 and 256), so you should use a one byte variable.

If you pass an int to write(), it has two choices. It could send both the high order byte and the low order byte. Or, it could ignore the high order byte, and send only the low order byte. Why should it have to choose, when you could send a one byte value, instead?

(It actually doesn't choose. It simply ignores the high order byte. But, it's better to not force that explicit conversion, which results in a warning that is suppressed.)

And why should I use " if (Serial.available ()> 5) " and not zero ?

That should be >= 5. Isn't it obvious? If one byte arrives, you can't read 5 bytes. If you need to read 5 bytes AS FAST AS POSSIBLE, do nothing about the serial data until there are 5 bytes to read. No delay()s and hoping that another byte arrives in time.

Thank you for your help, I understand how it works, the next step is to control the arm and hand together with three xbee arduino and three card aduino, i have xbee serie 1

hello,

I try to control two servo motors with arduino uno and three card three xbee.

I connect the two servos in a card with a arduino xbee S1.
A potentiometer in the second card with a xbee S1.
A potentiometer in the third with a xbee S1.

Is-it possible to use two xbee to send with two potentiometer, and the third to receive and control two servomotor ?

thank you
have a nice day

like this image

Is-it possible to use two xbee to send with two potentiometer, and the third to receive and control two servomotor ?

Yes, it is.

Is it possible that-you guide me or give me an exmple that I can apply

Is it possible that-you guide me or give me an exmple that I can apply

You send me all the hardware. I'll write the code, and send you a bill. You pay the bill, and I'll send the hardware back.

nah ty

I suggest you might step back and possibly use a different approach. Below is some servo/pot test code. You should be able to first connect the servos directly to the arduino to see if you can get the glove/servos operating as desired. Values are also sent to the arduino tx for debugging, and later sending to the tx xbee.

//zoomkat multi pot/servo test 3-23-13
//includes dead band for testing and limit servo hunting
//view output using the serial monitor
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h> 
Servo myservo1;  //declare servos
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;

int potpin1 = 0;  //analog input pin A0
int potpin2 = 1;
int potpin3 = 2;
int potpin4 = 3;
int potpin5 = 4;

int newval1, oldval1;  //pot input values
int newval2, oldval2;
int newval3, oldval3;
int newval4, oldval4;
int newval5, oldval5;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  Serial.println("testing multi pot servo");  
}

void loop()
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){ //dead band 
    myservo1.write(newval1); //position the servo
    Serial.print(newval1); //print the new value for testing 
    Serial.print("a,");
    oldval1=newval1; //set the current old value
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print(newval2);
    Serial.print("b,");
    oldval2=newval2;
  }

  newval3 = analogRead(potpin3);           
  newval3 = map(newval3, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval3 > (oldval3+2)){  
    myservo1.write(newval3);
    Serial.print(newval3);
    Serial.print("c,");
    oldval3=newval3;
  }

  newval4 = analogRead(potpin4);           
  newval4 = map(newval4, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval4 > (oldval4+2)){  
    myservo1.write(newval4);
    Serial.print(newval4);
    Serial.print("d,");
    oldval4=newval4;
  }

  newval5 = analogRead(potpin5);           
  newval5 = map(newval5, 0, 1023, 0, 179); 
  if (newval1 < (oldval5-2) || newval5 > (oldval5+2)){  
    myservo1.write(newval5);
    Serial.print(newval5);
    Serial.print("e,");
    oldval5=newval5;
  } 
  delay(50);  //to slow loop for testing, adjust as needed
}