Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: control angle of servo motor  (Read 1204 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hye..I have two Xbees and two Arduinos coded to control angle of servo motor.  How could I code both of them . I think we need a sender and receiver code. On the receiver ,I'm already have the receiver code, but I have no idea for sender code. I'm a total beginner. Thanks!

Here is my receiver code.



#include <Servo.h>
 
Servo myservo;
 
int pos = 0;   
 
void setup()
{
  Serial.begin(9600);
  myservo.attach(9); 
}
 
 
void loop()
{
 
   while (Serial.available() > 0) {

    pos = Serial.parseInt();

   }
   myservo.write(pos);
   delay (100);

     
}
Logged

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

Code:
delay (100);
In 1/10th of a second, your input serial buffer could easily overflow, and you'd never know.
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.

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

I want to create a wireless servo controller to control angle of servo motor . Now the problem is I don't know how to write the code:(

The above code that I posted is "receiver code".but could you please tell me how to write the (transmitter/sender) code for control the angle of servo?

actually I have just tried  by myself to write the transmitter/sender code,but it doesn't work(something wrong with the code)...any help I will appreciate it.



int pos = 0;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available() > 0) {
    pos = Serial.parseInt();
   }
  Serial.print(pos);
  delay(10);
}
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

At the moment your problem seems to be the communication, so I suggest you ignore the servo for now and just concentrate on getting communication working between two XBees. I suggest that Google is your friend here.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

Quote
I have just tried  by myself to write the transmitter/sender code,but it doesn't work(
...so you
posted the receiver code, again.
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: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
  while (Serial.available() > 0) {
    pos = Serial.parseInt();
   }
  Serial.print(pos);
  delay(10);
}
Don't you want to write the position to the servo, rather than the serial port? The sender knows what it sent. That dumb delay() is still there.
Logged

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

I configured my XBees using X-CTU and they've been working fine.Now i just don't know how to write the code.

here i'm using this simple code for receiver code.How to write the sender code?  smiley-sad

#include <Servo.h>
 
Servo myservo;
 
int pos = 0;   
 
void setup()
{
  Serial.begin(9600);
  myservo.attach(9); 
}
 
 
void loop()
{
 
   while (Serial.available() > 0) {

    pos = Serial.parseInt();

   }
   myservo.write(pos);
   delay (10);

     
}
Logged

0
Offline Offline
Tesla Member
***
Karma: 151
Posts: 9853
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You probably could use the below test code for both sending and receiving. For testing, skip the xbees, and connect the sending arduino tx pin to the receiving arduino rx pin (and connect both arduino grounds together). Send a servo position command to the sending arduio via the serial monitor, and it will echo the command to the receiving arduino via the tx/rx connection.

Code:
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

Logged

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

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

sorry can i know what do you mean by " connect the sending arduino tx pin to the receiving arduino rx pin"?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry can i know what do you mean by " connect the sending arduino tx pin to the receiving arduino rx pin"?

Which part don't you understand? (I'm not being facetious - presumably you do understand some of the terms.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

here..alike code as above.. but mine is used to debug my servo.. i used PWMServo lib bcoz Servo.h seems like having conflict with SoftwareSerial.h.. i used softwareserial for my GPS module before this

Code:
/*
  Servo debugger by Heiswayi Nrird
*/
#include <PWMServo.h> // library Servo.h conflicts with SoftwareSerial.h
PWMServo servo; // SERVO_PIN_A = pin 9 | SERVO_PIN_B = pin 10

char junk = ' ';
int angle;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Enter value of angle (0 - 180) to drive the servo");
  Serial.println();
  Serial.flush();
  servo.attach(SERVO_PIN_A); // pin 9
  servo.write(90);
}

void loop()
{
  while (Serial.available() == 0) ;  // Wait here until input buffer has a character
  {
    angle = Serial.parseInt();        // new command in 1.0 forward
    if (angle < 0 || angle > 180) { Serial.println("From 0 to 180 only!"); }
    else {
      Serial.print("Angle = "); Serial.println(angle); showDir(angle);
      servo.write(angle);

      while (Serial.available() > 0) { junk = Serial.read() ; }      // clear the keyboard buffer
    }
  }
}

void showDir(int a)
{
  if (a < 90) { Serial.print(" (Left)"); }
  else { Serial.print(" (Right)"); }
}
Logged

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

hye PeterH..im already test the coding as above and it work very well..after that, can i using the same testing for xbees??
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hye PeterH..im already test the coding as above and it work very well..after that, can i using the same testing for xbees??

Don't see why not. You might need to configure / initialise the Xbees, and you'd need to connect the Arduino's serial I/O to the XBee on each side instead of connecting wires between them, but fundamentally the XBee pair gives you a serial channel which is functionally equivalent to the wired connected between Rx/Tx pins.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: