After using the Elegoo Robot Car 3 for several years, this year I upgraded to version 4 for a class I teach - and the code is a nightmare to teach with. So to teach with it I have to write example code more like the version 3 car.
So far I've done example code to read the mode button, control the onboard SPI RGB LED, and to drive the Left/Right motors.
Drop me a note if you'd like copies of the code and I'll get them to you.
It's a cool car, but their age 12+ example code can barely be understood by even seasoned Arduino programmers! Forget about modifying it to make the car do what a student wants to or learn autonomous programming from it.
/* Code to test the motors on the Elegoo Robot Car 4
Martin H.
Southern Illinois University, for Automotive Technologies
Download, disconnect cable, place on floor with room to drive (about 4 feet clearance in all directions)
Press the mode button, wait 2 seconds for it to begin
*/
// define 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
//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);
digitalWrite(STBY, HIGH); //Enable Motors to run
digitalWrite(PWMA, LOW); // Fully on
// digitalWrite(PWMA, HIGH); // Fully on
}
//main loop
void loop() {
while (digitalRead(modeSwitch) == 1) // wait for mode switch to be pressed (go to 0)
; // Do nothing
delay(2000);
// RIGHT WHEELS
// Drive Right Wheels forward for 2 second
digitalWrite(AIN, HIGH); // Forward direction
digitalWrite(PWMA, HIGH); // Full power
delay(2000); //delay 2000mS
// Stop for 2 seconds
stopTime(2000);
// Drive Right Wheels Backwards for 2 second
digitalWrite(AIN, LOW); // Backwards direction
digitalWrite(PWMA, HIGH); // Full power
delay(2000); //delay 2000mS
// Stop for 2 seconds
stopTime(2000);
// Turn off Right Motor's Power
digitalWrite(PWMA, LOW);
// DRIVE LEFT WHEELS
// Drive LEFT Wheels forward for 2 second
digitalWrite(BIN, HIGH); // Forward direction
digitalWrite(PWMB, HIGH); // Full power
delay(2000); //delay 2000mS
// Stop for 2 seconds
stopTime(2000);
// Drive Left Wheels Backwards for 2 second
digitalWrite(BIN, LOW); // Forward direction
digitalWrite(PWMB, HIGH); // Full power
delay(2000); //delay 2000mS
// Stop for 2 seconds
stopTime(2000);
// Drive car Forward for 1 second
digitalWrite(AIN, HIGH); // Forward direction on Right
digitalWrite(BIN, HIGH); // Forward direction on Left
digitalWrite(PWMA, HIGH); // Full power on Right
digitalWrite(PWMB, HIGH); // Full power on Left
delay(1000); //delay 2000mS
// stop for 2 seconds
stopTime(2000);
// Drive car Backwards for 1 second1
digitalWrite(AIN, LOW); // Reverse direction on Right
digitalWrite(BIN, LOW); // Reverse direction on Left
delay(1000);
// stop for 2 seconds
stopTime(2000);
// All motor power off
digitalWrite(PWMA, LOW); // No power on Right
digitalWrite(PWMB, LOW); // No power on Left
}
// Function - accepts the time in milli-Seconds to go into standby for
void stopTime(int mS){
digitalWrite(STBY, LOW); // Go into standby
delay(mS); // Wait defined time
digitalWrite(STBY, HIGH); // Come out of standby
}
Nice. That is a basic motor sketch, so, a great place to start. Do you have any questions about this sketch? I hope you test every part on its own... that will give you understanding of each part before assembling the entire kit... then modifying it however you want.
No, I've seen several posts on the board about people not being able to do a dang thing with the car besides download their very complex code and 'play', so I'M writing this new code for it to make it educational and offering it up to those who want it.
Their original code did NOT show use of each part, just very complex code with multiple files per example, so I'm writing code to demonstrate use of each component on it that others can use to learn and modify to their desires.
Feel free to spend your time making something available to others to help in education and do it the way you want to. I'm sure all would benefit from your knowledge.
Unfortunate. Make it better. That's always the goal. I like using WOKWI.COM to have a visual companion of the code/text. It has motors, servos, ultrasonic sensor, light sensor, logic gates... and plenty of examples (all projects are public unless tagged private). It doesn't have all the sensors, but you can insert a switch to be a vibration sensor.
Here is my example of a "obstacle avoidance robot" (LEDs are used as motors for easier viewing). Press the green "play" button, then click the ultrasonic sensor and slide the value around 20cm to see the movements (LEDs and servos). It has a random object left or right or neither or both.
xfpd,
Sorry I bet you wanted to see an example of their code for the car. Here's the main sketch with around 10 header files that went with it. Teaches a lot, huh?
/*
* @Author: ELEGOO
* @Date: 2019-10-22 11:59:09
* @LastEditTime: 2020-06-28 14:55:26
* @LastEditors: Changhua
* @Description: SmartRobot robot tank
* @FilePath:
*/
#include <avr/wdt.h>
#include "DeviceDriverSet_xxx0.h"
#include "ApplicationFunctionSet_xxx0.h"
void setup()
{
// put your setup code here, to run once:
Application_FunctionSet.ApplicationFunctionSet_Init();
}
void loop()
{
Application_FunctionSet.ApplicationFunctionSet_Obstacle();
}
Modular. If they need to move to new devices, they change a device and its module, and it still works.
There is a robot out there called "sloth" that is nicely "un-modular" where they have a keyword (like, DEBUG or SETUP) to allow you to test every part as you assemble it. When all parts are tested, you comment the keyword out. Very good work, 30 minutes video for all sections. Different ideas on how to be an educational product.
When the bots primary code is running, through the phone app you can monitor the camera, use virtual joysticks for control, change modes, etc.
A lot of the coding is to get around using delays and such for keep all the systems active.
Like I said, it’s cool, but definitely not educational for 12+. Once it’s built and played with they have no concept what to do with it. Hopefully my code can help them use it more and learn. Plus help my students understand.
If you’re curious to see their code and “tutorial”…
I’ve written books on programming, mainly for parallax, but a lot of arduino code as well, plus some software packages. and what they provide is an engineer’s wet dream. Im not even sure of some of the calls made.
Parallax? I got my first BS (one) then BS2 in February 2000. Had a blast! Then I went away for a while, and when I returned, computers didn't want to connect to it (even the Chrome ext.). I wrote to your Parallax forum when I couldn't get even the bare-bone BS1 to work. : ( I still have it. One day...
Elegoo video... any time I have to hear kpop-techno, I select "do not suggest this channel."
The Sunfounder Sloth tutorial is perfect: https://docs.sunfounder.com/projects/sloth-kit/en/latest/
And their video is as detailed as their assembly:
Fourteen minutes of calibrating the servos:
Anyway... if you have not been there, I suggest trying DroneBotWorkshop.com (web site and youtube). He produces detailed, reproducible, fun, essential, robot stuff. You could probably de-assemble the Elegoo thing and re-make into a functional kit with DroneBot steps.
I am a beginner. I just built the Smart Robot Car V4.0 with camera. I do not understand how to upload code. Would appreciate copy of the code mentioned, and instructions on how to upload it.
Steve Katz