Hi,
I'm trying to send a command through serial in the format of "L1023" in order to move a motor to the left direction and 1023 speed. My approach is to store the command it to a char array, get the first element of array and then the remaining elements as integer using the atoi function but the results are not the expected. When I enter a sequence of "L100", L200, L300 the last one suppose to run faster but it doesn't. Do you know why? Its getting me crazy this.
void loop(){
index = 0;
i = 0;
//Get the serial command and store it to the char array
while (Serial.available()){
cmd[index] = Serial.read();
delay(1);
index++;
}
cmd[index] = 0;
int i = atoi(cmd+1);
if (index > 0){
Serial.println(i);
motorControl(cmd[0], i);
}
}
void motorControl(char dir, int spd){
if (dir == 'L'){
//turn left or counterclockwise at spd speed
digitalWrite(pinA, LOW);
digitalWrite(pinB, HIGH);
analogWrite(pinB, spd);
}
else if (dir == 'R'){
//turn right or clockwise at spd speed
digitalWrite(pinA, HIGH);
digitalWrite(pinB, LOW);
analogWrite(pinA, spd);
}
else{
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
}