So i am doing this object avoiding car project and for some reason it just doesn't avoid obstacles at all and i cant figure out why can someone please help.
The jest of the project is that i want the car to go right when its close to an object on the left and vice versa. i also want to display a stopwatch and distance traveled on my lcd screen
the servo is attached to the ultrasonic sensor to expand the area it covers i guess
here is the code i am using
`
[#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
//LCD
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
//Motor f/b
int forward = 9;
int backward = 10;
//motor L/R
int right = 5;
int left = 6;
int ena = 2;
Servo sensor;
int posi = 0;
#define trigPin 13
#define echoPin 12
void setup()
{
sensor.attach(4);
pinMode(forward, OUTPUT);
pinMode(backward, OUTPUT);
pinMode(right, OUTPUT);
pinMode(left, OUTPUT);
pinMode(ena,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin (9600);
lcd.begin(16, 2);
}
void gofor() {
digitalWrite(forward, HIGH);
digitalWrite(backward, LOW);
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, LOW);
}
void goback()
{
analogWrite(forward, 0);
analogWrite(backward, 127);
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, LOW);
}
void stopcar()
{
analogWrite(forward, 0);
analogWrite(backward, 0);
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, LOW);
}
void sharpl() {
digitalWrite(right, LOW);
digitalWrite(left, HIGH);
digitalWrite(ena, HIGH);
}
void sharpr() {
digitalWrite(right, HIGH);
digitalWrite(left, LOW);
digitalWrite(ena, HIGH);
}
void center() {
digitalWrite(right, LOW);
digitalWrite(left, LOW);
digitalWrite(ena, HIGH);
}
void sense()
{
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
lcd.print(distance);
lcd.print(" m");
if (10 > distance && posi == (180 || 360) ) {
Serial.println("left!!");
sharpl();
delay(750);
}
else if(10 > distance && posi == (45 || 0)){
Serial.println(" RIGHT!!");
sharpr();
delay(750);
}
else{
Serial.print(distance);
Serial.println(" cm");
}
if(0 == distance){
goback();
delay(2000);
}
delay(500);
}
void servo() {
sensor.write(180);
posi = 180;
sense();
sensor.write(135);
posi = 135;
sense();
sensor.write(90);
posi = 90;
sense();
sensor.write(45);
posi = 45;
sense();
sensor.write(0);
posi = 0;
sense();
sensor.write(45);
posi = 45;
sense();
sensor.write(90);
posi = 90;
sense();
sensor.write(135);
posi = 135;
sense();
}
void pattern() {
center();
gofor();
delay(20);
servo();
}
void loop() {
pattern();
lcd.setCursor(1, 1);
lcd.print(millis() / 3600000);
lcd.print("h:");
lcd.print(millis() / 60000);
lcd.print("m:");
lcd.print(millis() / 1000);
lcd.print("s");
}]
everything i guess works except for the avoidance part and by testing different codes i am sure everything is functional im sure the problem is with the code.