Get Help
Drawing with Sparki
Introduction
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
- A Sparki.
- A compatible marker to be inserted on the robot. The Sparki Materials Pack includes the best markers that we know to do this job.
- A surface where Sparki can draw such as a large piece of paper.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <Sparki.h> // include the sparki library void setup() { for (int i=0; i<=3; i++) { sparki.moveForward(10.0); sparki.moveLeft(90.0); } sparki.moveStop(); } void loop() { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <Sparki.h> // include the sparki library void setup() { for (int i=0; i<=3; i++) { sparki.moveForward(); delay(1000); sparki.moveLeft(); delay(2018); // aprox. 90 degrees with the current speed } sparki.moveStop(); } void loop() { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <Sparki.h> // include the sparki library void setup() { for (int i=0; i<=1; i++) { sparki.moveForward(10.0); sparki.moveLeft(90.0); sparki.moveForward(20.0); sparki.moveLeft(90.0); } sparki.moveStop(); } void loop() { } |
Circles
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <Sparki.h> // include the sparki library void setup() { sparki.motorRotate(MOTOR_LEFT, DIR_CCW, 100); sparki.motorRotate(MOTOR_RIGHT, DIR_CW, 25); delay(22000); //give enough time to make the drawing sparki.motorStop(MOTOR_LEFT); sparki.motorStop(MOTOR_RIGHT); } void loop() { } |
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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <Sparki.h> // include the sparki library void setup() { for (int i=0; i<=90; i++) { sparki.clearLCD(); sparki.moveForward(1.0); delay(100); sparki.moveLeft(4.0); delay(100); sparki.println(i); sparki.updateLCD(); } sparki.moveStop(); } void loop() { } |
Spirals
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <Sparki.h> // include the sparki library void setup() { for (int i=0; i<=90; i++) { sparki.clearLCD(); sparki.moveForward(i); delay(100); sparki.moveLeft(30); delay(100); sparki.println(i); sparki.updateLCD(); } sparki.moveStop(); } void loop() { } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <Sparki.h> // include the sparki library void setup() { randomSeed(analogRead(0)); } void loop() { sparki.moveForward(random(5, 25)); sparki.moveLeft(random(30, 360)); delay(100); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <Sparki.h> // include the sparki library void setup() { randomSeed(analogRead(0)); } void loop() { int distance = random(5, 25); int angle = random(30, 360); sparki.clearLCD(); sparki.moveForward(distance); sparki.moveLeft(angle); sparki.print("distance = "); sparki.println(distance); sparki.print("angle = "); sparki.println(angle); sparki.updateLCD(); delay(100); } |