Multiple commands at once (Serial comm)

So I have an RC car that's has individual parts working via Duemilanove and BlueSMiRF, and now am stuck in probably a simple programming problem.

I have BlueSMiRF hooked up to serial comm pins (RX and TX) on Arduino, which works fine from the GTKTerm I've been using. So commands are like 'w' for forward, 'a' for steer left, etc.

Now, I want to know how I can have multiple commands running at the same time (or seemingly).

For example: 1. Steer and Move forward/backward at the same time 2. Honk while moving 3. Right/Left signal blinks while moving. 4. etc. 5. etc.

I saw the BlinkWithOutDelay example, which was interesting but not sure how I can use it.

I understand that Arduino's processing one line at a time.

Any tips, ideas or examples?

Thank you

Please post the code you have so far.

Still fumbling through it. Trying to get steer and move first.

/*-------------------------------------------------------------------*/

// Output Pin Assignment
  // Digital Pins
  int headlight = 2;
  int signal_left = 8;
  int signal_right = 4;
  int rear_brake = 5;
  int rear_red = 6;
  int rear_rev = 7;
  int horn = 12;
  
  // Analog (PWM) Pins
  int motor_f=3;
  int motor_b=9;  
  int motor_r=10;
  int motor_l=11;

// Variables
  int InitCheck = 0;
  int i = 0;
  int head_status = 0;
  int defSig = 300;
  int loop_counter = 0;

// Toggle Variables
  int leftToggle = 0;
  int rightToggle = 0;
  int reverseToggle = 0;
  int xenonToggle = 0;

// Serial data
  char val;         // variable to receive data from the serial port


/*-------------------------------------------------------------------*/

/* Basic Setup Function */
  void setup() {
     // Initialize analog pins as outputs
     pinMode(motor_f, OUTPUT);   
     pinMode(motor_b, OUTPUT);
     pinMode(motor_r, OUTPUT);
     pinMode(motor_l, OUTPUT);   
  
    // initialize the digital pin as an output:
    pinMode(headlight, OUTPUT);
    pinMode(signal_right, OUTPUT);
    pinMode(signal_left, OUTPUT);  
    pinMode(rear_red, OUTPUT);  
    pinMode(rear_brake, OUTPUT);    
    pinMode(rear_rev, OUTPUT);  
    pinMode(horn, OUTPUT);
    
    // Start serial communication at 115200 bps
    // Meets Bluetooth Default Setting  
    Serial.begin(115200);
  }

/*-------------------------------------------------------------------*/

/* Car Functions */

/*** Init Function ***/
  //-- Simple Car Horn bleep
    void carIsOn(int interval){
        digitalWrite(horn, HIGH);
        delay(interval);
        digitalWrite(horn, LOW);
        delay(interval);  
    }

/*** Car Movemente Functions ***/ 
  //-- Move Back and Forth functions
    void moveForBack(char command){      
      // Move forward
      if (command == 'w'){
        digitalWrite(rear_rev, LOW);    
        analogWrite(motor_f, 255);
        analogWrite(motor_b, 0);
      }

      // Move backward      
      else if  (command == 's'){
        digitalWrite(rear_rev, HIGH);
        analogWrite(motor_f, 0);
        analogWrite(motor_b, 255);
      }

      // None of the above      
      else{
        digitalWrite(rear_rev, LOW);
        analogWrite(motor_f, 0);
        analogWrite(motor_b, 0);
      }
    }

  //-- Steering functions
    void steer(char command){
      // Turn left
      if (command == 'a'){
          turnLeft();
      }

      // Turn right      
      else if (command == 'd'){
          turnRight();
      }
      
      // None of the above      
      else{
        analogWrite(motor_l,0);
        analogWrite(motor_r,0);
      }
    }

    // Turn Left
      void turnLeft(){
        analogWrite(motor_l,255);
        analogWrite(motor_r,0);
      }

    // Turn Right      
      void turnRight(){
        analogWrite(motor_l,0);
        analogWrite(motor_r,255);
      }      
      
