2WD robot serial settings

Hi,

I made a basic (probably not the most efficient) program to control a robot from my keyboard using the serial monitor. But would it be possible to set the speed of the robot by writting a number, or a keyword followed by a number? I have no idea how I could make this. (Just to be clear, I'm using a DFRobot motor shield and I'm using PWM to control speed).

Here is my program:

int val;                      //Place to store the variable.
int MR = 4;                      //Motor Right.
int ML = 7;                      //Motor Left.
int MRs = 5;                     //Motor Right speed (PWM).
int MLs = 6;                     //Motor Left speed (PWM).
int fwd = LOW;                   //Motor direction, forward.
int bck = HIGH;                  //Motor direction, backward.
int VAL;               
int Speed;                       //Set the motors speed.
int step1;     
int stepT1;  
int step2;       
int stepT2;      
int TurnStepspeed;     
int TurnSteptime;

void setup(){

 pinMode(4, OUTPUT);                      //Setting pins 4 to 7 as OUTPUTS.
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);

 Serial.begin(9600);                      //Starts serial communication.



}

void loop(){
  

 val = Serial.read();   //val value.


Speed = 255;   
step1 = 100;           //Set Step 1 speed.
stepT1 = 50;          //Set Step 1 time (millis).
step2 = 200;           //Set Step 2 speed.
stepT2 = 100;          //Set Step 2 time (millis).
TurnStepspeed = 100;     //Set   the turning first step speed.
TurnSteptime = 100;       //Set the turnig first step time (millis).
  

 switch(val){                          
  
 case 'w':                      //Go forward.
 digitalWrite(ML, fwd);         //Left motor direction settings.
 digitalWrite(MR, fwd);         //Right motor direction settings.
analogWrite(MRs, (step1));    //Smooth start Step 1.
 analogWrite(MLs, (step1));
 delay(stepT1);                //Time of the step 1.
 analogWrite(MRs, (step2));   //Smooth start Step 2.
 analogWrite(MLs, (step2));
 delay(stepT2);                  //Time of the step 2.
 analogWrite(MRs, Speed);       //Full speed.
 analogWrite(MLs, Speed);       //Full speed.
 break;

 case ' ':                      //STOP.
 digitalWrite(ML, fwd);
 digitalWrite(MR, fwd);
 analogWrite(MRs, 0);
 analogWrite(MLs, 0);

 break;

 case 's':                      //Go backward.
 digitalWrite(ML, bck);
 digitalWrite(MR, bck);
 analogWrite(MRs, (step1));
 analogWrite(MLs, (step1));
 delay(stepT1); 
 analogWrite(MRs, (step2));
 analogWrite(MLs, (step2));
 delay(stepT2); 
 analogWrite(MRs, Speed);
 analogWrite(MLs, Speed);
 break;

 case 'd':                      //Go right.
 digitalWrite(ML, fwd);
 digitalWrite(MR, bck);
 analogWrite(MRs, TurnStepspeed);
 analogWrite(MLs, TurnStepspeed);
 delay(TurnSteptime);
 analogWrite(MRs, (Speed));
 analogWrite(MLs, (Speed));
 break;

 case 'a':                      //Go left.
 digitalWrite(ML, bck);
 digitalWrite(MR, fwd);
 analogWrite(MRs, 150);
 analogWrite(MLs, 150);
 delay(TurnSteptime);
 analogWrite(MRs, (Speed));
 analogWrite(MLs, (Speed));
 break;

 case 'e':                      //Go right and forward.
 digitalWrite(ML, fwd);
 digitalWrite(MR, fwd);
 analogWrite(MRs, (step1));
 analogWrite(MLs, (step1));
 delay(stepT1); 
 analogWrite(MRs, (step2));
 analogWrite(MLs, (step2));
 delay(stepT2); 
 analogWrite(MRs, (Speed-50));
 analogWrite(MLs, Speed);
 break;

 case 'q':                      //Go left and forward.
 digitalWrite(ML, fwd);
 digitalWrite(MR, fwd);
 analogWrite(MRs, (step1));
 analogWrite(MLs, (step1));
 delay(stepT1); 
 analogWrite(MRs, (step2));
 analogWrite(MLs, (step2));
 delay(stepT2); 
 analogWrite(MRs, Speed);
 analogWrite(MLs, (Speed-50));
 break;

 }
}

Thanks!

But would it be possible to set the speed of the robot by writting a number, or a keyword followed by a number? I have no idea how I could make this.

Yes, but first a question. Why is this code in loop?

Speed = 255;  
step1 = 100;           //Set Step 1 speed.
stepT1 = 50;          //Set Step 1 time (millis).
step2 = 200;           //Set Step 2 speed.
stepT2 = 100;          //Set Step 2 time (millis).
TurnStepspeed = 100;     //Set   the turning first step speed.
TurnSteptime = 100;       //Set the turnig first step time (millis).

Do you really need to set these variables to these values on EVERY pass through loop()?

What you want to do is read a value from the serial port, as a NULL terminated array of characters, and then convert that array to an integer (using atoi).

Of course it is not quite as simple as this, as you need some way of knowing when a packet starts and when one ends. Search the forum for "started && ended" for one idea how to do it.

Good question! in fact it's just that I did some experimentation trying be able to control those settings with serial communication and for that I wanted them in the loop. I just letted them there after my tries.

But thanks for the answer, I'm gonna look for that.

Well I,ve been looking for that post for 20 minutes now and I just found posts on which you were reffering to the "started && ended" post like this one: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1288218352/7

Do you have a link to your post?

Thanks,

Reply #3 in that thread contains code similar to what you will need. I'd recommend different start and end markers, so that you know when a packet is corrupt (a start marker following a start marker means that the first packet is incomplete; two end marker with no start marker in between means that the second packet is incomplete).