Hi,
The Arduino control CAR from: Obstacle Avoiding Car
https://create.arduino.cc/projecthub/adam/obstacle-avoiding-car-a192d9
running well by Ultrasonic scan + CAR moving alternately.
I try to modified it for Ultrasonic scan and CAR moving together and added some millis, seems not work as excepted, why?
Thanks for help please.
Adam
Original sketch:
#include <Servo.h> //standard library for the servo
#include <NewPing.h> //for the Ultrasonic sensor function library.
//L298N motor control pins
const int LeftMotorForward = 6;
const int LeftMotorBackward = 7;
const int RightMotorForward = 5;
const int RightMotorBackward = 4;
//sensor pins
#define trig_pin A1 //analog input 1
#define echo_pin A2 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor;
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
servo_motor.attach(9); //our servo pin
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
}
void loop(){
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 20){
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
distanceRight = lookRight();
delay(300);
distanceLeft = lookLeft();
delay(300);
if (distance >= distanceLeft){
turnRight();
moveStop();
}
else{
turnLeft();
moveStop();
}
}
else{
moveForward();
}
distance = readPing();
}
int lookRight(){
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft(){
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
int readPing(){
delay(70);
int cm = sonar.ping_cm();
if (cm==0){
cm=250;
}
return cm;
}
void moveStop(){
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward(){
if(!goesForward){
goesForward=true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward(){
goesForward=false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight(){
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft(){
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
modified sketch:
#include <Servo.h> //standard library for the servo
#include <NewPing.h> //for the Ultrasonic sensor function library.
//L298N motor control pins
const int LeftMotorForward = 10;
const int LeftMotorBackward = 11;
const int RightMotorForward = 12;
const int RightMotorBackward = 44;
//sensor pins
#define trig_pin 23 //analog input 1
#define echo_pin 22 //analog input 2
#define maximum_distance 200
boolean goesForward = false;
int distance = 100;
NewPing sonar(trig_pin, echo_pin, maximum_distance); //sensor function
Servo servo_motor;
int Tadj = 600;
///////////////////////////////// add millis 20220410
unsigned long READstartMillis; //some global variables available anywhere in the program
unsigned long READcurrentMillis;
const unsigned long READperiod = Tadj; //the value is a number of milliseconds
unsigned long lookLeftstartMillis; //some global variables available anywhere in the program
unsigned long lookLeftcurrentMillis;
const unsigned long lookLeftperiod = Tadj; //the value is a number of milliseconds
unsigned long lookRightstartMillis; //some global variables available anywhere in the program
unsigned long lookRightcurrentMillis;
const unsigned long lookRightperiod = Tadj;
unsigned long turnRightstartMillis; //some global variables available anywhere in the program
unsigned long turnRightcurrentMillis;
const unsigned long turnRightperiod = Tadj;
unsigned long turnLeftstartMillis; //some global variables available anywhere in the program
unsigned long turnLeftcurrentMillis;
const unsigned long turnLeftperiod = Tadj;
unsigned long moveForwardstartMillis; //some global variables available anywhere in the program
unsigned long moveForwardcurrentMillis;
const unsigned long moveForwardperiod = Tadj;
///////////////////////////////// add millis 20220410
void setup() {
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
servo_motor.attach(14); //our servo pin
servo_motor.write(115);
delay(2000);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
distance = readPing();
delay(100);
READstartMillis = millis();
lookLeftstartMillis = millis();
lookRightstartMillis = millis();
turnRightstartMillis = millis();
turnLeftstartMillis = millis();
moveForwardstartMillis = millis();
}
void loop() {
int distanceRight = 0;
int distanceLeft = 0;
delay(50);
if (distance <= 20) {
moveStop();
delay(300);
moveBackward();
delay(400);
moveStop();
delay(300);
/////////////////////////////////////////////////////////////////////////////////
lookRightcurrentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (lookRightcurrentMillis - lookRightstartMillis >= lookRightperiod) //test whether the period has elapsed
{
distanceRight = lookRight();
lookRightstartMillis = lookRightcurrentMillis; //IMPORTANT to save the start time of the current LED state.
}
/////////////////////////////////////////////////////////////////////////////
// distanceRight = lookRight();
// delay(300);
/////////////////////////////////////////////////////////////////////////////////
lookLeftcurrentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (lookLeftcurrentMillis - lookLeftstartMillis >= lookLeftperiod) //test whether the period has elapsed
{
distanceLeft = lookLeft();
lookLeftstartMillis = lookLeftcurrentMillis; //IMPORTANT to save the start time of the current LED state.
}
/////////////////////////////////////////////////////////////////////////////
// distanceLeft = lookLeft();
// delay(300);
if (distance >= distanceLeft) {
/////////////////////////////////////////////////////////////////////////////////
turnRightcurrentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (turnRightcurrentMillis - turnRightstartMillis >= lookLeftperiod) //test whether the period has elapsed
{
turnRight();
turnRightstartMillis = turnRightcurrentMillis; //IMPORTANT to save the start time of the current LED state.
}
/////////////////////////////////////////////////////////////////////////////
// turnRight();
moveStop();
}
else {
/////////////////////////////////////////////////////////////////////////////////
turnLeftcurrentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (turnLeftcurrentMillis - turnLeftstartMillis >= lookLeftperiod) //test whether the period has elapsed
{
turnLeft();
turnLeftstartMillis = turnLeftcurrentMillis; //IMPORTANT to save the start time of the current LED state.
}
/////////////////////////////////////////////////////////////////////////////
// turnLeft();
moveStop();
}
}
else {
/////////////////////////////////////////////////////////////////////////////////
moveForwardcurrentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (moveForwardcurrentMillis - moveForwardstartMillis >= lookLeftperiod) //test whether the period has elapsed
{
moveForward();
moveForwardstartMillis = moveForwardcurrentMillis; //IMPORTANT to save the start time of the current LED state.
}
/////////////////////////////////////////////////////////////////////////////
//moveForward();
}
distance = readPing();
}
int lookRight() {
servo_motor.write(50);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
}
int lookLeft() {
servo_motor.write(170);
delay(500);
int distance = readPing();
delay(100);
servo_motor.write(115);
return distance;
delay(100);
}
int readPing() {
delay(70);
int cm = sonar.ping_cm();
if (cm == 0) {
cm = 250;
}
return cm;
}
void moveStop() {
digitalWrite(RightMotorForward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
void moveForward() {
if (!goesForward) {
goesForward = true;
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
}
void moveBackward() {
goesForward = false;
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorForward, LOW);
}
void turnRight() {
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorForward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}
void turnLeft() {
digitalWrite(LeftMotorBackward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
delay(500);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(RightMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(RightMotorBackward, LOW);
}