Hey everyone, at the moment I'm working on a project which includes HC-05 module and a servo.
The goal is to send(using a phone) a number of degrees(0-180) that the servo must turn.
#include<Servo.h>
Servo servo;
String servoDegrees;
char number;
void setup() {
servo.attach(8);
Serial.begin(9600);
servo.write(0);
}
void GetTheNumberAndRotate(){
if(Serial.available()){
while(Serial.available()>0){
number=Serial.read();
servoDegrees+=number;
}
Serial.println(servoDegrees);
servo.write(servoDegrees.toInt());
}
}
void loop() {
GetTheNumberAndRotate();
}
The problem I'm facing is that I can't properly read from the Serial. It seems like the while loop is running only once, no matter the number I put in. As I've read in the internet the method Serial.available() should return an interger which indicates the number of characters that have been inputted(IF that's wrong, please, correct me), so if I input the number 123 the while loop should be repeated 3 times(Right?)
Because of this problem I cannot rotate the servo more than once, because if I do the 2 numbers joins together(E.G. 1st-123 and 2nd-78, the output of the 2nd is 12378)
I could clean the "servoDegrees" before inputing a new number. But the thing is that in order to get the first number the "void loop" has tu run 3 times(if the number is made out of 3 digits).
All in all I think what I need the most is to properly understand how Serial.available() and Serial.read() works
If anyone could help I'd be grateful.
Also if I forgot to mention something please tell me, I'll try to be as useful as posiblle