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