Pages: [1]   Go Down
Author Topic: Cant control servo angle via serial, why??  (Read 650 times)
0 Members and 1 Guest are viewing this topic.
universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have bought a servo, and I would like to control its angle via serial.
here is my code
Code:
//Library for the Servo
#include <Servo.h>

Servo servo1;

void setup()
{
  Serial.begin (57600);
 servo1.attach(3);
 servo1.write(90);
}

void loop()
{
 while (Serial.available()>1)
 {int sugi = Serial.read();
 if(0<sugi<180)
 {
 servo1.write(sugi);
 }
 }
}

but when I type for example 50, then it will turn to the same certain angle if I type 80.
For the second type, it cause no respon to the servo anything I type, what is wrong here?

Thank you!
Logged

0
Offline Offline
Tesla Member
***
Karma: 114
Posts: 8905
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but when I type for example 50, then it will turn to the same certain angle if I type 80.
For the second type, it cause no respon to the servo anything I type, what is wrong here?

Your code only uses one character at a time. The last character of both 50 and 180 is 0.

Servo test code you can study.

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

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

wOW it works, I'll learn how the code works!!!
Gracias!
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How if in the case:
I would like to use serial to send string to control my servo, which is around 0-180 degrees, so I could only type 0-180 in serial monitor.
Is there any way to send any string to control my servo and also for example relay? what I need to type in the serial monitor?
I would like to use serial to communicate to two device, that is servo and relay, could I do that? How the serial could differentiate that this command for servo and the other one for the relay?

Thank you, sorry for my bad english.
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I GOT THE WAY OUT! YEAY!
Logged

universe
Offline Offline
Sr. Member
****
Karma: 0
Posts: 258
I'm enjoying my Life
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It works only if I give input via serial monitor but It does not work well if I use processing with serial library, what happen?

here is my processing code:
Code:
import processing.serial.*;
Serial myPort;

void setup()
{
myPort = new Serial (this, "COM106", 57600);

}

void draw()
{
 myPort.write(500);
}
Logged

0
Offline Offline
Edison Member
*
Karma: 7
Posts: 1221
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

before the processing part.. how did you 'fix' your initial problem.. you didnt explain?


I was always sunder the impression (and of course I could be wrong!)  LOL....that  if you wanted to send an uknown amount of data (ie: more than 1 byte)...  it was best practice to put in a 'start & stop delimiting character?

most commonly I have seen used would be <  and > brackets.. signaling to the 'code' when a new packet is here/starting... and when that 'packet' of data is done...?

Is this how you 'solved' your initial problem?

Logged


New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3400
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've never used processing, but since you're expecting to build a string on the arduino, how about using "500" instead of 500 in the call to myPort.write.
Logged

Pages: [1]   Go Up
Jump to: