Hi!
I was working on a Bluetooth controlled robotic arm, and I was developing the remote control app for Android.
In order to control the servos, the app was supposed to send a continuous stream of data to the Arduino on long press of buttons, but because I was having trouble with the continuous data stream, I tried to send one byte when I press, and one different byte when I release.
When I press, the servo moves, until I release, then it stops and stays at its current position.
Although I originally managed to program the servo with an imaginary continuous stream of data (replaced by push-buttons during the development), using 'while' loops, I'm having trouble with a single signal (no continuous data).
I've been working on this for hours, but now I think I really need some help...
Please.
Hugo.T
Never send a continuous stream of data. Send a character (or a message) when a button pressed and send another character when the button is released.
Post your code so we can see what you can see.
And please use the code button </>so your code looks like this
and is easy to copy to a text editor
You find the examples in Serial Input Basics useful.
...R
Here is the code for the 'while' loop, with the imaginary continuous stream:
#include <Servo.h>
Servo servo;
int angle;
int button = 2;
int truc = 3;
void setup(){
servo.attach(9);
pinMode(button, INPUT);
pinMode(truc, INPUT);
Serial.begin(9600);
}
void loop(){
servo.write(170);
while (digitalRead(button) == HIGH){
Serial.println(angle--);
servo.write(angle--);
delay(35);
}
while (digitalRead(truc) == HIGH){
Serial.println(angle++);
servo.write(angle++);
delay(35);
}
while (digitalRead(button) == LOW && digitalRead(truc) == LOW){
angle = servo.read();
Serial.println(angle);
servo.write(angle);
delay(35);
}
}
And here is the code for the unique byte/char on press and on release:
#include <Servo.h>
int val;
int angle;
int charsRead;
char buffer[10];
Servo servo;
void setup(){
Serial.begin(9600);
servo.attach(6);
}
void loop(){
servo.write(20);
while(Serial.available() > 0){
charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
buffer[charsRead] = '\0';
val = atoi(buffer);
Serial.println(val);
if(val == 4021){
angle = servo.read();
servo.write(angle++);
Serial.println(angle++);
}
else if (val == 4022){
angle = servo.read();
servo.write(angle);
Serial.println(angle);
}
}
}
Note that in this one, the servo moves in only one direction.
I suppose i should also use 'while' loops, but the servo never stops then, and with 'if' conditions, it only moves one degree...
I don't know what more to try...
Have a look at the examples in the link I gave earlier.
Separate the receiving of data from the control of the servos. When you receive the data save it to a variable.
Make the servo do whatever is necessary based on the value in the variable.
Don't use WHILE or FOR because they block the Arduino until they finish. Just rely on the normal repetition of loop().
Have a look at how the servo is controlled in Serial Input Basics
...R
OK thanks a lot I'm gonna work on that.
I've read the overall of your tutorial, It seems very complete and potentially useful to me. I'm gonna study it in depth to solve my problem.
Thanks again and I will consider the message in the bottom of your replies for the next problems I may have to solve...