Hello all,
I have been working on a robot that will follow someone at a set distance using a ultrasonic distance sensor. The code was working but i needed to add millis instead of delays and now the robot has stopped working. any help or suggestions would be appreciated!
here is my code:
#define echoPin 10 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 7 //attach pin D3 Arduino to pin Trig of HC-SR04
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
unsigned long previousmillis = 0;
const long period = 500;
void setup() {
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
}
//start direction functions
void forward()
{
//Motor A forward @ full speed
digitalWrite(12, HIGH); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at half speed
//Motor B forward @ full speed
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
}
void brake()
{
//Motor A stop
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, HIGH); //Disengage the Brake for Channel A
analogWrite(3, 0); //Spins the motor on Channel A at half speed
//Motor B stop
digitalWrite(13, LOW); //Establishes forward direction of Channel B
digitalWrite(8, HIGH); //Disengage the Brake for Channel B
analogWrite(11, 0); //Spins the motor on Channel B at full speed
}
void turnr()
{
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void turnl()
{
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
//start loop
void loop(){
// Clears the trigPin condition
digitalWrite(trigPin, LOW);
delayMicroseconds(1);
// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
// Displays the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
unsigned long currentMillis = millis();
if (distance >= 130){
brake();
if (currentMillis - previousmillis >= period)
turnr();
previousmillis = currentMillis;
if (currentMillis - previousmillis >= period)
brake();
previousmillis = currentMillis;
if (currentMillis - previousmillis >= period)
turnl();
previousmillis = currentMillis;
if (currentMillis - previousmillis >= period)
brake();
previousmillis = currentMillis;
{ }
}else{
forward();
}
if (distance <= 40){
brake();
if (currentMillis - previousmillis >= period)
turnr();
previousmillis = currentMillis;
if (currentMillis - previousmillis >= period)
brake();
previousmillis = currentMillis;
if (currentMillis - previousmillis >= period)
turnl();
previousmillis = currentMillis;
if (currentMillis - previousmillis >= period)
brake();
previousmillis = currentMillis;
{ }
}else{
forward();
}}
//end loop