I tried the ardunio code I wrote in proteus, the code works correctly in simulation, but in real life my servos are constantly spinning. I could not find a logical error, I would appreciate your help.
#include <SoftwareSerial.h>
#include <AccelStepper.h>
#include <Servo.h>
SoftwareSerial Bluetooth(A8, 38); // bluetooth bağlantı yerleri
AccelStepper LeftBackWheel(1, 42, 43); // sağ arka motor
AccelStepper LeftFrontWheel(1, 40, 41); // sol ön motor
AccelStepper RightBackWheel(1, 44, 45); // sağ arka motor
AccelStepper RightFrontWheel(1, 46, 47); // sağ ön motor
Servo servos[6];//servolar
int dereceler[6] = {30, 70, 90, 150, 120, 180};//servo dereceleri sırasıyla
int tekerhizi = 1500;
int lbw[50], lfw[50], rbw[50], rfw[50];
int speedDelay = 20;
int index = 0;
int dataIn;
int m = 0;
int zaman1, zaman2, zaman3, zaman4, zaman5, zaman6;
void setup() {
LeftFrontWheel.setMaxSpeed(3000);
LeftBackWheel.setMaxSpeed(3000);
RightFrontWheel.setMaxSpeed(3000);
RightBackWheel.setMaxSpeed(3000);
for (int i = 0; i < 6; i++) {
servos[i].attach(i + 8,400,2400);
}
for (int i = 0; i < 6; i++) {
int derece = dereceler[i];
int zaman = round(derece*(200/18.0))+400;
if (i == 0) {
zaman1 = zaman;
servos[0].writeMicroseconds(zaman1);
} else if (i == 1) {
zaman2 = zaman;
servos[1].writeMicroseconds(zaman2);
} else if (i == 2) {
zaman3 = zaman;
servos[2].writeMicroseconds(zaman3);
} else if (i == 3) {
zaman4 = zaman;
servos[3].writeMicroseconds(zaman4);
} else if (i == 4) {
zaman5 = zaman;
servos[4].writeMicroseconds(zaman5);
} else if (i == 5) {
zaman6 = zaman;
servos[5].writeMicroseconds(zaman6);
}
}
Bluetooth.begin(9600);
Bluetooth.setTimeout(5);
delay(20);
Serial.begin(9600);
}
void loop() {
if (Bluetooth.available() > 0) {
dataIn = Bluetooth.read();
if (dataIn >= 0 && dataIn <= 27) {
m = dataIn;
}
if (m == 4) {
moveSidewaysLeft();
} else if (m == 5) {
moveSidewaysRight();
} else if (m == 2) {
moveForward();
} else if (m == 7) {
moveBackward();
} else if (m == 3) {
moveRightForward();
} else if (m == 1) {
moveLeftForward();
} else if (m == 8) {
moveRightBackward();
} else if (m == 6) {
moveLeftBackward();
} else if (m == 9) {
rotateLeft();
} else if (m == 10) {
rotateRight();
}
if (m == 0) {
stopMoving();
}
if (30 < dataIn && dataIn < 100) {
tekerhizi = dataIn * 20;
}
if (m >= 16 && m <= 27) {
if (Bluetooth.available() > 0) {
m = Bluetooth.read();
}
switch (m) {
case 16:
zaman1 = zaman1-11.1111;
servos[0].writeMicroseconds(zaman1);
break;
case 17:
zaman1 = zaman1+11.1111;
servos[0].writeMicroseconds(zaman1);
break;
case 18:
zaman2 = zaman2-11.1111;
servos[1].writeMicroseconds(zaman2);
break;
case 19:
zaman2 = zaman2+11.1111;
servos[1].writeMicroseconds(zaman2);
break;
case 20:
zaman3 = zaman3-11.1111;
servos[2].writeMicroseconds(zaman3);
break;
case 21:
zaman3 = zaman3+11.1111;
servos[2].writeMicroseconds(zaman3);
break;
case 22:
zaman4 = zaman4-11.1111;
servos[3].writeMicroseconds(zaman4);
break;
case 23:
zaman4 = zaman4+11.1111;
servos[3].writeMicroseconds(zaman4);
break;
case 24:
zaman5 = zaman5-11.1111;
servos[4].writeMicroseconds(zaman5);
m = 0;
break;
case 25:
zaman5 = zaman5+11.1111;
servos[4].writeMicroseconds(zaman5);
break;
case 26:
zaman6 = zaman6-11.1111;
servos[5].writeMicroseconds(zaman6);
break;
case 27:
zaman6 = zaman6+11.1111;
servos[5].writeMicroseconds(zaman6);
break;
}
delay(speedDelay);
Serial.print("m: ");
Serial.println(m);
Serial.print("zaman1: ");
Serial.println(zaman1);
}
if (dataIn > 101 & dataIn < 250) {
speedDelay = dataIn / 10;
}
if (m == 12) {
if (index == 0) {
LeftBackWheel.setCurrentPosition(0);
LeftFrontWheel.setCurrentPosition(0);
RightBackWheel.setCurrentPosition(0);
RightFrontWheel.setCurrentPosition(0);
}
lbw[index] = LeftBackWheel.currentPosition();
lfw[index] = LeftFrontWheel.currentPosition();
rbw[index] = RightBackWheel.currentPosition();
rfw[index] = RightFrontWheel.currentPosition();
index++;
m = 0;
}
}
LeftFrontWheel.runSpeed();
LeftBackWheel.runSpeed();
RightFrontWheel.runSpeed();
RightBackWheel.runSpeed();
}
void moveForward() {
LeftFrontWheel.setSpeed(tekerhizi);
LeftBackWheel.setSpeed(tekerhizi);
RightFrontWheel.setSpeed(tekerhizi);
RightBackWheel.setSpeed(tekerhizi);
}
void moveBackward() {
LeftFrontWheel.setSpeed(-tekerhizi);
LeftBackWheel.setSpeed(-tekerhizi);
RightFrontWheel.setSpeed(-tekerhizi);
RightBackWheel.setSpeed(-tekerhizi);
}
void moveSidewaysRight() {
LeftFrontWheel.setSpeed(tekerhizi);
LeftBackWheel.setSpeed(-tekerhizi);
RightFrontWheel.setSpeed(-tekerhizi);
RightBackWheel.setSpeed(tekerhizi);
}
void moveSidewaysLeft() {
LeftFrontWheel.setSpeed(-tekerhizi);
LeftBackWheel.setSpeed(tekerhizi);
RightFrontWheel.setSpeed(tekerhizi);
RightBackWheel.setSpeed(-tekerhizi);
}
void rotateLeft() {
LeftFrontWheel.setSpeed(-tekerhizi);
LeftBackWheel.setSpeed(-tekerhizi);
RightFrontWheel.setSpeed(tekerhizi);
RightBackWheel.setSpeed(tekerhizi);
}
void rotateRight() {
LeftFrontWheel.setSpeed(tekerhizi);
LeftBackWheel.setSpeed(tekerhizi);
RightFrontWheel.setSpeed(-tekerhizi);
RightBackWheel.setSpeed(-tekerhizi);
}
void moveRightForward() {
LeftFrontWheel.setSpeed(tekerhizi);
LeftBackWheel.setSpeed(0);
RightFrontWheel.setSpeed(0);
RightBackWheel.setSpeed(tekerhizi);
}
void moveRightBackward() {
LeftFrontWheel.setSpeed(0);
LeftBackWheel.setSpeed(-tekerhizi);
RightFrontWheel.setSpeed(-tekerhizi);
RightBackWheel.setSpeed(0);
}
void moveLeftForward() {
LeftFrontWheel.setSpeed(0);
LeftBackWheel.setSpeed(tekerhizi);
RightFrontWheel.setSpeed(tekerhizi);
RightBackWheel.setSpeed(0);
}
void moveLeftBackward() {
LeftFrontWheel.setSpeed(-tekerhizi);
LeftBackWheel.setSpeed(0);
RightFrontWheel.setSpeed(0);
RightBackWheel.setSpeed(-tekerhizi);
}
void stopMoving() {
LeftFrontWheel.setSpeed(0);
LeftBackWheel.setSpeed(0);
RightFrontWheel.setSpeed(0);
RightBackWheel.setSpeed(0);
}
void runSteps() {
while (dataIn != 13) {
for (int i = 0; i <= index - 2; i++) {
if (Bluetooth.available()>0) {
dataIn = Bluetooth.read();
if ( dataIn == 15) {
while (dataIn != 14) {
if (Bluetooth.available()) {
dataIn = Bluetooth.read();
if ( dataIn == 13) {
break;
}
}
}
}
if (dataIn > 100 && dataIn < 150) speedDelay = dataIn / 10;
if (dataIn > 30 && dataIn < 100) {
tekerhizi = dataIn * 10;
dataIn = 14;
}
}
LeftFrontWheel.moveTo(lfw[i]);
LeftFrontWheel.setSpeed(tekerhizi);
LeftBackWheel.moveTo(lbw[i]);
LeftBackWheel.setSpeed(tekerhizi);
RightFrontWheel.moveTo(rfw[i]);
RightFrontWheel.setSpeed(tekerhizi);
RightBackWheel.moveTo(rbw[i]);
RightBackWheel.setSpeed(tekerhizi);
while (LeftBackWheel.currentPosition() != lbw[i] || LeftFrontWheel.currentPosition() != lfw[i] || RightFrontWheel.currentPosition() != rfw[i] || RightBackWheel.currentPosition() != rbw[i]) {
LeftFrontWheel.runSpeedToPosition();
LeftBackWheel.runSpeedToPosition();
RightFrontWheel.runSpeedToPosition();
RightBackWheel.runSpeedToPosition();
}
}
}
}