Colours and brush
The ten colours are in an array, PALETTE. BAR is the height of the colour bar at the top. lastX and lastY remember where the finger was in the previous frame; -1 means “nowhere”.
Lesson 7 of 7
Draw with a finger. Pick a colour at the top, draw below, change the brush with L and R.
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 > 07_TouchDraw 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> const uint16_t PALETTE[] = {WHITE, ORANGE, YELLOW, GREEN, CYAN, BLUE, PURPLE, PINK, RED, BLACK}; const int COLOURS = 10; const int BAR = 28; // height of the colour bar at the top uint16_t color = ORANGE; int brush = 4; int lastX = -1, lastY = -1; // where the finger was in the previous frame
The ten colours are in an array, PALETTE. BAR is the height of the colour bar at the top. lastX and lastY remember where the finger was in the previous frame; -1 means “nowhere”.
void drawBar() { int w = 320 / COLOURS; for (int i = 0; i < COLOURS; i++) { kodeck.screen.fillRect(i * w, 0, w, BAR, PALETTE[i]); if (PALETTE[i] == color) kodeck.screen.drawRect(i * w + 2, 2, w - 4, BAR - 4, i == 0 ? BLACK : WHITE); } kodeck.screen.drawFastHLine(0, BAR, 320, DARKGREY); }
The bar is ten rectangles, each 32 pixels wide. The chosen colour gets a frame: black around white, white around the rest.
void clearPicture() {
kodeck.screen.fillScreen(BLACK);
kodeck.sound.slide(400, 1200, 120);
}Paint everything black and play a short rising sound.
void setup() { kodeck.begin(); clearPicture(); if (!kodeck.touch.ok()) { kodeck.screen.setTextColor(RED); kodeck.screen.drawString("No touch controller found", 60, 110); } }
kodeck.touch.ok() is true when the console found the touch controller. If not, the program says so on the screen instead of quietly doing nothing.
void loop() { if (kodeck.pressed(BUTTON_B)) clearPicture(); if (kodeck.pressed(BUTTON_L) && brush > 1) brush--; if (kodeck.pressed(BUTTON_R) && brush < 16) brush++;
B clears the picture. L and R make the brush thinner and thicker, between 1 and 16 pixels.
if (kodeck.touch.touched()) { int x = kodeck.touch.x, y = kodeck.touch.y; if (y < BAR) { color = PALETTE[x * COLOURS / 320]; // which colour is under the finger lastX = -1; } else { if (lastX >= 0) { // a thick line from the previous point: no gaps when the finger moves fast kodeck.screen.drawWideLine(lastX, lastY, x, y, brush, color); } kodeck.screen.fillCircle(x, y, brush / 2, color); lastX = x; lastY = y; } } else { lastX = -1; // finger lifted: the next line starts fresh }
kodeck.touch.x and kodeck.touch.y are the finger's position in screen pixels.
On the bar, the position chooses the colour: x * COLOURS / 320 turns 0 to 319 pixels into colour number 0 to 9.
Below the bar, a thick line joins the previous point to the new one. A finger can move 20 pixels in one frame; without the line the drawing would be a row of dots. When the finger lifts, lastX = -1 makes the next line start fresh.
drawBar();
kodeck.screen.fillRect(286, BAR + 4, 30, 30, BLACK);
kodeck.screen.fillCircle(301, BAR + 19, brush / 2 + 1, color); // brush preview
kodeck.show();
}Unlike the earlier lessons, this loop() never calls fillScreen: the picture stays from frame to frame. Only the bar and the brush preview in the corner are drawn again every time.
Find the eraser that is already there.
The last colour in the bar is BLACK, the same as the background. Drawing with it erases.
Make the colour change by itself while drawing.
In the touch block, before drawing: color = PALETTE[kodeck.frame() % 9];
Draw with two fingers at once.
kodeck.touch.count is 2 when two fingers touch the screen; the second one is at kodeck.touch.x2, kodeck.touch.y2. Draw a circle there too.
Kodeck library and examples: MIT licence, source on GitHub. Lessons are in English for now; other languages will follow. Privacy