Go Down

Topic: arduino+servo+xbee (Read 890 times) previous topic - next topic

eman_elassas

Hey everyone,

I'm new to Arduino and xbee world, and trying to control my servo by potentiometer over 2xbee.
i've this components:
- arduino uno
- arduino mega
- xbee s1 x2
-arduino xbee shield x2
- xbee usb adapter x1

i write this code for transmitter:


int potpin = 0;
int val;
void setup()
{
Serial.begin(9600);

}
void loop()
{
  val = analogRead(potpin);
    val = map(val, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
val = Serial.print(val);

}

receiver code:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo


void setup()
{
    Serial.begin(9600);      //set baud rate on Arduino to 9600

  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
   if (Serial.available()) {   //If there is data in the Serial Line
    int val1 = Serial.read(); //save data into integer variable dataByte
   
  myservo.write(val1);                  // sets the servo position according to the scaled value
}
}

but it doesn't work....has any one gotten an idea about this problem?

thank you in advance:)

PaulS

You are probably the 10th person this month alone that thinks that sending '1', '5', '0' to the serial port will result in one call to Serial.read() returning 150.

You have two choices. You can make the sender send binary data so that the receiver gets one byte per position, or you can make the receiver receive strings of characters, and recreate the integer value.

Both approaches are well documented.

eman_elassas

transmitter send char

void loop()
{
  val = analogRead(potpin);
    val = map(val, 0, 1023, 0, 180);  // scale it to use it with the servo (value between 0 and 180)
   Serial.println(val,BYTE);


}

so, how can i make the receiver receive strings of characters, and recreate the integer value?
i'm sorry if my question is super simple :)

PaulS

With the highlighted code, the sender no longer sends a string of characters. Although you'd be better off using Serial.write(), instead.

eman_elassas

it doesn't work well ...is there is tutorial or code can help me,plz?


PaulS

Quote
it doesn't work well

Well, that's certainly enough information to go on. Not!

Quote
is there is tutorial

One or two somewhere on the internet.

Quote
or code can help me

Sure is.

What help do you need?

eman_elassas

well, i send binary data

int potpin = 0;
char val;
void setup()
{
Serial.begin(9600);

}
void loop()
{
  val = analogRead(potpin);
  int val2= String(val,BIN).length();
    val2 = map(val2, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
Serial.write(val2);

}

what i need to change in the receiver code

#include <Servo.h>

Servo myservo;  // create servo object to control a servo


void setup()
{
    Serial.begin(9600);      //set baud rate on Arduino to 9600

  myservo.attach(3);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
   if (Serial.available()) {   //If there is data in the Serial Line
    int val1 = Serial.read(); //save data into integer variable dataByte

   
  myservo.write(val1);                  // sets the servo position according to the scaled value
}
}


PaulS

Quote
well, i send binary data

ok, fine.

Quote
what i need to change in the receiver code

the atrocious indenting. the useless blank lines. nothing else.

Capital letters are a good thing, you know?

dxw00d

Code: [Select]
  val = analogRead(potpin);
  int val2= String(val,BIN).length();
  val2 = map(val2, 0, 1023, 0, 179);  // scale it to use it with the servo (value between 0 and 180)
  Serial.write(val2);

Eh? You convert the value you got from analogRead() to a String, in binary format, then take the length of the String, scale it and send that value to the receiver.

PaulS

Quote
You convert the value you got from analogRead() to a String, in binary format, then take the length of the String, scale it and send that value to the receiver.

Sh*t! Glossed right over that.

dxw00d

I was surprised you didn't mention it.

Go Up