TinkerBlock UNO kit/Lights and buttons/04. Lab: count the presses
Lights and buttons · 04 of 21

Lab: count the presses

The push button on D7 and the XL LED on D3. Every press is counted once, printed, and steps the LED up one of five brightness levels; the fifth press wraps it back to dark. A debounce makes one press count once, and a remainder turns a count that never stops into a level that goes round.

Two modules and three ideas this lab does not re-teach: the button's pull-down and bounce are in the TK04's own book, the debounce in debouncing and millis() instead of delay(), and analogWrite in fade an LED.

Where they plug in

ModuleSocketPin
TK04 push buttonD6/D7D7
TK01 XL LEDD2/D3D3

The button reads HIGH while it is down. That is the TK04's own 10 kΩ pull-down at work, so the sketch uses INPUT and tests for HIGH; the button's red lamp lights with every press, before any code is involved.

A count that goes round

Five presses, five steps, then round again
press 0
Presses so far0
presses
0
presses % 5
0
level
0
No presses yet: level 0, the LED is dark. The count keeps climbing in the Serial Monitor; only the LED wraps round.

The count only ever climbs; the LED has five levels. presses % 5 is the remainder after dividing by five, so it runs 1, 2, 3, 4, 0, 1, 2… and picks the next level each time. Slide the presses past five and the LED wraps to dark while the count carries on.

The levels are 0, 8, 32, 96 and 255, each roughly three to four times the one before, not five equal steps. The eye judges light by ratios: equal steps of 64 look like dark and then four bright settings that are hard to tell apart.

The sketch

  • The debounce. reading follows the pin, bounce and all. Every change restarts the clock in lastChange. Only when the pin has held still for DEBOUNCE_MS, 20 ms, does state take the new value. Without it, one press can count several: the contacts make and break a few times as they close.
  • One press, once. The count goes up only at the moment state changes to HIGH. Holding the button down adds nothing; it is the change that counts, not the level.
  • The step. analogWrite(LED, LEVELS[presses % STEPS]). STEPS is sizeof(LEVELS), which is the number of entries because each is one byte, so adding a sixth level needs no other change.

loop() never waits, so the same sketch could also read a sensor or blink something else between presses.

Make it yours

  • Count releases instead. Test state == LOW and the count moves when the finger lifts. Slow presses make the difference easy to feel.
  • Remember the count. It starts at 0 on every reset. The ATmega328P's EEPROM keeps a value without power: flash, SRAM and EEPROM.

The code

Press_Steps.ino

reading is what D7 says now, bounce and all; state is what the sketch has decided the button is. A change to HIGH in state is one press: the count goes up, the Serial Monitor prints it, and LEVELS[presses % STEPS] goes to the LED.

/*
  Count the presses, and step the LED up with each one.
                                          TinkerBlock UNO kit / tkbu

  Every debounced press adds one to a count, prints it, and sets
  the XL LED to the next of five brightness levels. The fifth
  press wraps back to dark.

  Wiring: the TinkerBlock UNO shield on the UNO R3.
    TK04 push button in the D6/D7 socket, so it is on D7.
    TK01 XL LED in the D2/D3 socket, so it is on D3 (a ~ pin).

  Arduino IDE
    Tools > Board: Arduino Uno
    Tools > Port:  the CH340 port (it vanishes when you unplug)
    Serial Monitor at 115200 baud. No library needed.
*/

const int BUTTON = 7;
const int LED = 3;
const unsigned long DEBOUNCE_MS = 20;
const byte LEVELS[] = {0, 8, 32, 96, 255};
const int STEPS = sizeof(LEVELS);

int reading = LOW;          // what the pin says now, bounce and all
int state = LOW;            // what the sketch has decided
unsigned long lastChange = 0;
unsigned long presses = 0;

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON, INPUT);   // the TK04 has its own pull-down
  pinMode(LED, OUTPUT);
  Serial.println("presses: 0");
}

void loop() {
  unsigned long now = millis();
  int r = digitalRead(BUTTON);
  if (r != reading) {       // the pin moved: restart the clock
    reading = r;
    lastChange = now;
  }
  if (reading != state && now - lastChange >= DEBOUNCE_MS) {
    state = reading;        // held still long enough: believe it
    if (state == HIGH) {    // HIGH is pressed on the TK04
      presses++;
      analogWrite(LED, LEVELS[presses % STEPS]);
      Serial.print("presses: ");
      Serial.println(presses);
    }
  }
}

pinMode(BUTTON, INPUT), not INPUT_PULLUP: the TK04 has its own 10 kΩ pull-down and reads HIGH while pressed. The LED is on D3 because D3 is a ~ pin; analogWrite on D7 would only give off or full.

View on GitHub · kits/tkbu/button-counter/Press_Steps/Press_Steps.ino @ v1.14

When it does not work

One press counts two or three

That is contact bounce getting through. Raise DEBOUNCE_MS from 20 to 30 or 50. Nobody feels the difference, and the count settles to one per press. If it still jumps, check the cable is latched at both ends: a loose plug makes and breaks like a bouncing contact.

The count goes up on its own, with nobody pressing

The sketch says INPUT_PULLUP, or the button is not where the sketch looks. With INPUT_PULLUP the chip's pull-up fights the TK04's pull-down and the released pin sits near a volt, where it may read either way. Use INPUT, and check the button is in the D6/D7 socket and not an I2C one.

The button's red lamp lights but the count never moves

The lamp is on the module's SIGNAL and lights with no code, so the button and its supply are fine. The sketch is reading another pin: the D6/D7 socket puts the button on D7, the second printed pin, and BUTTON must be 7.

The LED is either off or full, with no steps between

The LED is on a pin without ~. Keep it in the D2/D3 socket, which puts it on D3. In D6/D7 it would be on D7, where analogWrite can only give 0 or 255.

Where this goes next

One button, two meanings: a tap toggles the LED, a one-second hold sets it blinking.

Lab: a long press →

Edit this page — content/books/tkbu/button-counter.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

TinkerBlock UNO R3 Starter Kit, 12 Modules

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →