Controlling multiple servos using a pot via xbee

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);
    }
  }
}

thanks

  if (Serial.available()) 
  {
    serv = Serial.read();

    Serial.print(int(serv));
    
     if ((int(serv) == 254)) 
     {
     byte ana1;
     ana1 = Serial.read();

You only know for sure one byte is available, but are reading at least two.

#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);
    }
  }
}

i guess this should work right

nope it does not work :cold_sweat:0
help please. To be honest i dont get the concept behind the linking of the pins, as in eg. pot in A0 controls servo on pin 12

could anybody help me with that please
thanks

Please re-read reply #1

okay i have a doubt now if i have to read values from two pots to control two servo separately do i need two bytes or no?

or are you trying to say that the code will be as follows

void loop() {
  if (Serial.available()) 
  {
    ana1 = Serial.read();
    Serial.print(ana1)
      myservo1.write(ana1);
    }
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);
    }
  }
}

i have one problem that is i do not know how to do the following in arduino 1.0

Serial.print(254, BYTE); //SYNC char
Serial.print(val2, BYTE);

as arduino 1.0 does not support BYTE anymore

but i do know i can use

Serial.write();

but i am not sure how to send 254 and val
can i write it as

Serial.write(val2, 254);

No, you cannot.
You can send "write" them as separate values, or you can put them in an array, and write out two elements of the array.

Sender

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.

thanks

What do you think this

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);

sends?
If you're in any doubt, have a look at the documentation for the Serial object.

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?

Change your sender to use:

Serial.print("<");
Serial.print(ana1);
Serial.print(",");
Serial.print(ana2);
Serial.print(">");

This will result in "<90,90>" being sent.

Then, code like this on the receiver:

#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.

sorry i lost you there

thanks a lot PaulS

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.

i am beginning to lose hope now, i just want this to work, i know i am missing something somewhere, i just dont know what and where it is :disappointed_relieved:

allen121:
i am beginning to lose hope now, i just want this to work, i know i am missing something somewhere, i just dont know what and where it is :disappointed_relieved:

I know the feeling :smiley: Like you're waiting for someone to just come along and make it work :smiley:

You seem to have about three separate threads going on the same subject - can't you glean some infor from them?

deathrow

i tried using it but 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

no use

i am trying my level best but i just cannot see what i am doing wrong, can somebody point it out. maybe i am just dumb