Kodeck

Lesson 1 of 7

First pixel

A small ship flies left and right along the bottom of the screen.

In this lesson

  • How the screen counts pixels: x from the left, y from the top
  • What setup() and loop() do
  • Variables: names for numbers the program remembers
  • Drawing shapes in colour

No controls in this lesson: change the code and watch the ship.

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.

First time? Setting up the Arduino IDE

The code, step by step

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 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.

int speed = 3;
uint16_t color = ORANGE;
int x = 150, dir = 1;

Variables

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(): once, at the start

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(): every frame

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.

0,0x 319y 239 x, 220x+10, 196x+20, 220
The screen is 320 pixels across and 240 down. The point 0, 0 is the top-left corner, and y grows downwards.

Try it yourself

  • Faster ship

    Make the ship twice as fast.

    Hint

    Change speed from 3 to 6.

  • Another colour

    Paint the ship cyan or yellow.

    Hint

    Replace ORANGE with CYAN or YELLOW. Kodeck also knows WHITE, RED, GREEN, PINK and PURPLE.

  • Up to the top

    Make the ship fly along the top of the screen.

    Hint

    The y values 220 and 196 are near the bottom. Try 40 for the two lower corners and 16 for the tip.

  • Leave a trail

    Delete the fillScreen line. What happens, and why?

    Hint

    Without it the old frames are never painted over, so every ship ever drawn stays on the screen.