I have successfully managed to write down something that get to read values from two potentiometers, but I'am still facing problems with the slave part. Can't figure out the problem.
Any Help please.
// == MASTER CODE ==
int potValuePins[] = { A0, A1 };
void setup() {
Serial.begin(9600); // Default communication rate of the Bluetooth module
for (auto pin : potValuePins) {
pinMode(pin, INPUT);
}
}
void loop() {
for (auto pin : potValuePins) {
Serial.write(pin); // Send the pin number
Serial.write(analogRead(pin)); // Send the pin value with a trailing newline as a delimiter
int value=analogRead(pin);
Serial.print(" Pin is: ");
Serial.print(pin);
Serial.print("\t");
Serial.print("Value is: ");
Serial.println(value);
}
}
// == SLAVE CODE ==
#include <Servo.h>
Servo myServo1;
Servo myServo2;
void setup() {
Serial.begin(9600);
myServo1.attach(9);
myServo2.attach(10);
}
void loop() {
if (Serial.available()) {
char input[3];
Serial.readBytes(input, 2);
int pin = input[0];
int angle = input[1];
Serial.print("the pin:");
Serial.print(pin);
Serial.print("\t");
Serial.print("the angle:");
Serial.println( angle);
//Serial.readStringUntil('\n'+ input[1]);
switch (pin) {
case A0:
myServo1.write(angle);
break;
case A1:
myServo2.write(angle);
break;
default:
Serial.println("Unknown value");
break;
}
}
}