Code for interaction test
#define pin1 1 // Chip Select (CS)
#define pin2 2 // Clock (SCK)
#define pin3 3 // Data Input (MOSI)
#define buttonPin1 28 // Button Input 1
#define buttonPin2 29 // Button Input 2
// LED Matrix Setup
#define X_SEGMENTS 4 // 4 LED matrix bars horizontally
#define Y_SEGMENTS 3 // 3 rows of matrices
#define NUM_SEGMENTS (X_SEGMENTS * Y_SEGMENTS)
// Framebuffer
byte fb[8 * NUM_SEGMENTS];
uint8_t number1 = 0; // Controlled by buttonPin1
uint8_t number2 = 0; // Controlled by buttonPin2
unsigned long lastPressTime1 = 0;
unsigned long lastPressTime2 = 0;
const unsigned long debounceDelay = 2500; // 2.5 seconds
// Function to Send Data to All MAX7219s
void shiftAll(byte address, byte data) {
digitalWrite(pin1, LOW); // Enable Chip Select
for (int i = 0; i < NUM_SEGMENTS; i++) {
shiftOut(pin3, pin2, MSBFIRST, address); // Send address
shiftOut(pin3, pin2, MSBFIRST, data); // Send data
}
digitalWrite(pin1, HIGH); // Disable Chip Select
}
void setup() {
Serial.begin(115200);
// Initialize SPI pins
pinMode(pin1, OUTPUT); // CS
pinMode(pin2, OUTPUT); // Clock
pinMode(pin3, OUTPUT); // Data Input
pinMode(buttonPin1, INPUT_PULLUP); // Button input 1
pinMode(buttonPin2, INPUT_PULLUP); // Button input 2
// Initialize Each MAX7219
shiftAll(0x0F, 0x00); // Display Test Register - Test Mode OFF
shiftAll(0x0B, 0x07); // Scan Limit: Display Digits 0-7
shiftAll(0x0C, 0x01); // Shutdown Register - Normal Operation
shiftAll(0x0A, 0x08); // Brightness: Medium
shiftAll(0x09, 0x00); // Decode Mode OFF
}
void loop() {
unsigned long currentTime = millis();
if (digitalRead(buttonPin1) == LOW && (currentTime - lastPressTime1 > debounceDelay)) {
lastPressTime1 = currentTime;
if (number1 < 99) {
number1++;
Serial.print("Button 1 touched! Number 1: ");
Serial.println(number1);
}
}
if (digitalRead(buttonPin2) == LOW && (currentTime - lastPressTime2 > debounceDelay)) {
lastPressTime2 = currentTime;
if (number2 < 99) {
number2++;
Serial.print("Button 2 touched! Number 2: ");
Serial.println(number2);
}
}
// Clear framebuffer
clear();
// Draw numbers on respective rows
draw_number(12, 0, number1); // Top row for number1
draw_number(12, 16, number2); // Bottom row for number2
// Show updated framebuffer
show();
}
void set_pixel(uint8_t x, uint8_t y, uint8_t mode) {
if (x >= X_SEGMENTS * 8 || y >= Y_SEGMENTS * 8) return;
byte *addr = &fb[(y * X_SEGMENTS) + (x / 8)];
byte mask = 128 >> (x % 8);
switch (mode) {
case 0: *addr &= ~mask; break; // Clear pixel
case 1: *addr |= mask; break; // Set pixel
case 2: *addr ^= mask; break; // Toggle pixel
}
}
void clear() {
memset(fb, 0, sizeof(fb));
}
void draw_number(uint8_t x, uint8_t y, uint8_t number) {
static const byte numbers[10][8] = {
{0b00111100, 0b01100110, 0b01101110, 0b01110110, 0b01111010, 0b01110010, 0b01100110, 0b00111100}, // 0
{0b00111100, 0b00111000, 0b00011000, 0b00011000, 0b00011000, 0b00011000, 0b00111000, 0b00111100}, // 1
{0b00111100, 0b01100110, 0b01100010, 0b00000110, 0b00001100, 0b00011000, 0b01100110, 0b00111100}, // Corrected 2
{0b00111100, 0b01100110, 0b00000110, 0b00011100, 0b00000110, 0b00000110, 0b01100110, 0b00111100}, // 3
{0b00001100, 0b00011100, 0b00101100, 0b01001100, 0b01111110, 0b00001100, 0b00001100, 0b00001100}, // 4
{0b01111110, 0b01100000, 0b01111100, 0b00000110, 0b00000110, 0b00000110, 0b01100110, 0b00111100}, // 5
{0b00111100, 0b01100110, 0b01100000, 0b01111100, 0b01100110, 0b01100110, 0b01100110, 0b00111100}, // 6
{0b01111110, 0b01100110, 0b00001100, 0b00011000, 0b00011000, 0b00011000, 0b00011000, 0b00011000}, // 7
{0b00111100, 0b01100110, 0b01100110, 0b00111100, 0b01100110, 0b01100110, 0b01100110, 0b00111100}, // 8
{0b00111100, 0b01100110, 0b01100110, 0b00111110, 0b00000110, 0b00000110, 0b01100110, 0b00111100} // 9
};
for (byte row = 0; row < 8; row++) {
for (byte col = 0; col < 8; col++) {
if (numbers[number][row] & (1 << col)) {
set_pixel(x + col, y + row, 1);
}
}
}
}
void show() {
for (byte row = 0; row < 8; row++) {
digitalWrite(pin1, LOW);
for (byte segment = 0; segment < NUM_SEGMENTS; segment++) {
byte x = segment % X_SEGMENTS;
byte y = segment / X_SEGMENTS * 8;
byte addr = (row + y) * X_SEGMENTS;
shiftOut(pin3, pin2, MSBFIRST, 8 - row);
shiftOut(pin3, pin2, MSBFIRST, fb[addr + x]);
}
digitalWrite(pin1, HIGH);
}
}