#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 14
#define DATA_PIN 12
#define CS_PIN 15
#define TURN_SIGNAL 5 // GPIO pin for the first switch (bottom-left LEDs)
#define OIL_TEMP 18 // GPIO pin for the second switch (top-left LEDs)
MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
unsigned long previousMillis = 0; // Stores the last time LEDs were updated
const long interval = 500; // Interval for LED flashing (0.5 seconds)
bool bottomLeftState = false; // To keep track of the flashing state for bottom-left LEDs
void setup() {
mx.begin(); // Initialize the matrix
pinMode(TURN_SIGNAL, INPUT_PULLUP); // Set the first button pin as input with internal pull-up
pinMode(OIL_TEMP, INPUT_PULLUP); // Set the second button pin as input with internal pull-up
}
void loop() {
unsigned long currentMillis = millis(); // Get the current time
// Read the state of the buttons (pressed = LOW, not pressed = HIGH)
int buttonState1 = digitalRead(TURN_SIGNAL);
int buttonState2 = digitalRead(OIL_TEMP);
// Handle bottom-left LEDs (flashing with Button 1)
if (buttonState1 == LOW) {
if (currentMillis - previousMillis >= interval) {
// Toggle the flashing state every 2 seconds
previousMillis = currentMillis;
bottomLeftState = !bottomLeftState; // Toggle state
if (bottomLeftState) {
// Turn on the bottom-left corner 4 LEDs
mx.setPoint(0, 7, true); // Bottom-left LED
mx.setPoint(1, 7, true); // Next LED to the right
mx.setPoint(0, 6, true); // LED above the bottom-left
mx.setPoint(1, 6, true); // LED to the right of the one above
} else {
// Turn off the bottom-left LEDs
mx.setPoint(0, 7, false);
mx.setPoint(1, 7, false);
mx.setPoint(0, 6, false);
mx.setPoint(1, 6, false);
}
mx.update(); // Apply the changes
}
} else {
// If Button 1 is not pressed, turn off the bottom-left LEDs
mx.setPoint(0, 7, false);
mx.setPoint(1, 7, false);
mx.setPoint(0, 6, false);
mx.setPoint(1, 6, false);
mx.update();
}
// Handle top-left LEDs (on when Button 2 is pressed)
if (buttonState2 == LOW) {
// Light up the top-left corner 4 LEDs
mx.setPoint(0, 0, true); // Top-left LED
mx.setPoint(1, 0, true); // Next LED to the right
mx.setPoint(0, 1, true); // LED below the top-left
mx.setPoint(1, 1, true); // LED to the right below the second
mx.update(); // Apply the changes
} else {
// If Button 2 is not pressed, turn off the top-left LEDs
mx.setPoint(0, 0, false);
mx.setPoint(1, 0, false);
mx.setPoint(0, 1, false);
mx.setPoint(1, 1, false);
mx.update();
}
}