/*** Car Signal Functions ***/

  //-- Right Turn Signal
    void rightSignal(){
        digitalWrite(signal_right, HIGH);   // set the LED on
        delay(300);
        digitalWrite(signal_right, LOW);   // set the LED on    
        delay(300);
    }
  
  //-- Left Turn Signal
    void leftSignal(){
        digitalWrite(signal_left, HIGH);   // set the LED on
        delay(300);
        digitalWrite(signal_left, LOW);   // set the LED on  
        delay(300);
    }
  
  //-- Warning Light Signal
    void warningSignal(int interval){
        digitalWrite(signal_left, HIGH);   // set the LED on        
        digitalWrite(signal_right, HIGH);   // set the LED on
        delay(interval);                         // wait for 0.3 second
        digitalWrite(signal_right, LOW);    // set the LED off
        digitalWrite(signal_left, LOW);   // set the LED on    
        delay(interval);                         // wait for 0.3 second     
    }

/*** Car Horn Function ***/
  void honk(){
    digitalWrite(horn, HIGH); 
  }

/*** Car Front Light Function***/
  //-- Headlight Function
    void xenonLight(int mode){
      if (mode == 1){
        digitalWrite(headlight, HIGH);
      }
      
      else{
        digitalWrite(headlight, LOW);
      }
    }

  //-- Brake Light
    void brakeLight(){
       digitalWrite(rear_brake, HIGH);
    }

/*** Emergency All-Off Function ***/
  void allOff(){
    analogWrite(motor_r,0);
    analogWrite(motor_l,0);
    analogWrite(motor_f,0);    
    analogWrite(motor_b,0);    
    digitalWrite(signal_right, LOW);    
    digitalWrite(signal_left, LOW);        
    digitalWrite(rear_rev, LOW);
    digitalWrite(rear_red, LOW);        
    digitalWrite(horn, LOW);    
  }

/*-------------------------------------------------------------------*/

/*  Forever Loop */
  void loop() {
    // Let the user know that if the car just turned on, 
    // Honk the horn twice long
      if (InitCheck == 0) {
        for (i = 0; i<2; i++ ) {
          carIsOn(250);
          warningSignal(50);
        }
        InitCheck = 1;
      }
      
    
    // If Serial Data is available,  
      if( Serial.available() )
      { 
        val = Serial.read();          // read it and store it in 'val'
        Serial.print(val);  // Print serial input      
        Serial.flush();
  
        // First time the serial connection is established,
        // Let the user know by horn that it is connected
          if (InitCheck == 1){        
             for (i=0;i<3;i++){
               carIsOn(100);      // Honk the horn three times
             }
             InitCheck = 2;    // Done init check
          }    
           
          moveForBack(val);  // Move back and forth
          steer(val);        // Steer left anf right
          
          
          if (val == 'l'){
            xenonToggle = !xenonToggle;
            xenonLight(xenonToggle);
          }
          
          if (val == 'q'){
            leftSignal();
          }
          
          if (val == 'e'){
            rightSignal(); 
          }
          
          if (val == 'h'){
            honk();
          }
          
        loop_counter = 3; // increase this or the delay() to reduce choppiness
  
      }
      
      // No serial comm available
      else{        
        if(loop_counter == 0){
          allOff();
      }
        else{
          // hold the current state until loop_counter reaches 0
            loop_counter--;
      }
      } 
   
      // Rear non-brake red light is always on once the car is on
        if (InitCheck == 2){
          digitalWrite(rear_red, HIGH);  
        }              

    delay(125);  // Short delay between messages
  
  }
if( Serial.available() )
      {
        val = Serial.read();          // read it and store it in 'val'
        Serial.print(val);  // Print serial input      
        Serial.flush();

What's the point of calling "flush()"? It will just discard any data in the input buffer, meaning you might miss a message. Serial.read() will automatically remove the byte you read from the input buffer.

The ideas from BlinkWithoutDelay is what you need in order to do many things simultaneously. Try with just two commands first to get it going. I.e. instead of delay()ing you compute millis() + delaytime to be the moment to turn off, then let loop() constantly check the current millis() against the computed stop-times.

You need a finite state machine, that is what the blink without delay is doing. See:- http://www.arduino.cc/playground/Code/FiniteStateMachine

I tried “flush()” because the car kept going even when I wasn’t pressing certain commands.

I didn’t know there was an FSM.

Thank you all