Go Down

Topic: Cant control servo angle via serial, why?? (Read 1 time) previous topic - next topic

creativen

I have bought a servo, and I would like to control its angle via serial.
here is my code
Code: [Select]
//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!

zoomkat

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: [Select]

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

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

creativen

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

creativen

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.

creativen


creativen

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: [Select]

import processing.serial.*;
Serial myPort;

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

}

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

xl97

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?


wildbill

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.

Go Up