Programming a serial connection

Hey i am new to Arduino and i was wondering if i could get some help programming a wireless car. I am having trouble programming the code that reads the serial input and then runs commands based off of characters received though the connection. Any help would be appreciated.

Read this:- http://www.ladyada.net/learn/arduino/lesson4.html

Simple test code for use with the serial monitor.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(2);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);

    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}

How would you make it so like you sent a command through serial connection that said to make the car go forward, and then sent a different command that would make it stop?

Ranster: How would you make it so like you sent a command through serial connection that said to make the car go forward, and then sent a different command that would make it stop?

You would connect the appropriate hardware between the car and the arduino, then write the code that made the car do what you want. I suggest you do more reading, research, and study on the subject then ask specific questions on what you are having difficulty with.

Ranster: How would you make it so like you sent a command through serial connection that said to make the car go forward, and then sent a different command that would make it stop?

Zoomkat has answered your question: how to make the Arduino do (something) based on serial input.

Are you now asking how to have the Arduino make the car move forwards and stop? That's a completely different question, and much harder to answer since it needs you to decide how to interface the Arduino to your car's control system, and we don't know anything about your car or how you're going to interface it.

The problem that i am having is when i send the command to go forward the car goes forward, but then when i send the command to stop it just keeps going forward and i cant get it to stop.

int drivePin1 = 3;
int drivePin2 = 4;
int enable1Pin = 9;
//int steerPin1 = 5;
//int steerPin2 = 6;
//int enable2Pin = 10;

void setup() {
  Serial.begin(115200);      //serial connection number
  pinMode(drivePin1, OUTPUT);
  pinMode(drivePin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
//  pinMode(steerPin1, OUTPUT);
//  pinMode(steerPin2, OUTPUT);
//  pinMode(enable2Pin, OUTPUT);
//  digitalWrite(enable2Pin, HIGH);
Serial.println("Setup Complete");
}

//void SteerLeft()
//{
//  digitalWrite(steerPin1, HIGH);
//  digitalWrite(steerPin2, LOW);
//}

//void SteerRight()
//{
//  digitalWrite(steerPin1, LOW);
//  digitalWrite(steerPin2, HIGH);
//}

//void SteerStop()
//{
//  digitalWrite(steerPin1,LOW);
//  digitalWrite(steerPin2,LOW);
//}

void DriveForward()
{
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(drivePin1, LOW);
  digitalWrite(drivePin2, HIGH);
}

void DriveBackward()
{
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(drivePin1, HIGH);
  digitalWrite(drivePin2, LOW);
}
void DriveStop()
{
  digitalWrite(drivePin1, LOW);
  digitalWrite(drivePin2, LOW);
}

void loop() {
   //read the serial data into the buffer
  if(Serial.available() > 0)
  {
      if(Serial.read() == 'w');
      {
          DriveForward();   
      }
  
      if(Serial.read() == 'b');
      {
          DriveBackward();
      } 
      if(Serial.read() == 's');
      {
          digitalWrite(drivePin1, LOW);
          digitalWrite(drivePin2, LOW);
          digitalWrite(enable1Pin, LOW);
      }   
    }
}

Two things, 1) go back and modify the post, select the code and hit the # icon.

2) You are using

if(Serial.read() == 'w');

So the serial byte is now gone, there is nothing there when you later say:-

if(Serial.read() == 'b');

You need to put the result of Serial.read() into a variable and then compare that variable to many things.

Would this work?

int drivePin1 = 3;
int drivePin2 = 4;
int enable1Pin = 9;
//int steerPin1 = 5;
//int steerPin2 = 6;
//int enable2Pin = 10;
char inSerial;

void setup() {
  Serial.begin(115200);      //serial connection number
  pinMode(drivePin1, OUTPUT);
  pinMode(drivePin2, OUTPUT);
  pinMode(enable1Pin, OUTPUT);
//  pinMode(steerPin1, OUTPUT);
//  pinMode(steerPin2, OUTPUT);
//  pinMode(enable2Pin, OUTPUT);
//  digitalWrite(enable2Pin, HIGH);
Serial.println("Setup Complete");
}

//void SteerLeft()
//{
//  digitalWrite(steerPin1, HIGH);
//  digitalWrite(steerPin2, LOW);
//}

//void SteerRight()
//{
//  digitalWrite(steerPin1, LOW);
//  digitalWrite(steerPin2, HIGH);
//}

//void SteerStop()
//{
//  digitalWrite(steerPin1,LOW);
//  digitalWrite(steerPin2,LOW);
//}

void DriveForward()
{
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(drivePin1, LOW);
  digitalWrite(drivePin2, HIGH);
}

void DriveBackward()
{
  digitalWrite(enable1Pin, HIGH);
  digitalWrite(drivePin1, HIGH);
  digitalWrite(drivePin2, LOW);
}
void DriveStop()
{
  digitalWrite(drivePin1, LOW);
  digitalWrite(drivePin2, LOW);
}

void loop() 
{
   //read the serial data into the buffer
  while(Serial.available() > 0)
  {
    inSerial == Serial.read();
  } 
     if(inSerial == 'w')
      {
          DriveForward();   
      }
      else if(inSerial == 'b')
      {
          DriveBackward();
      } 
      else if(inSerial == 's')
      {
          digitalWrite(drivePin1, LOW);
          digitalWrite(drivePin2, LOW);
          digitalWrite(enable1Pin, LOW);
      }   
}
  while(Serial.available() > 0)
  {
    inSerial == Serial.read();
  }

It'll probably work most of the time, but if any of your called functions take longer to execute than the transmission time of a single character, then you may start to miss things. Better to use "if", rather than "while"

  while(Serial.available() > 0)
  {
    inSerial == Serial.read();
  }

No, this is wrong. This is comparing inSerial to the value read from the serial port, and then discarding the value and the result of the comparison.

It is also reading all serial data that is available, not just the next character.

Change the while to an if, change the == to =, and move the } after the if/else if collection.

Then, instead of asking us if it will work, just try it.

Oops, missed the "=="