heyyy all,
can anyone please help me with my code for the dual servo control project ,it doesn't work code gives a controlling error i cant figure it out =(
#include<servo.h>
char buffer[10];
Servo servo1;
Servo servo2;
void setup()
{
servo1.attach(5);
servo2.attach(6);
Serial.begin(9600);
Serial.flush();
servo1.write(90);
servo2.write(90);
Sreial.println("starting...");
}
void loop()
{
if(Serial.available()>0)
{
int index=0;
delay(100);
int numChar = Serial.available();
if (numChar>10)
{
numChar=10;
}
while(numChar--)
{
buffer{index++}= Serial.read();
}
splitString(buffer);
}
}
void splitString(char*data)
{
Serial.print("data entered....");
Serial.println(data);
char*parameter;
parameter = strtok(data,",");
while(parameter != null)
{
setServo(parameter);
parameter= strtok (NULL,",");
}
for (int x=0,x <9 ,x++)
{
buffer[x]='\0';
}
Serial.flush();
}
}
void setsServo(char*data)
{
if((data[0]=='L')||(data[0] =='1'))
{
int firstVal = strtol(data+1,NULL,10);
firstVal = constrain(firstVal,0,180);
servo1.write(firstVal);
Serial.print("servo1 is set to ...");
Serial.println(firstVal);
}
if((data[0]=='R')||(data[0] =='r'))
{
int secondVal = strtol(data+1,NULL,10);
firstVal = constrain(secondVal,0,180);
servo2.write(secondVal);
Serial.print("servo2 is set to ...");
Serial.println(secondVal);
}
}
Maybe spell-check the rest of your code.
Pay attention to capitals where there should be capitals ("servo.h: No such file or directory"), brackets where there should be brackets ("buffer{index++}= ") etc.
You are testing to see that there is at least one character to read, then reading up to 11 of the 1 available. Do you see a problem with that?
You are passing strtok() an array of characters, when it expects a string. The difference is that a string is an array of characters THAT IS NULL TERMINATED. You do not NULL terminate your array. Do you know why that could be a problem?
Serial.flush();
Do you know what this function does? Do you know why using it is generally a bad idea?
for (int x=0,x <9 ,x++)
Clauses in a for loop are separated by ;, not commas. The comma serves a different purpose in a for loop.