Forum >serial comm port , w,a,s,d case statements.
General

serial comm port , w,a,s,d case statements.

userHead bkrause 2012-02-25 22:21:44 5469 Views3 Replies
hi guys, i have a question which mite be really simple to answer,

romeo board, default drive control code used
#include <SoftwareSerial.h>


#include <Stepper.h>

int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control


///
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void advance(char a,char b) //Move forward
{
analogWrite (E1,a); //PWM Speed Control
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void back_off (char a,char b) //Move backward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void turn_L (char a,char b) //Turn Left
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void turn_R (char a,char b) //Turn Right
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void setup(void)
{
int i;
for(i=6;i<=9;i++)
pinMode(i, OUTPUT);
Serial.begin(9600); //Set Baud Rate
}
void loop(void)
{
char val = Serial.read();
if(val!=-1)
{
switch(val)
{
case 'w'://Move Forward
advance (170,170); //PWM Speed Control
break;
case 's'://Move Backward
back_off (170,170);
break;
case 'a'://Turn Left
turn_L (170,170);
break;
case 'd'://Turn Right
turn_R (170,170);
break;
///added to test comms
case 'z':
Serial.print("hello");
///added
}
delay(2000);
}
else stop();
}


i want to give command such as drive forward "wwwwww", but to be able to stop the bot moving half way or so with a stop/halt command..is its possible to override a sequence that has been sent down the commport?


void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
ive tried working this sub in to the case statement such as

case 'h':
stop;
break;

but its doesnt work, im just nt sure how to do this, or even if it is possible to overide sent commands?

thanks for your time guys,
2012-02-28 22:15:24 ;D

haha...You're welcome.
userHeadPic Lauren
2012-02-28 22:03:44 sorry for mot replying sooner, thank you, i love you soo much lol, seriously thank you, does it exactly want i wanted it to do and its being manipulated from a vb6 front end so all i do is click buttons, click forwards it goes..forward, click left it starts turning left and stop will stop any command dead in its tank track.. ive even managed to get data output (printins) from arduino to vb.

again thanks again for the help
userHeadPic bkrause
2012-02-27 19:28:56 I improve the sample code a little bit. Have a try.
Code: Select all
//Standard PWM DC control
int E1 = 5;    //M1 Speed Control
int E2 = 6;    //M2 Speed Control
int M1 = 4;    //M1 Direction Control
int M2 = 7;    //M1 Direction Control
///For previous Romeo, please use these pins.
//int E1 = 6;    //M1 Speed Control
//int E2 = 9;    //M2 Speed Control
//int M1 = 7;    //M1 Direction Control
//int M2 = 8;    //M1 Direction Control
void stop(void)                    //Stop
{
  digitalWrite(E1,LOW); 
  digitalWrite(E2,LOW);     
void advance(char a,char b)          //Move forward
{
  analogWrite (E1,a);      //PWM Speed Control
  digitalWrite(M1,HIGH);   
  analogWrite (E2,b);   
  digitalWrite(M2,HIGH);
void back_off (char a,char b)          //Move backward
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW); 
  analogWrite (E2,b);   
  digitalWrite(M2,LOW);
}
void turn_L (char a,char b)            //Turn Left
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);   
  analogWrite (E2,b);   
  digitalWrite(M2,HIGH);
}
void turn_R (char a,char b)            //Turn Right
{
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);   
  analogWrite (E2,b);   
  digitalWrite(M2,LOW);
}
void setup(void)
{
  int i;
  for(i=4;i<=7;i++)
    pinMode(i, OUTPUT); 
  Serial.begin(19200);      //Set Baud Rate
  Serial.println("Run PC keyboard control");
}
void loop(void)
{
  if(Serial.available()){
    char val = Serial.read();
    if(val != -1)
    {
      switch(val)
      {
      case 'w'://Move Forward
        advance (255,255);  //move forward in max speed
        break;
      case 's'://Move Backward
        back_off (255,255);  //move back in max speed
        break;
      case 'a'://Turn Left
        turn_L (100,100);
        break;     
      case 'd'://Turn Right
        turn_R (100,100);
        break;
      case 'z':
        Serial.println("Hello");
        break;
      case 'x':
        stop();
        break;
      }
    }
    else stop(); 
  }
}
userHeadPic Lauren