Welcome to the World of Coding!
In this lesson we’ll cover the basics that you need to know to get started with programming Sparki. This is a great place to start if you’ve never written any code before. Here’s what we’ll be covering on this page: Super Basic- Empty Code that is Needed in Every Program for Sparki This is the basic code that all Sparki code starts with:
1 2 3 4 5 6 7 8 9 10 11 |
#include <Sparki.h> // include the sparki library void setup() // code inside these brackets runs first, and only once { } void loop() // code inside these brackets runs over and over forever { } |
On its own this code will not actually make Sparki do anything, but it’s necessary for this code to be present for Sparki to do anything.
Sections of Code
Include Sparki Library
1 |
#include <Sparki.h> // include the sparki library |
1 2 |
sparki.moveForward(); //example of a movement function found in the sparki library |
1 |
hexy.LF #Left Front |
Setup: Starting Up the Robot
All of the code needed to get the robot started lives inside of the curly brackets after the word “setup”. The code inside these brackets will be the first code to run when the robot is turned on. This code will only run once and it’s the only portion of code that the robot will only try to run once. You can think of this portion of the code as your morning routine when you wake up in the morning or stretching before playing an athletic game. While your day or sport will probably have things that repeat over and over again (walking to and from classes, breathing, placing one foot in front of the other, running up the field and back, etc.) you only do your morning routine or stretch once. That’s because you only need to get ready for your day or game once. Sure, sometimes you may have a really long and complicated morning routine or somedays you may just throw on clothing and run out of the door, but you always have some sort of “setup” routine for your day.setup( ) happens exactly once when you turn on your robot and it gets the robot ready for the rest of the code- kind of like when you brush your teeth in the morning to get ready for the rest of your day
Just like your morning routine changes there may be a bunch of different things inside of setup depending on what you want the robot to do. It may contain commands to start communication, code to make the robot move around or calibrate sensors.
Loop: Repeating Code Over and Over and Over and Over and Over…..
The code inside of the curly brackets after the word “loop” will start after the setup function has run. Code inside these brackets will loop forever until the robot gets turned off or the batteries run out. That’s right, once the robot gets to the bottom of the loop code it goes right back up to the top and starts running it all over again.
1 2 3 4 5 6 7 |
void loop() // code inside these brackets runs over and over forever { //it starts here (and restarts here and restarts here and restarts here) sparki.moveForward(); //then the robot does this command second sparki.moveRight(); //then the robot does this command third sparki.moveLeft(); //fourth sparki.moveBackward(); //fifth } //the robot goes back to the top of the loop once it reaches here |
- For loops– Useful for making code execute for a certain number of times or making code execute until an input has happened a certain number of times
- While loops– Useful for making code execute while a variable (or multiple variables) is equal to a certain value or set of values
- Switch/Case statements– Useful for making different things happen depending on the value of a variable. Switch/Case statements will also only execute once each time through the main loop