Hi there,
I would like some help with my programing of an arduino uno, I'm pretty new to this and this project has got me confused ahah.
I'd like the code to read an arcade button's state and check the distance of an ultrasonic sensor. Then If that sensor's distance is less than 100cm I would the arduino to run the motors forward, while checking the distance. Once the distance is greater than 100cm I would like the motors to stop running. If no button state has changed then nothing will happen and if the first distance check results in a reading greater than 100cm then the motors should do nothing.
Unfortunately instead of this the code runs through correctly, starting the motor but doesnt stop it when the distance is greater that 100cm. I'm trying to achieve this with a "do" "while" function shown below.
I'm using a L298n dual motor driver with two 12VDC motors and a Ultrasonic sensor. All components work when run separately I just cant get this code to work correctly
Thank you all for your help and contributions
#include <L298N.h>
#define trigPin 5
#define echoPin 6
#define ButtonPin 7 //momentary switch
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// motor one
const int enA = 11;
const int in1 = 13;
const int in2 = 12;
//motor two
const int enB = 9;
const int in3 = 10;
const int in4 = 8;
int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration1, distance1, cm; // Duration used to calculate distance
void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ButtonPin,INPUT);
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin (9600);
}
void loop() {
buttonState = digitalRead(ButtonPin); //read the state of the button
if(digitalRead(ButtonPin) == HIGH) {
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
long duration1, distance1;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration1 = pulseIn(echoPin, HIGH);
distance1 = duration1 / 58; // distance in cm
Serial.print(distance1);
Serial.print("cm");
Serial.println();
if (distance1 < 100); {
do {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// set speed to 210 out of possible range 0~255
analogWrite(enA, 200);
// turn on motor B
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(enB, 200);
delay(50);
long duration1, distance1;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration1 = pulseIn(echoPin, HIGH);
distance1 = duration1 / 58; // distance in cm
Serial.print(distance1);
Serial.print("cm");
Serial.println();
}
while (distance1 < 100);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
} else {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}