Skip to main content

Light a pad when you press it

What this example shows​

Each pad switches its own LED. The first press lights it in the colour of its row (row 0 red, row 1 green, row 2 blue, row 3 orange), and the next press turns it off. Every press is printed in the Serial Monitor at 115200 baud.

The example code​

This is examples/ButtonLEDControl/ButtonLEDControl.ino from the pinned library, unchanged.

Select Board
ButtonLEDControl.ino
/**
**************************************************
*
* @file ButtonLEDControl.ino
* @brief Example showing button-driven LED toggle on the Inputronic Grid.
*
* Each button toggles its corresponding LED on or off.
* First press turns the LED on in a row-specific color;
* second press turns it off.
*
* Demonstrates reading individual pads by row/col coordinate
* and reacting with per-LED color control using the same
* row/col coordinates.
*
* Hardware wiring:
* - Connect Inputronic Grid to host via Qwiic / I2C
* (SDA, SCL, 3V3, GND).
*
* @authors Josip Šimun Kuči @ soldered.com
***************************************************/

#include <InputronicGrid.h>

InputronicGrid grid;

// Toggle state per button, indexed by [row][col]
bool ledOn[4][4] = {};

// Colors per row (R, G, B)
const uint8_t rowColor[4][3] = {
{255, 0, 0}, // row 0 — red
{ 0, 255, 0}, // row 1 — green
{ 0, 0, 255}, // row 2 — blue
{255, 128, 0}, // row 3 — orange
};

// Previous pad state per [row][col] for edge detection
bool prevState[4][4] = {};

void setup()
{
Serial.begin(115200);

if (!grid.begin())
{
Serial.println("Inputronic GRID not found. Check wiring.");
while (1)
;
}

grid.clearLEDs();
Serial.println("Press any button to toggle its LED.");
}

void loop()
{
for (uint8_t row = 0; row < 4; row++)
{
for (uint8_t col = 0; col < 4; col++)
{
bool pressed = grid.readPad(row, col);

if (pressed && !prevState[row][col])
{
ledOn[row][col] = !ledOn[row][col];

if (ledOn[row][col])
grid.setLED(row, col, rowColor[row][0], rowColor[row][1], rowColor[row][2], 255);
else
grid.setLED(row, col, 0, 0, 0, 255);

Serial.print("Button (row ");
Serial.print(row);
Serial.print(", col ");
Serial.print(col);
Serial.print(") → LED ");
Serial.println(ledOn[row][col] ? "ON" : "OFF");
}

prevState[row][col] = pressed;
}
}

delay(20);
}

Expected result, after pressing the pad at row 0, column 0 twice:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
Press any button to toggle its LED.
Button (row 0, col 0) → LED ON
Button (row 0, col 0) → LED OFF

The pad's LED lights red on the first press and goes off on the second. When the board does not answer, the sketch prints Inputronic GRID not found. Check wiring. and stops.

Functions used​

InputronicGrid(uint8_t address = INPUTRONIC_GRID_DEFAULT_ADDR)returns InputronicGrid

Creates the driver object for one GRID. The example declares it as InputronicGrid grid; with no address, so it uses the default 0x30.

ReturnsThe driver object.

Parameters

TypeNameDescription
uint8_taddressI2C address of the board. Optional, 0x30 (INPUTRONIC_GRID_DEFAULT_ADDR) by default.
grid.begin(TwoWire &wire = Wire)returns bool

Starts the I2C bus with wire.begin() and checks that the board answers on its address.

Returnstrue when the board acknowledged on the bus, false when it was not found.

Parameters

TypeNameDescription
TwoWire &wireThe I2C bus to use. Optional, Wire by default.
grid.clearLEDs()returns void

Turns all 16 LEDs off. It is the same as setAllLEDs(0, 0, 0).

ReturnsNothing.

grid.readPad(uint8_t row, uint8_t col)returns bool

Asks the board whether one pad is pressed right now, by its place in the grid.

Returnstrue while the pad is pressed, false when it is not.

Parameters

TypeNameDescription
uint8_trowRow, 0 to 3, top to bottom.
uint8_tcolColumn, 0 to 3, left to right.
grid.setLED(uint8_t row, uint8_t col, uint8_t r, uint8_t g, uint8_t b, uint8_t intensity)returns void

Sets the colour of the LED under one pad, by its place in the grid. Each colour value is scaled by intensity / 255 before it is sent.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_trowRow, 0 to 3.
uint8_tcolColumn, 0 to 3.
uint8_trRed, 0 to 255.
uint8_tgGreen, 0 to 255.
uint8_tbBlue, 0 to 255.
uint8_tintensityBrightness, 0 (off) to 255 (full). Required in this form.

Always pass all six arguments to the row and column form. With five arguments, setLED(index, r, g, b, intensity = 128) is called instead, and the first number is read as an LED index.

Putting it together​

begin() checks the board is there, and clearLEDs() starts with every LED off. The loop reads all 16 pads with readPad() every 20 ms and compares each one with its state from the last pass, so a pad held down counts as one press. On a new press the sketch flips that pad's entry in ledOn and calls setLED() with the same row and column, at full intensity in the row's colour or with 0, 0, 0 to turn it off.