I have this code:
#include <Servo.h>
Servo motor;
char readInf;
void setup(){
Serial.begin(9600);
motor.attach(8);
}
void loop(){
if(Serial.available() > 0) {
readInf = Serial.read();
motor.write(readInf);
delay(550);
motor.write(120);
}
} ;
From Android, I send how many degrees the Servo have to work, but when it's read, the value comes like 'char' and accept only int values. The question is: How can I set a String value to "motor.write()"? Or, is there another way to do it?
Thanks!
Very basic servo test code that you can try.
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.attach(9);
Serial.println("servo-test"); // 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
myservo.write(n);
readString="";
}
}
It's so much easier to read code if you use the # tag just above the smilies.
Without the code tags, some of the code gets interpreted as formatting so you get this:
Serial.begin(9600);
motor.attach(8);
}
..... not this:
Serial.begin(9600);
motor.attach(8);
}