hey Nicola,
thank you, this is very helpful, however, I tried to implement it and I havnt been able to make it work.. so right now. the motors does not work at all... can you help me to look at my code?
Thank you..
#define HALT_TIME 2000
#define BACK_TIME 2000
#define TURN_TIME 8000
#define trigPin A1
#define echoPin A0
#include <LiquidCrystal.h>
#include <NewTone.h>
#include<AFMotor.h>
long duration;
int distance;
int distanceUltra;
int maximum = 100;
int buzzerPin = A2;
int dur = 100;
LiquidCrystal lcd(8, 9, 4, 7, 12, 13);
AF_DCMotor motor1(1,MOTOR34_1KHZ);
AF_DCMotor motor2(2,MOTOR34_1KHZ);
AF_DCMotor motor3(3,MOTOR34_1KHZ);
AF_DCMotor motor4(4,MOTOR34_1KHZ);
enum ApplicationState {
Drive,
Halt,
Back,
Turn
};
static enum ApplicationState currentApplicationState = Drive;
static unsigned long t = 0;
static bool sensorTriggered() {
// TODO return true if too close, return false otherwise.
if (distance <= 10){return true;}
else {return false;}
}
static void funnyFace() {
// TODO funny face and buzzer
lcd.setCursor(6,0);
lcd.write("x");
lcd.setCursor(8,0);
lcd.write("x");
lcd.setCursor(7,1);
lcd.write("o");
randSound(maximum);
}
static void driveHandler() {
// TODO do whatever the car is supposed to do normally
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 (sensorTriggered()) {
t = millis();
currentApplicationState = Halt;
}
}
static void haltHandler() {
static bool shouldShowFunnyFace = true;
// TODO stop the motors
motor1.setSpeed(0);
motor1.run(RELEASE);
motor2.setSpeed(0);
motor2.run(RELEASE);
motor3.setSpeed(0);
motor3.run(RELEASE);
motor4.setSpeed(0);
motor4.run(RELEASE);
if (shouldShowFunnyFace) {
funnyFace();
shouldShowFunnyFace = false;
}
if (millis() - t > HALT_TIME) {
t = millis();
currentApplicationState = Back;
shouldShowFunnyFace = true;
}
}
static void backHandler() {
// TODO drive backwards
motor1.setSpeed(100);
motor1.run(FORWARD);
motor2.setSpeed(100);
motor2.run(FORWARD);
motor3.setSpeed(100);
motor3.run(FORWARD);
motor4.setSpeed(100);
motor4.run(FORWARD);
if (millis() - t > BACK_TIME) {
t = millis();
currentApplicationState = Turn;
}
}
static void turnHandler() {
// TODO turn the car
motor1.setSpeed(100);
motor1.run(FORWARD);
motor2.setSpeed(100);
motor2.run(FORWARD);
motor3.setSpeed(100);
motor3.run(BACKWARD);
motor4.setSpeed(100);
motor4.run(BACKWARD);
if (millis() - t > TURN_TIME) {
currentApplicationState = Drive;
}
}
void setup() {
Serial.begin(9600);
lcd.begin(16,2);
pinMode (trigPin, OUTPUT);
pinMode (echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
Serial.print("distance:");
Serial.println(distance);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Serial.println(sensorTriggered());
duration = pulseIn(echoPin, HIGH);
distance = duration*0.034/2;
switch (currentApplicationState) {
case Drive:
driveHandler();
break;
case Halt:
haltHandler();
break;
case Back:
backHandler();
break;
case Turn:
turnHandler();
break;
}
}
void randSound(int maximum){
NewTone(buzzerPin, random(maximum, 10*maximum),dur);
delay(maximum);
noNewTone(buzzerPin);
}