So I am trying to put together a code for 4dc motors, buzzer, ultrasonic and lcd to work together,. they will together make up a small car. when the distance to the ultrasonic is smaller than 10cm, the buzzer makes a random sound, and a funny face. Meanwhile the motors will stop for 2 seconds, goes back for 2 seconds and do a turning motion for 8 seconds. However. I do not want to use the delay function for the motors since it will cause the buzzer to sound too slowly. I know I need to do a blink without delay code, I am trying to implement this but am having trouble right now: the motors will stop when I put in the motor stop function, but it will not go backwards.. I know there are some timing issues but how can I fix this? please see attached the code and wiring diagram.
Thank you sooo much !!!!
#include <LiquidCrystal.h>
#include <NewTone.h>
#include<AFMotor.h>
#define trigPin A1
#define echoPin A0
AF_DCMotor motor1(1,MOTOR34_1KHZ);
AF_DCMotor motor2(2,MOTOR34_1KHZ);
AF_DCMotor motor3(3,MOTOR34_1KHZ);
AF_DCMotor motor4(4,MOTOR34_1KHZ);
LiquidCrystal lcd(8, 9, 4, 7, 12, 13);
int buzzerPin = A2;
int notes[] = {262};
int dur = 100;
int maximum = 100;
long duration;
int distance;
int distanceUltra;
unsigned long previousBuzzerMillis = 0;
unsigned long previousDCStopMillis = 0;
unsigned long previousDCBackMillis = 0;
unsigned long currentMillis = 0;
const unsigned long DCStopInterval =2000;
const unsigned long DCBackInterval =2000;
byte a[]={
B10001,
B10001,
B10101,
B10101,
B01010,
B00000,
B00000,
B00000
};
byte b[]={
B00000,
B00010,
B00100,
B01000,
B01000,
B01000,
B01000,
B01000
};
byte c[]={
B00000,
B01000,
B01000,
B01000,
B01000,
B01000,
B00100,
B00010
};
byte h[]={
B00000,
B01000,
B00100,
B00010,
B00010,
B00010,
B00010,
B00010
};
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
lcd.createChar(0,a);
lcd.createChar(1,b);
lcd.createChar(2,h);
lcd.createChar(3,c);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
currentMillis = millis();
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
Serial.print("distance:");
Serial.println(distance);
lcd.clear();
currentMillis = millis();
if (distance <= 10){
stopDCMotorStop();
//stopDCMotorBack();
}
else{
motor1.setSpeed(100);
motor1.run(BACKWARD);
motor2.setSpeed(100);
motor2.run(BACKWARD);
motor3.setSpeed(100);
motor3.run(BACKWARD);
motor4.setSpeed(100);
motor4.run(BACKWARD);
}
if (distance <= 10){
lcd.setCursor(5,1); //set lcd faces
lcd.write((char) 3);
lcd.setCursor(5,0);
lcd.write((char) 1);
lcd.setCursor(9,1);
lcd.write((char) 201);
lcd.setCursor(9,0);
lcd.write(2);
lcd.setCursor(6,0);
lcd.write("x");
lcd.setCursor(8,0);
lcd.write("x");
lcd.setCursor(7,1);
lcd.write("o");
}
if (distance <= 10){
randSound(maximum);
}
}
void randSound(int maximum){
NewTone(buzzerPin, random(maximum, 10*maximum),dur);
delay(maximum);
noNewTone(buzzerPin);
}
void stopDCMotorStop(){
if (currentMillis-previousDCStopMillis<= DCStopInterval) {
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
motor3.setSpeed(0);
motor3.run(RELEASE);
motor4.setSpeed(0);
motor4.run(RELEASE);
previousDCStopMillis =currentMillis;
}
}
void stopDCMotorBack(){
if (currentMillis-previousDCBackMillis<= DCBackInterval) {
motor1.setSpeed(100);
motor1.run(FORWARD);
motor2.setSpeed(100);
motor2.run(FORWARD);
motor3.setSpeed(100);
motor3.run(FORWARD);
motor4.setSpeed(100);
motor4.run(FORWARD);
previousDCBackMillis =currentMillis;
}
}
