Hello,
I am a bit stuck here and was hoping someone may be able to help. I am trying to control 5 servos with 5 pots via bluetooth but i am having no luck!
i got this program working with 1 pot and servo so I know the bluetooth is working but I must be doing something wrong trying to scale it up.
Here's the code i'm trying;
////Master Code////
#define ledPin 9
int state = 0;
int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
int potValue = 0;
int potValue1 = 1;
int potValue2 = 2;
int potValue3 = 3;
int potValue4 = 4;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read(); // Reads the data from the serial port
state1 = Serial.read(); // Reads the data from the serial port
state2 = Serial.read(); // Reads the data from the serial port
state3 = Serial.read(); // Reads the data from the serial port
state4 = Serial.read(); // Reads the data from the serial port
}
// Reading the potentiometer
potValue = analogRead(A0);
potValue1 = analogRead(A1);
potValue2 = analogRead(A2);
potValue3 = analogRead(A3);
potValue4 = analogRead(A4);
int potValueMapped = map(potValue, 0, 1023, 0, 180);
Serial.write(potValueMapped); // Sends potValue to servo motor
delay(10);
int potValueMapped1 = map(potValue1, 0, 1023, 0, 180);
Serial.write(potValueMapped1); // Sends potValue to servo motor
delay(10);
int potValueMapped2 = map(potValue2, 0, 1023, 0, 180);
Serial.write(potValueMapped2); // Sends potValue to servo motor
delay(10);
int potValueMapped3 = map(potValue3, 0, 1023, 0, 180);
Serial.write(potValueMapped3); // Sends potValue to servo motor
delay(10);
int potValueMapped4 = map(potValue4, 0, 1023, 0, 180);
Serial.write(potValueMapped4); // Sends potValue to servo motor
delay(10);
}
///Slave Code////
#include <Servo.h>
#define button 8
Servo myServo;
int state = 20;
Servo myServo1;
int state1 = 20;
Servo myServo2;
int state2 = 20;
Servo myServo3;
int state3 = 20;
Servo myServo4;
int state4 = 20;
void setup() {
pinMode(button, INPUT);
myServo.attach(9);
myServo1.attach(10);
myServo2.attach(11);
myServo3.attach(12);
myServo4.attach(13);
Serial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
state = Serial.read();
state1 = Serial.read();
state2 = Serial.read();
state3 = Serial.read();
state4 = Serial.read();
// Reads the data from the serial port
}
// Controlling the servo motor
myServo.write(state);
delay(10);
myServo1.write(state1);
delay(10);
myServo2.write(state2);
delay(10);
myServo3.write(state3);
delay(10);
myServo4.write(state4);
delay(10);
}
Any help anyone could supply would be great,
Thanks!!!