Hello,
I want to control 5 servos with 5 sensors through 2 bluetooth modules (HC-05). I have good code for only one servo, cause if i want to control all servos and i use function "Serial.read();" it reads all signals to every single servo and its dancing. How can i send 5 signals to 5 servos, but 1 signal controls 1 servo not more. This is my MASTER CODE for one servo:
int flex=0;
void setup() {
Serial.begin(9600);
}
void loop() {
flex = analogRead(A0); // reads the value of the potentiometer (value between 0 and 1023)
int flexmap = map(flex, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
Serial.write(flexmap);
delay(10);
}
And this is my SLAVE CODE for one servo:
#include <Servo.h>
Servo kciuk; // create servo object to control a servo
int data=0;
void setup() {
kciuk.attach(5); // attaches the servo on pins to the servo object
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
data = Serial.read();
kciuk.write(data);
delay(10);
}
}
Can somebody help me to solve this problem? Thanks!
Yes, but if i will type
data = Serial.read();
servo.write(data);
and do this the same to other servos, i mean data1, data2, data3 etc. It's not working cause Serial.read() reads signal from all sensors in one moment.
Have You some example maybe what should i do?
My Arduino Software Solutions has a variety of sketches to read from Serial and parse the results.
My SafeString library (available from the library manager) has a high level SafeStringReader that tokenizes input and methods to reliably convert text to numbers.
Comma separate data is a common format
see this example of parsing comma separated GPS data
So you could use a format like
servoNo:steps,servoNo:steps
e.g. 1:10,2:30 etc
then tokenize by , and the for that substring tokenize by : to and then convert to ints to control the respective servo