The library
The first line brings in the Kodeck library. After it, the program can use one object called kodeck: kodeck.screen for the picture, and later the stick, the buttons and the sound.
Lesson 1 of 7
A small ship flies left and right along the bottom of the screen.
In this lesson
A browser copy of the same program, written in JavaScript, so you can try it before uploading. On Kodeck the C++ code below runs.
On the console: in the Arduino IDE open File > Examples > Kodeck > 01_FirstPixel and press Upload.
The whole program, from top to bottom. The comment at the top of the file repeats what this page says. The file on GitHub
#include <Kodeck.h>The first line brings in the Kodeck library. After it, the program can use one object called kodeck: kodeck.screen for the picture, and later the stick, the buttons and the sound.
int speed = 3; uint16_t color = ORANGE; int x = 150, dir = 1;
A variable is a name for a value that the program remembers. int means a whole number.
speed is how many pixels the ship moves in one frame. color keeps a colour; colours have their own type, uint16_t. x is where the ship is across the screen, and dir is the direction: 1 to the right, -1 to the left.
void setup() {
kodeck.begin();
}setup() runs once, when the console switches on. kodeck.begin() starts the screen, the buttons, the stick and the sound. Every Kodeck program starts with this line.
void loop() { x = x + speed * dir; if (x < 0 || x > 300) dir = -dir; // at an edge: turn around kodeck.screen.fillScreen(BLACK); kodeck.screen.fillTriangle(x, 220, x + 10, 196, x + 20, 220, color); kodeck.show(); }
loop() runs again and again, about 30 times per second. Each run draws one frame.
First the ship moves: x = x + speed * dir. At an edge it turns around: when x is below 0 or above 300, dir changes sign. Why 300 and not 320? The ship is 20 pixels wide, so at x = 300 its right corner already touches the edge.
Then the picture: fillScreen(BLACK) paints the old frame black, and fillTriangle draws a triangle through three corners: (x, 220), (x + 10, 196) and (x + 20, 220). The middle corner is higher, so the ship points up. kodeck.show() sends the finished picture to the display.
Make the ship twice as fast.
Change speed from 3 to 6.
Paint the ship cyan or yellow.
Replace ORANGE with CYAN or YELLOW. Kodeck also knows WHITE, RED, GREEN, PINK and PURPLE.
Make the ship fly along the top of the screen.
The y values 220 and 196 are near the bottom. Try 40 for the two lower corners and 16 for the tip.
Delete the fillScreen line. What happens, and why?
Without it the old frames are never painted over, so every ship ever drawn stays on the screen.
Kodeck library and examples: MIT licence, source on GitHub. Lessons are in English for now; other languages will follow. Privacy