Hi,
I have a problem, I have a code in the Arduino Nano to control 4 servo motors, but I want to control them 6th but do not know how to redo the code. I'm a complete beginner with Arduino code.
I could anyone help with this?
Here is this code :
#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
char kind_of_data;
int valLSP = 15;
int valRSP = 15;
int valXSP = 15;
int valYSP = 15;
void setup()
{
myservo1.attach(6);
myservo2.attach(7);
myservo3.attach(8);
myservo4.attach(9);
myservo1.write(valLSP);
myservo2.write(valRSP);
myservo3.write(valXSP);
myservo4.write(valYSP);
Serial.begin(115200);
}
void loop()
{
while (Serial.available() > 0)
{
kind_of_data = Serial.read();
if (kind_of_data == 'R' ) Read_RightServo();
if (kind_of_data == 'L' ) Read_LeftServo();
if (kind_of_data == 'X' ) Read_RechterServo();
if (kind_of_data == 'Y' ) Read_LinkerServo();
myservo1.write(valRSP);
myservo2.write(valLSP);
myservo3.write(valXSP);
myservo4.write(valYSP);
}
}
void Read_RightServo(){
int RightServoPos = 0;
delay(2);
int RSP100 = Serial.read();
delay(2);
int RSP10 = Serial.read();
delay(2);
int RSP1= Serial.read();
RSP100 = ((RSP100)-48)*100;
RSP10 = ((RSP10)-48)*10;
RSP1 = ((RSP1)-48)*1;
if (RSP10 < 0 && RSP1 < 0){RSP100 = RSP100/100;RSP10 = 0;RSP1 = 0;}
if (RSP1 < 0){RSP100 = RSP100/10;RSP10 = RSP10/10;RSP1 = 0;}
RightServoPos = RSP100+RSP10+RSP1;
valRSP = map(RightServoPos, 0, 255, 15, 165);//already testet (RightServoPos, 127, 255, 15, 165)
}
void Read_LeftServo(){
int LeftServoPos = 0;
delay(2);
int LSP100 = Serial.read();
delay(2);
int LSP10 = Serial.read();
delay(2);
int LSP1= Serial.read();
LSP100 = ((LSP100)-48)*100;
LSP10 = ((LSP10)-48)*10;
LSP1 = ((LSP1)-48)*1;
if (LSP10 < 0 && LSP1 < 0){LSP100 = LSP100/100;LSP10 = 0;LSP1 = 0;}
if (LSP1 < 0){LSP100 = LSP100/10;LSP10 = LSP10/10;LSP1 = 0;}
LeftServoPos = LSP100+LSP10+LSP1;
valLSP = map(LeftServoPos, 0, 255, 15, 165); //already testet (RightServoPos, 0, 126, 15, 165)
}
void Read_RechterServo(){
int RechterServoPos = 0;
delay(2);
int XSP100 = Serial.read();
delay(2);
int XSP10 = Serial.read();
delay(2);
int XSP1= Serial.read();
XSP100 = ((XSP100)-48)*100;
XSP10 = ((XSP10)-48)*10;
XSP1 = ((XSP1)-48)*1;
if (XSP10 < 0 && XSP1 < 0){XSP100 = XSP100/100;XSP10 = 0;XSP1 = 0;}
if (XSP1 < 0){XSP100 = XSP100/10;XSP10 = XSP10/10;XSP1 = 0;}
RechterServoPos = XSP100+XSP10+XSP1;
valXSP = map(RechterServoPos, 0, 255, 15, 165);
}
void Read_LinkerServo(){
int LinkerServoPos = 0;
delay(2);
int YSP100 = Serial.read();
delay(2);
int YSP10 = Serial.read();
delay(2);
int YSP1= Serial.read();
YSP100 = ((YSP100)-48)*100;
YSP10 = ((YSP10)-48)*10;
YSP1 = ((YSP1)-48)*1;
if (YSP10 < 0 && YSP1 < 0){YSP100 = YSP100/100;YSP10 = 0;YSP1 = 0;}
if (YSP1 < 0){YSP100 = YSP100/10;YSP10 = YSP10/10;YSP1 = 0;}
LinkerServoPos = YSP100+YSP10+YSP1;
valYSP = map(LinkerServoPos, 0, 255, 15, 165);
}