Read the buttons
What this example shows
All 16 pads are read every 50 ms and printed as a 4x4 map in the Serial Monitor
at 115200 baud: X for a pressed pad, . for a released one. The LEDs stay
off.
The example code
This is examples/ReadButtons/ReadButtons.ino from the pinned library,
unchanged.
/**
**************************************************
*
* @file ReadButtons.ino
* @brief Example showing how to read all 16 buttons on the Inputronic Grid.
*
* Reads all 16 button states every 50 ms and prints a 4×4 grid
* map to the Serial monitor. Pressed buttons show 'X'; released
* buttons show '.'.
*
* 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;
void setup()
{
Serial.begin(115200);
while (!Serial)
;
if (!grid.begin())
{
Serial.println("Inputronic GRID not found. Check wiring.");
while (1)
;
}
Serial.println("Inputronic GRID ready.\n");
grid.clearLEDs();
}
void loop()
{
Serial.println("+---------+");
for (uint8_t row = 0; row < 4; row++)
{
Serial.print("| ");
for (uint8_t col = 0; col < 4; col++)
{
Serial.print(grid.readPad(row, col) ? 'X' : '.');
Serial.print(' ');
}
Serial.println("|");
}
Serial.println("+---------+\n");
delay(50);
}
Expected result, with nothing pressed:
Inputronic GRID ready.
+---------+
| . . . . |
| . . . . |
| . . . . |
| . . . . |
+---------+
A new map follows every 50 ms. While you hold a pad, an X takes the place of
its .: the top line of the map is row 0 and the first character in a line is
column 0. 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 InputronicGridCreates 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
| Type | Name | Description |
|---|---|---|
uint8_t | address | I2C address of the board. Optional, 0x30 (INPUTRONIC_GRID_DEFAULT_ADDR) by default. |
grid.begin(TwoWire &wire = Wire)returns boolStarts 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
| Type | Name | Description |
|---|---|---|
TwoWire & | wire | The I2C bus to use. Optional, Wire by default. |
grid.clearLEDs()returns voidTurns all 16 LEDs off. It is the same as setAllLEDs(0, 0, 0).
ReturnsNothing.
grid.readPad(uint8_t row, uint8_t col)returns boolAsks the board whether one pad is pressed right now, by its place in the grid. Each call sends the board a short I2C request and reads its one-byte reply.
Returnstrue while the pad is pressed, false when it is not.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | row | Row, 0 to 3, top to bottom. |
uint8_t | col | Column, 0 to 3, left to right. |
Putting it together
setup() waits for the Serial Monitor, checks the board with begin() and turns
the LEDs off. Each pass of loop() calls readPad() once for every row and
column and prints the result as one line per row.
To read all 16 pads with one request and reply instead of sixteen, the library also has
readAllPads(), which returns a 16-bit mask with bit row * 4 + col set for
each pressed pad. No example uses it at this commit.