Hi guys.
i have trouble with controling my servo via bluetooth. it seems that arduino and android can not come to an agreement. i wanna show you my both code, and video with the relation of action.
slider is not working fluently, can you tell me how to fix it and where is mistake ?
and...
what exactly defines Serial1.setTimeout(100); ?
PaulS it's a good point but not enough for me cause idk how to solve it and add a 'space'.
Robin2 your answers are always helpful
i find your example a bit hard for me to understand but will try.
plz tell my its good idea to send servo position as text? if not which data type should i use ?
in your example in arduino code you are using byte type so it's necessary to send it from android as byte ?
harry37:
i find your example a bit hard for me to understand but will try.
Ask questions if you wish.
plz tell my its good idea to send servo position as text? if not which data type should i use ?
in your example in arduino code you are using byte type so it's necessary to send it from android as byte ?
You can send the data any way you like as long as the receiving code figures it out properly. Which piece of which example are you referring to?
Sending data as Ascii characters makes debugging easier because you can probably send it with the Serial Monitor and you can print what is received to verify receipt.
Sending binary values requires less data to be transmitted and should reduce the amount of processing the Arduino has to do. In many (most?) cases that doesn't matter.
all about servo in your code is in byte data type.
i don't know how to sent it as byte (error in app inventor). so... i have to try to recive it as text...
correct me if i am wrong but i think that i should use "readSingleChar() reads and saves a single character and its byte value" to 'help' arduino understand recived text ?
Below is some simple servo test code you can try with the serial monitor. The bottom code uses a comma data packet delimiter which would need to be included with the servo position.
//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="";
}
}
//zoomkat 3-5-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
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-delomit-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like 700, or 1500, or 2000,
//or like 30, or 90, or 180,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >0) {
Serial.println(readString); //prints string to serial port out
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);
}
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
That part of the demo is not really relevant to your situation. I only wrote it like that to show how to convert incoming data to a float. I would not normally control a servo by sending fractions.
In any case, in that demo, all of the data is transmitted as Ascii characters and converted to other types when the Arduino receives it.
If you want to send an angle for your servo (or a number of millisseconds) you can send it as Ascii and convert it using atoi(). There is also an example of atoi() in the demo.
I'm not sure from your comments whether you actually want to send the data as Ascii or binary.
If you really want to send the data in binary format you might look at the more complex example at the start of that Thread or at this demo.