can someone pls help me i am trying to control 5 servos with a 5char string consisting of 0s and 1s where 0 is 0degree and 1 is 180degree for the servo but for some reason servos do not respond at all can someone help me find my mistake?
#include <Servo.h>
Servo servoThumb;
Servo servoIndex;
Servo servoMiddle;
Servo servoRing;
Servo servoPinky;
String str[5] = {"00000", "11111", "00000", "11111", "00000"};
String rec;
int vals[5];
int i,j;
void setup() {
Serial.begin(9600);
servoThumb.attach(3);
servoIndex.attach(6);
servoMiddle.attach(5);
servoRing.attach(4);
servoPinky.attach(7);
for(int i=0;i<5;i++){
rec=str[i];
for(int j=0;j<5;j++){
vals[j] = rec.substring(j,j+1).toInt();
Serial.println(vals[j]);
}
if(vals[0] == 1){servoThumb.write(180);}else{servoThumb.write(0);}
if(vals[1] == 1){servoIndex.write(180);}else{servoIndex.write(0);}
if(vals[2] == 1){servoMiddle.write(180);}else{servoMiddle.write(0);}
if(vals[3] == 1){servoRing.write(180);}else{servoRing.write(0);}
if(vals[4] == 1){servoPinky.write(180);}else{servoPinky.write(0);}
//Serial.println(rec);
//Serial.println(vals[5]);
//Serial.println(i);
//rec="";
}
}
void loop() {
}
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
then what shd i use instead
Are you sure your loopings are working? i and j are beggining from zero. Zero < 5 always...
alto777
November 29, 2021, 7:06pm
6
jarvisman00723:
Serial.println(vals[j]);
Are the printed values plausible?
Can you run the servos in a simpler test program, maybe for just one servo for checking basic function and another to see about basic function testing all at once?
How are you powering the servos?
a7
yes i checked by printing j and i it looked like they r working fine but u can also try it in case i am wrong
LarryD
November 29, 2021, 7:08pm
8
jarvisman00723:
String str[5] = {"00000", "11111", "00000", "11111", "00000"};
byte myNums[] = {0b00000000,0b11111111,0b00000000,0b11111111,0b00000000 };
there is another code which uses smillar string for controlling servos and that one is much more complex but works fine as it is intend but this one is not so there is no problem in then hardware its just the code.
About 95% of servo problems reported on this forum are power problems.
Please answer the questions posted in reply #6
the servos work fine with another similar type of code i an using mg 996r servos and have external power supply hocked on so there is no problem with the servo as they work just fine with another code
LarryD
November 29, 2021, 7:22pm
13
You are using byte instead of Strings.
You can use bit(index) to sample the individual bits in the byte for a 1 or 0.
Then step the index variable.
Have not looked closely at the code, make sure you give time for the servos to actually do the required movement.
We always need a good schematic so we came be effective when we answer your questions.
okay i am i little new to arduino so might face some issues in following through but i understood ur suggestion and there was also a good amount of time for the servoes to respond but they simply didnt.
Power or ground problem, despite OP's unsupported claims to the contrary.
LarryD
November 29, 2021, 7:26pm
16
First thing first, as mentioned previously, run a test sketch to confirm the servos will all work.
And give use a schematic
#include <Servo.h>
#define numOfValsRec 5
#define digitsPerValRec 1
Servo servoThumb;
Servo servoIndex;
Servo servoMiddle;
Servo servoRing;
Servo servoPinky;
int valsRec[numOfValsRec];
//$00000
int stringLength = numOfValsRec * digitsPerValRec + 1;
int counter = 0;
bool counterStart = false;
String recievedString;
void setup() {
Serial.begin(9600);
servoThumb.attach(7);
servoIndex.attach(6);
servoMiddle.attach(5);
servoRing.attach(4);
servoPinky.attach(3);
}
void recieveData(){
while (Serial.available())
{
char c = Serial.read();
if (c == '$'){
counterStart = true;
}
if (counterStart) {
if (counter < stringLength) {
recievedString = String(recievedString + c);
counter ++;
//Serial.println(counter);
//Serial.println(counterStart);
//Serial.println(c);
//Serial.println(recievedString);
}
if (counter >= stringLength) {
for (int i = 0; i < numOfValsRec; i++)
{
int num = (i * digitsPerValRec) + 1;
valsRec[i] = recievedString.substring(num, num + digitsPerValRec).toInt();
Serial.println(valsRec[i]);
}
recievedString = "";
counter = 0;
counterStart = false;
}
}
}
}
void loop() {
recieveData();
if(valsRec[0] == 1){servoThumb.write(180);}else{servoThumb.write(0);}
if(valsRec[1] == 1){servoIndex.write(180);}else{servoIndex.write(0);}
if(valsRec[2] == 1){servoMiddle.write(180);}else{servoMiddle.write(0);}
if(valsRec[3] == 1){servoRing.write(180);}else{servoRing.write(0);}
if(valsRec[4] == 1){servoPinky.write(180);}else{servoPinky.write(0);}
}```
just ran this code and the servoes work fine so no hardware issue
This was my thinking as well.
well i just posted a much more complex code which is similar in principle just a bit more is happening there but this code conforms that all the connections are good and working
with this code the servos are moving infront of my eyes so i dont know what more supportive evidence do i need to conform that the connections are good