Kodeck

Lesson 3 of 7

Buttons and sound

The ship shoots: A fires, B sets off a bomb, Y switches the sound on and off.

In this lesson

  • pressed() and held(): one press or holding a button
  • Writing your own function
  • true and false: the bool type
  • Sound effects: beeps, slides and noise

Stick: drag the pad or use the arrow keys. Buttons: the A, B and Y keys.

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 > 03_ButtonsAndSound 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>

float shipX = 150;

The ship

The ship only moves left and right in this lesson, so one variable is enough.

// Up to 20 shots at a time
const int MAX_SHOTS = 20;
float shotX[MAX_SHOTS], shotY[MAX_SHOTS];
bool shotAlive[MAX_SHOTS];

Twenty shots in three arrays

Each shot has a place (shotX, shotY) and a state: shotAlive is true while the shot flies and false when its place is free. bool is the type for true and false.

void fire() {
  for (int i = 0; i < MAX_SHOTS; i++) {
    if (!shotAlive[i]) {  // the first free place
      shotX[i] = shipX + 9;
      shotY[i] = 196;
      shotAlive[i] = true;
      kodeck.sound.slide(1600, 700, 60);  // from 1600 Hz down to 700 Hz in 60 ms
      return;
    }
  }
}

Your own function

A function is a piece of code with a name. Every time the program says fire(), this code runs.

It looks for the first free place in the arrays, puts a new shot just above the ship and plays a sound that slides from 1600 Hz down to 700 Hz in 60 milliseconds. return leaves the function at once, so one call fires one shot. If all 20 shots are flying, nothing happens.

void setup() {
  kodeck.begin();
}

setup()

Nothing new here: start the console.

void loop() {
  shipX = constrain(shipX + kodeck.stick.x * 5 / 100.0, 0, 300);

  if (kodeck.pressed(BUTTON_A)) fire();
  if (kodeck.held(BUTTON_A) && kodeck.frame() % 6 == 0) fire();  // held: 5 shots per second

Pressed or held

kodeck.pressed(BUTTON_A) is true only in the one frame when the button goes down. kodeck.held(BUTTON_A) is true in every frame while it is held.

Held, the ship fires automatically: kodeck.frame() counts frames, and % 6 is the remainder after dividing by 6. It is 0 in every sixth frame, so at 30 frames per second that is 5 shots per second.

  if (kodeck.pressed(BUTTON_B)) {
    for (int i = 0; i < MAX_SHOTS; i++) shotAlive[i] = false;
    kodeck.sound.noise(300, 0.7);
  }

  if (kodeck.pressed(BUTTON_Y)) {
    kodeck.sound.mute(!kodeck.sound.muted());
    if (!kodeck.sound.muted()) kodeck.sound.beep(880, 80);
  }

Bomb and sound switch

B makes every place free at once and plays 300 milliseconds of noise at volume 0.7.

Y switches the sound: ! means “not”, so mute(!muted()) turns sound off when it is on and on when it is off. When the sound comes back, a short beep confirms it.

  kodeck.screen.fillScreen(BLACK);
  for (int i = 0; i < MAX_SHOTS; i++) {
    if (!shotAlive[i]) continue;
    shotY[i] = shotY[i] - 6;
    if (shotY[i] < 0) shotAlive[i] = false;
    kodeck.screen.fillRect(shotX[i], shotY[i], 2, 8, YELLOW);
  }
  kodeck.screen.fillTriangle(shipX, 220, shipX + 10, 196, shipX + 20, 220, ORANGE);

Moving and drawing the shots

One loop does both jobs: it moves each flying shot up by 6 pixels and draws it as a small yellow bar. continue skips free places. A shot that leaves the top of the screen frees its place.

  kodeck.screen.setTextColor(GREY);
  kodeck.screen.setCursor(4, 4);
  kodeck.screen.print("A shoot   B bomb   Y sound ");
  kodeck.screen.print(kodeck.sound.muted() ? "off" : "on");

  kodeck.show();
}

Text on the screen

print writes text at the cursor and moves the cursor on, so the second print continues the same line with “on” or “off”.

Try it yourself

  • Faster fire

    Make holding A fire 10 shots per second.

    Hint

    Fire every third frame: kodeck.frame() % 3 == 0.

  • Your own sound

    Give the shot a different sound.

    Hint

    Change the numbers in kodeck.sound.slide(1600, 700, 60). Try slide(300, 1200, 120) for a rising sound, or beep(1000, 30) for a click.

  • Only three shots

    Allow only three shots on the screen at a time.

    Hint

    Set MAX_SHOTS to 3.

  • Double shot

    Fire two shots side by side with every press.

    Hint

    Give fire() a parameter: void fire(int offset), and start the shot at shipX + offset. Then call fire(2) and fire(16).