Now that you've mastered the basics of motor control and movement, it's time to take your BallyBot to the next level! In this lesson, we'll explore how to make your BallyBot perform tricks using combinations of movements and timing.
Prerequisites- Complete Lessons 1-3
- Familiarity with the Arduino IDE and basic programming concepts
To make your BallyBot perform tricks, you need to understand how to sequence movements and control timing. This involves using loops, delays, and functions to create complex behaviors.
Example Code: Sequencing MovementsIn this example will do a series of movements 1 time after being turned on. There are 2 core commands being used. First is digitalWrite
to power motors, and then delay
to set of long it will power at the written setting.
void setup() {
pinMode(14, OUTPUT); /* both wheel Backward */
pinMode(12, OUTPUT); /* right wheel Forward */
pinMode(2, OUTPUT); /* left wheel Forward */
digitalWrite(2, LOW); /* Go backwards */
digitalWrite(12, LOW);
digitalWrite(14, HIGH);
delay(250);
digitalWrite(2, LOW); /* Stop */
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(500);
digitalWrite(2, LOW); /* Go left */
digitalWrite(12, HIGH);
digitalWrite(14, LOW);
delay(250);
}
void loop() {
}
Step 2: Creating a looping RoutineLooping is used to make the Ballybot repeat actions from the beginning instead acting 1 single time. Using the previous example, all we need to do is move the write & delay commands to the loop function of our code.
void loop() {
digitalWrite(2, LOW); /* Go backwards */
digitalWrite(12, LOW);
digitalWrite(14, HIGH);
delay(250);
digitalWrite(2, LOW); /* Stop */
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(500);
digitalWrite(2, LOW); /* Go left */
digitalWrite(12, HIGH);
digitalWrite(14, LOW);
delay(250);
}
Step 3: Creating a Trick RoutineKnowing the core concepts, create a trick routine that makes your BallyBot perform a series of movements. You can use the example code above as a starting point and modify it to create your own unique routine.
Example Trick Routine: "BallyBot Dance"void setup() {
pinMode(14, OUTPUT); /* both wheel Backward */
pinMode(12, OUTPUT); /* right wheel Forward */
pinMode(2, OUTPUT); /* left wheel Forward */
}
void loop() {
digitalWrite(2, LOW); /* Go backwards */
digitalWrite(12, LOW);
digitalWrite(14, HIGH);
delay(250);
digitalWrite(2, LOW); /* Stop */
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(500);
digitalWrite(2, LOW); /* Go left */
digitalWrite(12, HIGH);
digitalWrite(14, LOW);
delay(250);
digitalWrite(2, LOW); /* Stop */
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(500);
digitalWrite(2, HIGH); /* Go right */
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(250);
digitalWrite(2, LOW); /* Stop */
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(500);
}
Step 4: Improving Code with FunctionsNotice above example, that it can be very repetitive. There is a repeating shape of commands like this:
digitalWrite(2, __on or off__);
digitalWrite(12, __on or off__);
digitalWrite(14, __on or off__);
delay(__movement time__);
If you can see a pattern like this in you code, it is a good sign that you are in need of functions! They will enable us to convert many lines of code, into a simple more useful command.
Here is an example of how a function will work:
void somefunction(inputType input){
command1();
command2(input);
command3();
}
To learn more about functions go here: https://www.w3schools.com/cpp/cpp_functions.asp
Now let's start in making our function for the movements of the BallyBot. We will start by replacing the placeholder name of the function somefunction
with another more useful name like move
:
void move(inputType input){
command1();
command2(input);
command3();
}
Then lets take the commands we are actually using instead of command_
:
void move(inputType input){
digitalWrite(2, LOW);
digitalWrite(12, LOW);
digitalWrite(14, LOW);
delay(500);
}
Lastly, we will connect inputs from the Function to the command. This works by pulling info from where you call the command and then using it inside of the function. So in this case we have 4 peices of info that can change. 3 I/O pin HIGH/LOW and the delay time. We will say the types if these inputs, where the 3 I/O pins are considered bool
that only hold an on or off value. Then we have an int
for the delay to represent any integer.
This is the function we will end up with:
void move(bool l, bool r, bool b, int time){
digitalWrite(2, l); /* left motor forward */
digitalWrite(12, r); /* right motor forward */
digitalWrite(14, b); /* both motors backward */
delay(time); /* time till next step */
}
And in full context here is another example for the BallyBot:
void setup() {
pinMode(14, OUTPUT); /* both wheel backward */
pinMode(12, OUTPUT); /* right wheel forward */
pinMode(2, OUTPUT); /* left wheel forward */
}
void loop() {
move(LOW,LOW,LOW, 3000); /* stay off */
move(HIGH,HIGH,LOW,1000); /* move forward */
move(LOW,LOW,HIGH, 500); /* jolt backward */
move(HIGH,HIGH,LOW,2000); /* go forward again to land */
}
void move(bool l, bool r, bool b, int time){
digitalWrite(2, l); /* left motor forward */
digitalWrite(12, r); /* right motor forward */
digitalWrite(14, b); /* both motors backward */
delay(time); /* time till next step */
}
Look how much more compact that is!
Step 5: Uploading and Testing the Trick RoutineUpload the trick routine code to your BallyBot and test it out! Make adjustments to the timing and sequencing as needed to perfect the routine.
ConclusionCongratulations! You've successfully created a trick routine for your BallyBot. With these new skills, you can create even more complex and impressive behaviors for your robot.
What's Next?In the next lesson, we'll explore advanced topics such as camera integration and connecting to WiFi.
Similar Tutorials and ResourcesNext Lesson: https://www.hackster.io/williamsokol0/lesson-5-hosting-a-hotspot-website-using-example-website-ecdc85
Previous Lesson: https://www.hackster.io/williamsokol0/lesson-3-programming-your-ballybot-to-move-4ac981
Comments
Please log in or sign up to comment.