Go Down

Topic: from 4 servos to 6 servos (Read 1 time) previous topic - next topic

pavlado

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);
}

MarkT

It would be helpful if you could edit your posting replacing the garbled code with the
smiley with the original sketch in code tags.  The # button generates code tags for
you to make it easier.
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

Robin2

As well as making your code so we can read it ...

What have you tried? and what results did you have?

If you haven't yet tried anything then what do you think might be the solution?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

pavlado

Code: [Select]
#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(smiley-cool;
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);
}



I tried something like this: Servo myservo1;
                                          Servo myservo2;
                                          Servo myservo3;
                                          Servo myservo4;
                                          Servo myservo5;
                                          Servo myservo6;
so I redid the entire code, and when I gave upload to the board so the
myservo5.write (valZSP);
I announced a mistake.

Robin2


so I redid the entire code, and when I gave upload to the board so the
myservo5.write (valZSP);
I announced a mistake.


We are all a bit slow here. You will need to tell us what the mistake is.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

You're on right lines, add one more servo object, add a little code at a time and
check everything still compiles - work by example from the existing code and
you should at least be able to get it copying what an existing servo does - you'll
then have to change the code to make it do what you actually want.

Repeat for the 6th servo.

Then go and look at arrays, its probably time to organise those servo objects
into an array to keep the code tight and manageable - but first get something working
and save a copy (!)
[ I DO NOT respond to personal messages, I WILL delete them unread, use the forum please ]

JimboZA

By the way, you didn't copy paste your code into code tags from the original sketch as requested, you obviously did it from the top post, so you still have the smiley. Except now it's not a real smiley it's the text of a smiley.... Look at the 3rd line in setup().

It might also help if you explained what the sketch is supposed to do. As MarkT says, expanding 4 servos to 6 by copying might compile, but will it do what you want?
Johannesburg hams call me: ZS6JMB on Highveld rep 145.7875 (-600 & 88.5 tone)
Dr Perry Cox: "Help me to help you, help me to help you...."
Your answer may already be here: https://forum.arduino.cc/index.php?topic=384198.0

pavlado

Thank you to everyone for your help, I have it, I found one that works.  :)

AWOL

Your code suffers from a very common problem - you check to see that there is at least one character to read, you read it, then you read three more, which may well not have arrived yet.
A simple "delay" is not always sufficient to ensure  you've received data, particularly during testing.

Code: [Select]
myservo3.attach(smiley-cool;
Usually, it's best to cut an paste code from your IDE, not from the forum after it has mangled the code ;)

Grumpy_Mike

Quote
I have it, I found one that works

So you think that finding code that some one else has done is OK?
You have not learned a thing and you will be back again asking how to do the next bit.

There is no substitute for learning to code.

Go Up