i have made bluetooth control arduino car by using arduino uno + L2938 motor shield and it was working fine ...then i came up with the idea of using ultrasonic sensor with lcd to find distance ....when i merged both programmes i end up only running lcd with distance displaying ......but my car is not working ..plsss help ......here is the code----
//Distance sensor
#define trigPin 12
#define echoPin 8
//Flashing LED on Arduino board
#define LEDPin 13
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <AFMotor.h> //Adafruit Motor Shield Library. First you must download and install AFMotor library
#include <Servo.h> //Servo library. This is standard library. (Sketch -> Include Library -> Servo)
String voice;
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
AF_DCMotor motor1 (1, MOTOR12_8KHZ); //create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor2 (2, MOTOR12_8KHZ); //create motor #2 using M2 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor3(3, MOTOR12_8KHZ); //create motor #1 using M1 output on Motor Drive Shield, set to 1kHz PWM frequency
AF_DCMotor motor4 (4, MOTOR12_8KHZ); //create motor #2 using M2 output on Motor Drive Shield, set to 1kHz PWM frequency
Servo myServo; //define servo name
void forward_car();
void back_car();
void right_car();
void stop_car ();
void left_car();
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT); //The transmit pin of the ultrasonic sensor
pinMode(echoPin, INPUT); //The receive pin of the ultrasonic sensor
pinMode(LEDPin, OUTPUT); //The LED of the Arduino
lcd.begin (20,4); //Size of LCD
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home
Serial.begin(9600); //start serial communication
myServo.attach(10); //define our servo pin (the motor shield servo1 input = digital pin 10)
myServo.write(90); //servo position is 90 degrees
}
void loop(){
while (Serial.available())
{ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
if (c == '#') {break;} //Exit the loop when the # is detected after the word
voice += c; //Shorthand for voice = voice + c
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance =(duration/2) / 29.1;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Distance from OB");
lcd.setCursor(0,1);
lcd.print(distance);
lcd.print("cm");
if (distance >=10)
{
lcd.setCursor(0,4);
lcd.print("Safe Zone :)");
digitalWrite(LEDPin,HIGH);
delay(500);
digitalWrite(LEDPin,LOW);
delay(500);
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" door ho ja !!!");
lcd.setCursor(0,1);
lcd.print(" STEP AWAY!!!");
lcd.setCursor(0,2);
lcd.print(" STEP AWAY!!!");
lcd.setCursor(0,3);
lcd.print(" STEP AWAY!!!");
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
delay(50);
digitalWrite(LEDPin,HIGH);
delay(50);
digitalWrite(LEDPin,LOW);
}
if (voice.length() > 0){
if(voice == "*go ahead")
forward_car();
}
else if(voice == "*go back"){
back_car();
}
else if(voice == "*turn right") {
right_car();
}
else if(voice == "*turn left") {
left_car();
}
//else if(voice == "*turn on light") {
//LED_on();
//}
//else if(voice == "*turn off light") {
//LED_off();
//}
else if(voice == "*stop") {
stop_car();
}
voice=""; //Reset the variable after initiating
}
}
void forward_car()
{
motor1.run(BACKWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(3000);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void back_car()
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(3000);
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void right_car()
{
myServo.write(0);
delay(1000);
myServo.write(90);
delay(1000);
motor3.run(FORWARD);
motor3.setSpeed(170);
motor4.run(FORWARD);
motor4.setSpeed(170);
delay(1000);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void left_car()
{
myServo.write(180);
delay(1000);
myServo.write(90);
delay(1000);
motor1.run(BACKWARD);
motor1.setSpeed(170);
motor2.run(FORWARD);
motor2.setSpeed(170);
delay(1000);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
void stop_car ()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}