hi i am trying to control two servos using a pot via xbee could somebody help me, i have the codes for both the sender and receiver but i just cannot get it to work , i am using arduino uno R3 (it needs arduino 1.0)
below i am posting the code for both sender and receiver
sender code
void setup()
{
Serial.begin(9600); // used to be 19200
}
void loop()
{
int ana1 = analogRead(1);
delay(10);
int ana2 = analogRead(2); // read the analog values in
delay(10);
int val1 = map(ana1, 0, 1023, 0, 179);
int val2 = map(ana2, 0, 1023, 0, 179); // assign maped values to val1 & val2
Serial.print(ana1);
Serial.print(ana2);
delay (5);
}
receiver code
#include <Servo.h>
byte serv, ana1, ana2;
Servo myservo1;
Servo myservo2;
void setup()
{
myservo1.attach(12);
myservo2.attach(13);
Serial.begin(9600); // used to be 19200
}
void loop() {
if (Serial.available())
{
serv = Serial.read();
Serial.print(int(serv));
if ((int(serv) == 254))
{
byte ana1;
ana1 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana1)); // To check if data is received
myservo1.write(ana1);
}
if ((int(serv) == 253))
{
byte ana2;
ana2 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana2)); // To check if data is received
myservo2.write(ana2);
}
}
}
#include <Servo.h>
byte serv1, serv2, ana1, ana2;
Servo myservo1;
Servo myservo2;
void setup()
{
myservo1.attach(12);
myservo2.attach(13);
Serial.begin(9600); // used to be 19200
}
void loop() {
if (Serial.available())
{
serv1 = Serial.read();
Serial.print(int(serv1));
if ((int(serv1) == 254))
{
byte ana1;
ana1 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana1)); // To check if data is received
myservo1.write(ana1);
}
if ((int(serv2) == 253))
{
byte ana2;
ana2 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana2)); // To check if data is received
myservo2.write(ana2);
}
}
}
void loop() {
if (Serial.available())
{
if (ana1 == 254)
{
byte ana1;
ana1 = Serial.read();
myservo1.write(ana1);
}
if (ana2 == 253)
{
byte ana2;
ana2 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana2)); // To check if data is received
myservo2.write(ana2);
}
}
}
i tried the above code also but does not work. Somehow this does not make sense to me, can somebody please guide me i am new to coding
The first thing to remember is that serial communications are really slow.
Just because you've called "Serial.available ()" and it has returned 1 and you've read that character, you cannot assume that there is another one to read immediately.
(uncompiled, untested)
#include <Servo.h>
Servo myservo1;
Servo myservo2;
void setup()
{
myservo1.attach(12);
myservo2.attach(13);
Serial.begin(9600); // used to be 19200
}
void loop()
{
if (Serial.available())
{
int serv = Serial.read();
Serial.print(int(serv));
if ((int(serv) == 254))
{
while(Serial.available() == 0)
{}
byte ana1 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana1)); // To check if data is received
myservo1.write(ana1);
}
if ((int(serv) == 253))
{
while(Serial.available() == 0)
{}
byte ana2 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana2)); // To check if data is received
myservo2.write(ana2);
}
}
}
void setup()
{
Serial.begin(9600); // used to be 19200
}
void loop()
{
int ana1 = analogRead(1);
delay(10);
int ana2 = analogRead(2); // read the analog values in
delay(10);
int val1 = map(ana1, 0, 1023, 0, 179);
int val2 = map(ana2, 0, 1023, 0, 179); // assign maped values to val1 & val2
Serial.print(253);
Serial.print(ana1);
Serial.print(254);
Serial.print(ana2);
delay (5);
}
#include <Servo.h>
Servo myservo1;
Servo myservo2;
void setup()
{
myservo1.attach(12);
myservo2.attach(13);
Serial.begin(9600); // used to be 19200
}
void loop()
{
if (Serial.available())
{
int serv = Serial.read();
Serial.print(int(serv));
if ((int(serv) == 254))
{
while(Serial.available() == 0)
{}
byte ana1 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana1)); // To check if data is received
myservo1.write(ana1);
}
if ((int(serv) == 253))
{
while(Serial.available() == 0)
{}
byte ana2 = Serial.read();
// Serial.println(int(serv)); // To check if data is received
// Serial.println(int(ana2)); // To check if data is received
myservo2.write(ana2);
}
}
}
even this does not work,
when i compile the receiver code it shows no error but after uploading the code to the arduino the orange LED glows in a dim manner. i suspect that i am sending more data.
int ana1 = analogRead(1);
delay(10);
int ana2 = analogRead(2); // read the analog values in
delay(10);
int val1 = map(ana1, 0, 1023, 0, 179);
int val2 = map(ana2, 0, 1023, 0, 179); // assign maped values to val1 & val2
Serial.print(253);
Serial.print(ana1);
Serial.print(254);
Serial.print(ana2);
Suppose that the pots are both set to about the middle, so that ana1 and ana2 are both 512. The map() calls will result in val1 and val2 being 90.
So, what you are sending to the serial port is "2539025490". How do you expect to parse that?
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Where it says "Process the packet", use strtok() and atoi() to extract the two position tokens and convert them to numeric values that you then write to the servos.
i am a newbie to programming but from what i understood the codes change as below i tried uploading these but it still does not work, and the LED is still dim.
Sender
void setup()
{
Serial.begin(57600); // used to be 19200
}
void loop()
{
int ana1 = analogRead(1);
delay(10);
int ana2 = analogRead(2); // read the analog values in
delay(10);
int val1 = map(ana1, 0, 1023, 0, 179);
int val2 = map(ana2, 0, 1023, 0, 179); // assign maped values to val1 & val2
Serial.print("<");
Serial.print(ana1);
Serial.print(",");
Serial.print(ana2);
Serial.print(">");
delay (5);
}
receiver end
#define SOP '<'
#define EOP '>'
#include <Servo.h>
Servo myservo1;
Servo myservo2;
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
myservo1.attach(12);
myservo2.attach(13);
Serial.begin(57600); // used to be 19200
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Where it says "Process the packet", use strtok() and atoi() to extract the two position tokens and convert them to numeric values that you then write to the servos.
You can also try the EasyTransfer library. It's pretty straight forward and I've been successful with it doing the same thing that you're trying to do now.