Binary counter
A counts up in binary on the top row of LEDs, from 0 to 63. B resets. Learn how computers count!
What you need
- TinkerPie V1 board (or the free online simulator)
- USB-C data cable
Step by step
- Open Binary counter in TinkerPie Studio. It opens as blocks.
- Press Run to try it in the simulator. Use the sliders and buttons under the board picture to test it.
- Connect TinkerPie with a USB-C cable in Chrome or Edge, press Connect, then Download. Or, with Studio started from the TinkerPie launcher on your computer, open the Arduino C++ tab and press Build & Flash.
- Change something: a colour, a number, a sound. Then run it again.
The code
This is the program the blocks make. The same program runs in the simulator and on the board.
JavaScript
var n, bit, i;
input.onButtonPressed(Button.A, function () {
n = (n + 1) % 64;
serial.writeValue('value', n);
basic.clearScreen();
basic.pause(20);
bit = n;
for (i = 5; i >= 0; i--) {
if (bit % 2 == 1) {
led.plot(i, 0, Colors.Cyan);
basic.pause(20);
}
bit = Math.floor(bit / 2);
}
});
input.onButtonPressed(Button.B, function () {
n = 0;
serial.writeValue('value', n);
basic.clearScreen();
basic.pause(20);
bit = n;
for (i = 5; i >= 0; i--) {
if (bit % 2 == 1) {
led.plot(i, 0, Colors.Cyan);
basic.pause(20);
}
bit = Math.floor(bit / 2);
}
});
n = 0;Arduino C++ (TinkerPie library)
// Binary counter
// Arduino C++ made by TinkerPie Studio from the blocks. Board: TinkerPie V1 (ESP32-S3).
// Arduino IDE: install the TinkerPie library (Studio menu > Arduino library), then Tools > Board > ESP32S3 Dev Module,
// USB CDC On Boot: Enabled, Flash Size: 16MB, Partition Scheme: 16M Flash (3MB APP/9.9MB FATFS), PSRAM: OPI PSRAM.
#include <TinkerPie.h>
using namespace tinkerpie;
double n = 0;
double bit2 = 0;
double i = 0;
void setup() {
tinkerpie::begin();
input::onButtonPressed(Button::A, []() {
n = fmod(n + 1, 64);
serial::writeValue("value", n);
basic::clearScreen();
basic::pause(20);
bit2 = n;
for (i = 5; i >= 0; i -= 1) {
if (fmod(bit2, 2) == 1) {
led::plot(i, 0, Colors::Cyan);
basic::pause(20);
}
bit2 = floor(bit2 / 2);
}
});
input::onButtonPressed(Button::B, []() {
n = 0;
serial::writeValue("value", n);
basic::clearScreen();
basic::pause(20);
bit2 = n;
for (i = 5; i >= 0; i -= 1) {
if (fmod(bit2, 2) == 1) {
led::plot(i, 0, Colors::Cyan);
basic::pause(20);
}
bit2 = floor(bit2 / 2);
}
});
n = 0;
}
void loop() {
tinkerpie::loop(); // runs the forever blocks and handles events
}More projects like this
Hello, TinkerPieA red heart, then your first scrolling message.StarterScrolling BOTICSBOTICS scrolls across the matrix in a new colour every time.StarterHeartbeatA beating heart: brightness pulses like lub-dub.Starter
Rainbow waveA rainbow that flows across all 30 LEDs.StarterButton counterA adds one, B resets. Counts show on the matrix.StarterShake diceShake the board to roll a number from 1 to 6, with a click.Starter