Pages: [1]   Go Down
Author Topic: arduino+servo+xbee  (Read 826 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46091
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was surprised you didn't mention it.
Logged

Pages: [1]   Go Up
Jump to: