Pages: [1]   Go Down
Author Topic: Char array to integer  (Read 619 times)
0 Members and 1 Guest are viewing this topic.
Crete, Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 15
HelloArduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Code:
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);
    }
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13705
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the string get read in completely? if L300 gets cut of to L30 it would explain all

What is speed of your Serial? 9600? As at 9600 baud there is approx 1 char per millisecond, you might need to increase the delay(1) to delay(5) or make it adaptive

Code:
while (Serial.available() > 0)
{  
  cmd[index++] = Serial.read();
  int t = 5;                                   // max 5 millis between chars
  while ((Serial.available() == 0 ) && (t-- > 0)) delay(1);
}
cmd[index] = 0;
« Last Edit: May 13, 2011, 04:02:44 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Crete, Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 15
HelloArduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've increased the delay but still the same problem. The baud rate is at 9600. Something else is the cause that I cannot figure out because when I enter L200 and then L500 then the motor goes quicker than before. How can be sure that the integer value is the right one? I've puted a println function in order to see the value but it doesn't make sense. The 300 speed is lower than the 200 which is not normal.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Isn't the normal PWM range 0 to 255? So, it could be overflow. Instead of 300 you get 45, while the 500 rolls over to 245. Try 255 and 256 and see what happens
Logged

Crete, Greece
Offline Offline
Newbie
*
Karma: 0
Posts: 15
HelloArduino
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It worked!!! The range is 0-255 and not 0-1024 as I thought. This was my confusing due to my little experience.
Thank you for your help.
Logged

Pages: [1]   Go Up
Jump to: