2WD Electric Car

Dear all,

I would like to build a simple 2WD electric car. I am using a serial bluetooth module and I am controlling them through android smart phone.

I am suck in the programming now.
This is what I want my car to do :
When moving forward, then once I click the left button, it will turn left for a period of time and then continue to move straight. We consider the move to left as interrupt.

Current coding :

I do not know how to use the auto format tools. >.<

Thanks for help

We consider the move to left as interrupt.

Can you explain what you mean please?

Can you also remember to use the auto-format tool before posting code?
My eyes ache.

I mean I want the car to move like this :

Once I press the button moving forward, then it will move forward. While moving forward, I pressing the 'turn left' button then the car will turn left until I released from the 'turn left'button. It seems to be like I program the virtual button to act like a tact switch.

Thanks !

So, what does it do at the moment ?

At the moment, when I press forward and release, the car will move forward. while moving forward, when I click left button and release, then it will change to always turn left(which can be a 360 degree rotate).

I want it to be like when I press the left button it will go left slightly until I release the left button.

Thanks !

Current coding :

Is not accessible to everyone. There is a mechanism for you to attach your code to your post. Use the Additional Options link below the text entry box. Do that only after using Tools + Auto Format.

If you can't figure out that Tools + Auto Format means to go to the Tools menu in the IDE, and then select Auto Format, I don't think that there is much chance of your robot ever working.

PaulS:

Current coding :

Is not accessible to everyone. There is a mechanism for you to attach your code to your post. Use the Additional Options link below the text entry box. Do that only after using Tools + Auto Format.

If you can’t figure out that Tools + Auto Format means to go to the Tools menu in the IDE, and then select Auto Format, I don’t think that there is much chance of your robot ever working.

Current coding

int motorPin1 = 7;
int motorPin2 = 8;
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int buzzer = 11;
int f_b_light = 12 ;
int state;
int flag=0; //makes sure that the serial only prints once the state
boolean buzzerstatus = 0;
boolean LEDstatus = 0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 10, 9, A0, A1, A2);

void setup() {
// sets the pins as outputs:
lcd.begin(16, 2);
pinMode(buzzer,OUTPUT);
pinMode(f_b_light,OUTPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(enablePin_1, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(enablePin_2, OUTPUT);
// sets enablePin high so that motor can turn on:
digitalWrite(enablePin_1, HIGH);
digitalWrite(enablePin_2, HIGH);
pinMode(13,OUTPUT);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}

void loop() {
//if some date is sent, reads it and saves in state
if(Serial.available() > 0){
state = Serial.read();
flag=0;
}
// if the state is ‘0’ the DC motor will turn off
if (state == ‘0’) {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Current State:”);
lcd.setCursor(0, 1);
lcd.print(“STOP”);
flag=1;
}
}
// if the state is ‘1’ the motor will forward
else if (state == ‘1’) {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print(“Current State:”);
lcd.setCursor(0, 1);
lcd.print(“FORWARD”);
flag=1;
}
}
// if the state is ‘2’ the motor will backward
else if (state == ‘2’) {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print(“Current State:”);
lcd.setCursor(0, 1);
lcd.print(“REVERSE”);
flag=1;
}
}

else if (state == ‘3’) {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
if(flag == 0){
lcd.setCursor(0, 0);
lcd.print(“Current State:”);
lcd.setCursor(0, 1);
lcd.print(“RIGHT”);
flag=1; //Left wheel forward, Right wheel static
flag=1;
}
}
else if (state == ‘4’) {
digitalWrite(f_b_light,LOW);
digitalWrite(buzzer,LOW);
digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
if(flag == 0){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Current State:”);
lcd.setCursor(0, 1);
lcd.print(“LEFT”);
flag=1;
}
}

OK, you've found the quote tags, well, the code tags are to the left (#)

AWOL:
OK, you've found the quote tags, well, the code tags are to the left (#)

I tried and at arduino IDE, I copied to forum then paste. It does not work.

Thanks !

{sob}

Is that ok ?

Thanks ! :smiley:

Is that ok ?

Is what OK? The way you posted your code? No, that isn't.

Do not use the copy for forum option. Ctrl-A, Ctrl-C will select and copy all of your code to the clipboard. Then, press the # icon and Ctrl-V to create code tags with your code between them.

  // if the state is '0' the DC motor will turn off
  if (state == '0') {

Doesn’t it matter that you may not have received any data from the phone?

If never received any data, then the car won’t be moving :smiley:

Current code :

int motorPin1 = 7; 
int motorPin2 = 8; 
int enablePin_1 = 6;
int motorPin4 = 2;
int motorPin3 = 4;
int enablePin_2 =3 ;
int buzzer = 11;
int f_b_light = 12 ; 
int state;
int flag=0; //makes sure that the serial only prints once the state
boolean buzzerstatus = 0;
boolean LEDstatus = 0;

#include <LiquidCrystal.h>

LiquidCrystal lcd(13, 10, 9, A0, A1, A2);

void setup() {
  // sets the pins as outputs:
  lcd.begin(16, 2);
  pinMode(buzzer,OUTPUT);
  pinMode(f_b_light,OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(enablePin_1, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(enablePin_2, OUTPUT);
  // sets enablePin high so that motor can turn on:
  digitalWrite(enablePin_1, HIGH);
  digitalWrite(enablePin_2, HIGH);
  pinMode(13,OUTPUT);
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  //if some date is sent, reads it and saves in state
  if(Serial.available() > 0){
    state = Serial.read();
    flag=0;
  }
  // if the state is '0' the DC motor will turn off
  if (state == '0') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("STOP");
      flag=1;
    }
  }
  // if the state is '1' the motor will forward
  else if (state == '1') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D high
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D low
    digitalWrite(motorPin4, HIGH); // set pin 7 on L293D high
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("FORWARD");
      flag=1;
    }
  }
  // if the state is '2' the motor will backward
  else if (state == '2') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, HIGH); // set pin 2 on L293D high
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("REVERSE");
      flag=1;
    }
  }

  else if (state == '3') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin2, HIGH); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin4, LOW); // set pin 7 on L293D low
    if(flag == 0){
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("RIGHT");
      flag=1; //Left wheel forward, Right wheel static
      flag=1;
    }
  }
  else if (state == '4') {
    digitalWrite(f_b_light,LOW);
    digitalWrite(buzzer,LOW);
    digitalWrite(motorPin1, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin2, LOW); // set pin 7 on L293D low
    digitalWrite(motorPin3, LOW); // set pin 2 on L293D high
    digitalWrite(motorPin4, HIGH); // set pin 7 on L293D low
    if(flag == 0){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Current State:");
      lcd.setCursor(0, 1);
      lcd.print("LEFT");
      flag=1;
    }
  }

On every pass through loop, you may receive a character. Or maybe not. You then take some action based on the last character received. The first time that a new character arrives, you display some data on the LCD. From then on, you take action based on the last character received, but you do not show anything on the LCD.

I'm curious as to why you do this. I'd certainly expect you to check for a character. If there is one, read it, show it, and use it. If not, the reasonable thing to do is nothing.

Currently, I received the byte from my phone, then it will perform what it received and display the current status to LCD.

For example, when I pressed '1', then the car will move forward. It will continue to move forward until it received another character byte maybe like '2' , then it will reverse.

What I want it to be now is :
I need to send a continuous '1' to make the car move forward continuously. When I don't press anything on the phone, then it will STOP.

How is it possible for me to do that ? I am curious because once I send '1 ' to the arduino, then the serial.read() will always read it as '1' and perform what it is in the loop isn't ?

Please help.

Thanks !

I am curious because once I send '1 ' to the arduino, then the serial.read() will always read it as '1' and perform what it is in the loop isn't ?

No. If you send '1', then one character will become available. You read that character, and set the flag to say show this data on the LCD.

Then, you use the value read and show the data on the LCD. When you show the data, you clear the flag that says to show the data.

But, you keep using that value, on every pass through loop, as though you had received a new value.

You have two choices. First is to add state = 0; to the end of loop. If you do this, you can get rid of flag, as you will only be showing data when the data changes. The second is to move all the code into the if(Serial.available() > 0) block, so that you only read, use, and show the data when a new value arrives. If you do this, you can get rid of flag, as you will only be showing data when the data changes.

Note that whichever method you choose, flag is useless.

I thought the flag just to make sure the character are not printed few times on the LCD ?

Correct me if I am wrong.

Thanks !

I thought the flag just to make sure the character are not printed few times on the LCD ?

It does. And, that seems to be the crux of your problem. The code keeps on acting on the last value received. It simply stops telling you that it is doing that.

Okay, I will try out your suggestion.

Thank you . )