In this tutorial, we'll explore how to control the wheels of your BallyBot using code. This lesson builds upon the previous tutorial, Programming a BallyBot: A Step-by-Step Tutorial.
Prerequisites- Complete the previous tutorial, Programming a BallyBot: A Step-by-Step Tutorial
- Familiarity with the Arduino IDE and basic programming concepts
- BallyBot robot
- USB C cable
- Computer
- Arduino IDE (version 1.8.x or later)
Based on the last lesson we now understand how to light the BallyBot's LED using a modified blink example:
void setup() {
/* initialize digital pin LED_BUILTIN as an output. */
pinMode(4, OUTPUT);
}
/* the loop function runs over and over again forever */
void loop() {
digitalWrite(4, HIGH); /* turn the LED on (HIGH is the voltage level)*/
delay(1000); /* wait for a second */
digitalWrite(4, LOW); /* turn the LED off by making the voltage LOW */
delay(1000); /* wait for a second */
}
Our next goal is to move a motor with code. This is very similar to using an LED, mainly the difference is in changing the pins that the esp32-cam powers.
So for the BallyBot, we will be using the 14, 15, and 18 pins to power the motors.
To use a motor pin, edit your blink example like this. Once you've uploaded the code, observe how it moves the left motor:
void setup() {
pinMode(14, OUTPUT);
}
void loop() {
digitalWrite(14, HIGH);
delay(1000);
digitalWrite(14, LOW);
delay(1000);
}
Turning data pins HIGH & LOW in code can be thought of like connecting and disconnecting power to a motor. That way it makes sense for a powered motor move, an unpowered to be still, and swaping PWR and GND to reverse the direction of the motor.
If you are not yet familiar with manually powering motors, you can learn more about how a dc motor works from this video: https://www.youtube.com/watch?v=1AaUK6pT_cE
Step 2: Writing the Code to Control the WheelsNow that we know how to move one motor, let's make the full BallyBot move!
The first thing to note is that the ballybot is based on a 3 I/O pin control scheme.
So, to go forward we need to power both forward pins, in this case 14 & 15:
digitalWrite(14, HIGH); /* left wheel forward */
digitalWrite(17, HIGH); // right wheel forward
We can go Backward with:
digitalWrite(14, LOW); /* left wheel forward */
digitalWrite(15, HIGH); /* both wheels backward */
digitalWrite(17, LOW); /* right wheel forward */
And stop with:
digitalWrite(14, LOW); /* left wheel forward */
digitalWrite(15, LOW); /* both wheels backward */
digitalWrite(17, LOW); /* right wheel forward */
You should also note that if both the forward and backward pins for a wheel are HIGH, they will cancel out, and no signal gets sent. This could theoretically be used to stop (NOT RECOMMENDED)
digitalWrite(14, HIGH); /* left wheel forward */
digitalWrite(15, HIGH); /* both wheels backward */
digitalWrite(17, HIGH); /* right wheel forward */
Step 3: Uploading the Code to the BallyBotIn case you forgot how to upload from the last lesson, ensure your FTDI programmer is plugged into the BallyBot, and the Arduino IDE has the correct board selected.
Troubleshooting tips for upload issues:
- Make sure USB wire can send data
- BallyBot should be set in Upload then turned off->on again
This is what the core of your code should look like. Try and change it around to see what happens!
/* motor pins are 14,15,16 */
void setup() {
pinMode(14, OUTPUT);
}
void loop() {
digitalWrite(14, HIGH);
delay(1000);
digitalWrite(14, LOW);
delay(1000);
}
ConclusionNow that you understand the basics of motor movements and using I/O pins, we'll now look further at controlling motors with combinations of movements and timing. Let's get your BallyBot to do tricks!
In the next lesson we will learn how to make the BallyBot do a somersault trick automatically, see if you can get there with what you already know!
Next Lesson: https://www.hackster.io/williamsokol0/lesson-4-making-your-ballybot-do-tricks-76d2f9
Previous Lesson: https://www.hackster.io/williamsokol0/programming-a-ballybot-a-step-by-step-tutorial-947ed5
Comments
Please log in or sign up to comment.