We finally got the Elegoo Robot Car v4 to work with some of the easy code examples supplied by Martin Hebel (thanks so much Martin!). The original code was to navigate to an object and turn but my son just wants to make the car stop. We tried modifying the code but the car stops and then jerks a bit - presumably getting back into the loop. What do we need to change? Thanks for your help with this very basic query!
/*Drive_until_object
Uses Driving with Function
Martin Hebel, March 2023
Drives the car forward until the ultrasonic sensor detects an object, then stops
Be sure to add the library for the sensor:
Sketch--> Include Library --> Add Zip Library --> HC-SR04.zip
Place on floor, press mode switch.
*/
// Configure ultrasonic sensor
#include "SR04.h"
#define TRIG_PIN 13
#define ECHO_PIN 12
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); // create an instance of the ultrasonic class
// define Motor IO pin
#define PWMA 5 // Controls power to right motor
#define PWMB 6 // Controls power to left motor
#define AIN 7 // Controls direction of right motor, HIGH = FORWARD, LOW = REVERSE
#define BIN 8 // Controls direction of right motor, HIGH = FORWARD, LOW = REVERSE
#define STBY 3 // Place H-Bridge in standby if LOW, Run if HIGH
#define modeSwitch 2 // Mode Switch input
// Define variables
int speedLeft = 0; // holds the speed of left wheels
int speedRight = 0; // holds the speed of right wheels
long distance; // Holds distance from sensor
//init the car
void setup() {
pinMode(PWMA, OUTPUT); //set IO pin mode OUTPUT
pinMode(PWMB, OUTPUT);
pinMode(BIN, OUTPUT);
pinMode(AIN, OUTPUT);
pinMode(STBY, OUTPUT);
analogWrite(PWMA, 0); // Fully off for Right
analogWrite(PWMB, 0); // Fully off for Left
digitalWrite(STBY, HIGH); //Enable Motors to run
waitForStart(); // wait until mode is pressed
setSpeeds(100, 100); // sets moderate speed
}
//main loop to drive forward until an object is detected
void loop() {
forward(); // drive forward
distance=sr04.Distance(); // Get and store the distance from the device driver
if (distance < 40) { // if too close, stop, reverse and turn
stop(); delay(500);
}
}
void waitForStart(){
while (digitalRead(modeSwitch) == 1) // wait for mode switch to be pressed (go to 0)
; // Do nothing
delay(2000); // delay 2 seconds before starting
}
void setSpeeds(int leftSpeed, int rightSpeed){ // stores speeds in variables to be used when driving
speedLeft = leftSpeed;
speedRight = rightSpeed;
}