One way to improve matters would be to send a prefix character to indicate which servo the following number is for. A, B and C would do. On the receiver, look for one of those three chars and use it to decide which servo to move in accordance with what comes next.
zoomkat can i use the same code i have for the sender ?
I don't understand your code. The code I previously posted would be modified and used on the sending side. Below is code that might be used on the receiving end. The sending code would be modified to match the receiving format of the receiving code (the servo position number, the servo identifying character, and a delimiting comma, like 1500c,). The code I've posted can be used with the serial monitor for testing.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
i am sorry zoomkat i tried to understand your code but unable to could you help me please
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
i dont understand the above part
i tried using this but, it when i compile i am getting the error
For the sender part: ' easy transfer' does not name a type.
i tried downloading easy transfer from
#include <EasyTransfer.h>
EasyTransfer ET;
int potpin1 = 0;
int potpin2 = 1;
struct SEND_DATA_STRUCTURE{
int servo1val;
int servo2val;
};
SEND_DATA_STRUCTURE txdata;
void setup(){
Serial.begin(115200);
ET.begin(details(txdata), &Serial);
pinMode(potpin1, INPUT);
pinMode(potpin2, INPUT);
}
void loop(){
txdata.servo1val = analogRead(potpin1);
txdata.servo2val = analogRead(potpin2);
ET.sendData();
}
For the receiving side:
#include <Servo.h>
#include <EasyTransfer.h>
EasyTransfer ET;
Servo myservo1;
Servo myservo2;
struct RECEIVE_DATA_STRUCTURE{
int servo1val;
int servo2val;
};
RECEIVE_DATA_STRUCTURE txdata;
void setup(){
Serial.begin(115200);
ET.begin(details(txdata), &Serial);
myservo1.attach(9);
myservo2.attach(10);
}
void loop(){
if(ET.receiveData()){
myservo1.write(map(txdata.servo1val, 0, 1023, 0, 179));
myservo2.write(map(txdata.servo2val, 0, 1023, 0, 179));
}
}
I tried the code but nothing happens :S
Can you guys please tell me what I'm doing wrong? Thanks in advance
[/quote]
I tried the code but nothing happens :S
Can you guys please tell me what I'm doing wrong? Thanks in advance
I wrote in one of my earlier comments some of the changes I made to both the sending and receiving sides. Make those changes and the code should work flawlessly. Also, make sure your XBees are communicating with each other.
I'll reply here just to make it more convenient.
Did you extract and place the EasyTransfer folder in the Library folder of Arduino?
yes i did
@Allen121, can you please just stick to one thread?
Summarise where you are, but don't hijack other people's threads.
OP: Now that you can send and receive, it's time to learn how to send and receive strings, so you can send a value like 1023, or to send and receive bytes, as binary data, so you can map the pot value to a position and Serial.write() that byte.
Thanks for the suggestion, Paul! I will try to do that soon. But, can't this done with the EasyTransfer library using a different data type?
deathrow:
It finally works!I did the modifications, and also re-programmed my XBees.
Thanks for the help!
I have been following this post and I used deathrow's posted code and made the modifications needed. I get 2 servos working off 2 pots.
Im using 2 Arduino Megas, 2 Xbee S1, 2 pots and 2 servos.
here is the code.
Sending code
#include <EasyTransfer.h>
EasyTransfer ET;
int potpin1 = 0;
int potpin2 = 1;
struct SEND_DATA_STRUCTURE{
int servo1val;
int servo2val;
};
SEND_DATA_STRUCTURE txdata;
void setup(){
//Serial.begin(115200);
Serial.begin(9600);
ET.begin(details(txdata), &Serial);
//pinMode(potpin1, INPUT);
//pinMode(potpin2, INPUT);
}
void loop(){
/*
txdata.servo1val = analogRead(potpin1);
txdata.servo2val = analogRead(potpin2);
*/
txdata.servo1val = analogRead(potpin1);
txdata.servo2val = analogRead(potpin2);
txdata.servo1val = map(txdata.servo1val, 0, 1023, 0, 179);
txdata.servo2val = map(txdata.servo2val, 0, 1023, 0, 179);
//========================================
/*
Serial.print("txdata.servo1val =");
Serial.print(txdata.servo1val);
Serial.print(" ");
Serial.print("txdata.servo2val =");
Serial.print(txdata.servo2val);
Serial.println();
*/
//======================================
ET.sendData();
}
Receive code
#include <Servo.h>
#include <EasyTransfer.h>
EasyTransfer ET;
Servo myservo1;
Servo myservo2;
struct RECEIVE_DATA_STRUCTURE{
int servo1val;
int servo2val;
};
RECEIVE_DATA_STRUCTURE txdata;
void setup(){
//Serial.begin(115200);
Serial.begin(9600);
ET.begin(details(txdata), &Serial);
myservo1.attach(9);
myservo2.attach(10);
}
void loop(){
if(ET.receiveData()){
myservo1.write(txdata.servo1val);
myservo2.write(txdata.servo2val);
}
}
Will these solutions allow the operation of both servos simultaneously? Could you use similar code to operate four servos simultaneously using four pots?
// tx code working ok independetly 3 servos contros by 3 potentiometer
int potPin_0 = 0;
int potPin_1 = 1;
int potPin_2 = 2;
void setup()
{
//Create Serial Object (9600 Baud)
Serial.begin(9600);
}
void loop()
{
int val_0 = map(analogRead(potPin_0), 0, 1023, 0, 9);
Serial.print(val_0);
delay(50);
int val_1 = map(analogRead(potPin_1), 0, 1023, 0, 9);
Serial.print(val_1);
delay(50);
int val_2 = map(analogRead(potPin_2), 0, 1023, 0, 9);
Serial.print(val_2);
delay(50);
}
// rx code
//receiver end
#include <Servo.h>
//Define Pins
int servoPin_0 = 9;
int servoPin_1 = 3;
int servoPin_2 = 5;
//Create Servo Object
Servo orangeServo;
Servo wristServo;
Servo gripperServo;
void setup()
{
//Start Serial
Serial.begin(9600);
//Attaches the Servo to our object
orangeServo.attach(servoPin_0);
wristServo.attach(servoPin_1);
gripperServo.attach(servoPin_2);
delay(500);
}
void loop()
{
while( Serial.available() == 0);
int data_0 = Serial.read() -'0';
int pos_0 = map(data_0, 0, 9, 0, 180);
pos_0 = constrain(pos_0, 0, 180);
//Turn the servo
Serial.print(pos_0);
orangeServo.write(pos_0);
while(Serial.available()>0);
Serial.read();
while( Serial.available() == 0);
int data_1 = Serial.read() -'1';
int pos_1 = map(data_1, 0, 9, 0, 180);
pos_1 = constrain(pos_1, 0, 180);
//Turn the servo
Serial.print(pos_1);
wristServo.write(pos_1);
while(Serial.available()>0);
Serial.read();
while( Serial.available() == 0);
int data_2 = Serial.read() -'2';
int pos_2 = map(data_2, 0, 9, 0, 180);
pos_2 = constrain(pos_2, 0, 180);
//Turn the servo
Serial.print(pos_2);
gripperServo.write(pos_2);
while(Serial.available()>0);
Serial.read();
}
rx.ino (1.22 KB)