Hi,
Can someone help me with code. I'm stuck in this situation of controlling 2 servo opposite way.
I'm controling it throu the bluetooth app. So its changing degrees in -+1 degree.
I'm trying to make robotic arm. So in this situation I'm trying that 2 servo work like 1 servo.
For example like "servoleft" and "servoright".
#include <SoftwareSerial.h>
#include <Servo.h>
Servo servo01;
Servo servo02;
SoftwareSerial Bluetooth(3, 2); // Arduino(RX, TX) - HC-05 Bluetooth (TX, RX)
int servo1Pos, servo2Pos; // current position
int servo1PPos, servo2PPos; // previous position
int servo01SP[50], servo02SP[50]; // for storing positions/steps
int speedDelay = 20;
int index = 0;
int b;
int c;
String dataIn = "";
void setup() {
servo01.attach(4);
servo02.attach(5);
Serial.begin(9600);
Bluetooth.begin(9600); // Default baud rate of the Bluetooth module
Bluetooth.setTimeout(1);
delay(20);
// Robot arm initial position
servo1PPos = 84;
servo2PPos = 80;
servo01.write(servo1PPos);
servo02.write(servo2PPos);
}
void loop() {
// Check for incoming data
if (Bluetooth.available() > 0) {
dataIn = Bluetooth.readString(); // Read the data as string
// If "Waist" slider has changed value - Move Servo 1 to position
if (dataIn.startsWith("s6")) {
String dataInS = dataIn.substring(2, dataIn.length());
servo1Pos = dataInS.toInt();
if (servo1PPos > servo1Pos) {
for ( int j = servo1PPos; j > servo1Pos; j -= 1) { // Run servo down
servo01.write(j);
delay(5);
servo02.write(0 + j);
delay(30); // defines the speed at which the servo rotates
}
}
// If previous position is smaller then current position
if (servo1PPos < servo1Pos) {
for ( int j = servo1PPos; j < servo1Pos; j += 1) { // Run servo up
servo01.write(j);
delay(5);
servo02.write(155 - j);
delay(30);
}
}
servo1PPos = servo1Pos; // set current position as previous position
}