Drawing with Sparki

Sparki's quite the artist! Teach it how to draw.


Lessons You Should Know


Sparki has a centered hole specially designed to hold markers. This will let us create drawings with the robot by moving it over a suitable surface. If you are not familiar yet with the instructions to move Sparki, it’s highly recommended that you review the Moving the Robot lesson first.

What You’ll Need

Warning_Triangle If you are going to select a marker by yourself, please note that when the marker is inserted into the hole, it should hit the paper without being able to be moved much side to side. If the marker is too small for the hole, it will drift from side to side when Sparki moves from one side to another. Warning_Triangle Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the motors here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki.  

Squares and Rectangles

Some very simples shape to draw are squares and rectangles. We just need to program Sparki to go forward for some distance, then rotate 90 degrees, and go forward again. To do it without writing the same “go forward and rotate” code four times, we will use a for cycle: Another way of doing this is by using the moveForward and moveLeft instructions without any parameter, and then waiting for the robot to move with a delay. Here is a code snippet doing exactly this, but it’s just to show the concept since the previous method is conceptually clearer and shorter: It’s worth noting that the complete drawing is done in the setup function, instead of doing it in the loop. This is because we want the square to be drawn just once. Finally, to draw a rectangle instead of a square, what we need to do is to advance Sparki with different longitudes. However, this should be done 2 times instead of 4 in the for cycle:


Although the circle is among the simplest shapes to draw with Sparki, there are different ways of doing that. One possible way is to move a wheel faster than the other. The bigger the speed difference between the wheels is, the smaller the resulting circle will be: So to do this, we need to control the speeds of the motors individually using the sparki.motorRotate  (and of course the sparki.motorStop) instructions:

Aside from the motor speeds (100 and 25 in the previous code), the other very important parameter here is the time that we want the motors to be moving. That’s defined in the amount of milliseconds passed to the delay function. This delay time is determined experimenting with Sparki.

Another way of drawing a circle is by repeatedly rotating the robot just a few degrees and then advancing small distances. This is one of the most used techniques when working with turtle geometry. The basic circle can be drawn by rotating 1 degree and advancing 1 cm (for example) 360 times. If Sparki advances a longer distance in each iteration, the circle is just bigger. But this will take a lot of time (360 iterations!), so we can lower the resolution of the circle, repeating it just for 90 iterations (you can try different numbers!) and thus rotating the robot 4 degrees on each step (since 360 / 4 is 90). Here is an example (please note that we have added some display printing to know the iteration number): The delays in the previous code are to make the movements smoother. Extra question: What do you think should be modified in order to draw a semicircle instead of a circle?  


A variation from the circle program lets us draw spirals with Sparki: To do that, instead of advancing a fixed distance, we need to increment that distance on each iteration. The easiest way of doing that is just by passing the i variable as the parameter for the sparki.moveForward instruction:  

Random Drawing

What if we want to create some modern art with our Sparki? Let’s modify the previous examples a bit so the robot can surprise as. We will make use of the random function: Note: The complete code for the final version of this program is available at the end of this section. As you can see, the program is pretty simple. And without adding much complexity, we can add a few lines of code to see the numbers generated by the random function in the LCD display: A few final comments about this example: we have used the randomSeed function in the setup. This is to initialize the generation of (pseudo) random numbers with a different number each time you turn on the Sparki. But please don’t worry if you don’t understand it right now, since it’s not really important for our drawing activities here.