Pages: [1] 2   Go Down
Author Topic: plz help me in servo problem  (Read 1197 times)
0 Members and 1 Guest are viewing this topic.
EGYPT
Offline Offline
Newbie
*
Karma: 0
Posts: 16
IAM EGYPTIAN
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Arduino i have tried this code of controlling servo motor by pot wirelessly by 2 arduinos & 2 Xbees the servos making random moves without any control with the pots , so what's the error in these codes :

Code:
Pots Code @ the First Arduino :

//Define Pins
int potPin = 0;
int potPin2 = 1;

void setup()
{
  //Create Serial Object (9600 Baud)
  Serial.begin(9600);
}

void loop()
{
  int val = map(analogRead(potPin), 0, 1023, 0, 179);
  int val2 = map(analogRead(potPin2), 0, 1023, 0, 179);
  Serial.println(val);
  Serial.println(val2);
  delay(50);
 
}

__________________________________________________________________

Motor Code @ the sec. Arduino :

#include <Servo.h>

//Define Pins
int servoPin = 9;
int servoPin2 = 10;

//Create Servo Object
Servo Servo1;
Servo Servo2;

void setup()
{
 //Start Serial
 Serial.begin(9600);
 
  //Attaches the Servo to our object
  Servo1.attach(servoPin);
  Servo2.attach(servoPin2);
 
  delay(500);
}

void loop()
{

  while( Serial.available() == 0);
  int data = Serial.read() -'0';

  int pos = map(data, 0, 1023, 0, 179);
  int pos2 = map(data, 0, 1023, 0, 179);
  pos = constrain(pos, 0, 179);
  pos2 = constrain(pos, 0, 179);

  //Turn the servo
  Serial.println(pos);
   Serial.println(pos2);
  Servo1.write(pos);
  Servo2.write(pos2);
  Serial.flush();
 
 
}

sincerely - amr
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your receiver takes a single decimal digit (when the transmitter is sending multiple digits) and maps each to a different range, and writes that to the servos. A millisecond or so later, a new digit arrives, you map that to the same range and send that to the servos.
What did you expect to happen?
« Last Edit: June 09, 2012, 05:24:52 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Code:
  int data = Serial.read() -'0';

  int pos = map(data, 0, 1023, 0, 179);
  int pos2 = map(data, 0, 1023, 0, 179);
  pos = constrain(pos, 0, 179);
  pos2 = constrain(pos, 0, 179);

  //Turn the servo
  Serial.println(pos);
   Serial.println(pos2);
  Servo1.write(pos);
  Servo2.write(pos2);
  Serial.flush();
You are sending two strings of characters, representing each servo position, separated by a carriage return and line feed. Then, you read a single character, pretend it is two values (when in reality data is an int in the range 0 to 9), and throw away the rest of the data on the serial port (pre-1.0) or block until all data has been sent (1.0 and on).

Regardless of which version of the IDE you are using, you do NOT need the serial flush statement. Get rid of it. You do need to read ALL the data sent, properly.
Logged

EGYPT
Offline Offline
Newbie
*
Karma: 0
Posts: 16
IAM EGYPTIAN
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok , how can i change the single decimal digit to 4 digits what about this modification ( int data = Serial.read() -'0000'; )

regards - amr
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Converting strings to decimal number comes up on this forum about once a week.
There are examples.

Quote
what about this modification ( int data = Serial.read() -'0000';
That will not work for more reasons than I have time to go into now.
« Last Edit: June 09, 2012, 05:42:48 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

EGYPT
Offline Offline
Newbie
*
Karma: 0
Posts: 16
IAM EGYPTIAN
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for reply but can u give me the link please , & what about this :   int data = Serial.read() -'4';

best
« Last Edit: June 09, 2012, 06:09:34 pm by amrhamed » Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Simple servo test code for two servos that converts a string into two numbers for servo position control.

Code:
// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  Serial.begin(9600);
  myservo1.attach(6);  //the pin for the servo control
  myservo2.attach(7);
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
    }
  }

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
     
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
     
      Serial.println(servo1);  //print to serial monitor to see parsed results
      Serial.println(servo2);

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      Serial.println(n2);
           
      myservo1.writeMicroseconds(n1); //set servo position
      myservo2.writeMicroseconds(n2);
    readString="";
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

EGYPT
Offline Offline
Newbie
*
Karma: 0
Posts: 16
IAM EGYPTIAN
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 @ zoomkat i can't understand , sorry but can u modify my code to understand what u need

regards
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 206
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
myservo2.attach(7);

I thought that the servo library only used outputs capable of PWM. Can you use any output?
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought that the servo library only used outputs capable of PWM. Can you use any output?

Seemed to work for me. It doesn't work for you?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
@ zoomkat i can't understand , sorry but can u modify my code to understand what u need

Sorry, no. I posted the code to show one way to capture a string from the serial port, parse the captured string into two seperate numeric strings (one for each servo), and convert the numeric string into a number to use to position a servo. You need to see if any of this is useful to you.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 206
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Seemed to work for me. It doesn't work for you?

Never tried it... made a rash assumption that because the sketch examples use pin 9 & 10 that  the servo library only used the PWM outputs to control the servos... DUH!
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Never tried it... made a rash assumption that because the sketch examples use pin 9 & 10 that  the servo library only used the PWM outputs to control the servos... DUH!

The pins available for servo use can also depend on what is going on and possible conflicts with other hardware being used.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

EGYPT
Offline Offline
Newbie
*
Karma: 0
Posts: 16
IAM EGYPTIAN
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

plz any one help me to correct my code , i am very newbie so , hardly i have written this code

thanks
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 308
Posts: 26465
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Zoomkat has given you code.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1] 2   Go Up
Jump to: