Hi All,
I am currently stuck with my code to create a simple flashing LED step sequence that loops around using 4 x NeoKey 1x4 Modules chained together, with a separate module for controlling the start and stop.
My wiring and I2C communication is working correctly and no issues with power. I am using a teensy 4.1 board.
So far it works but the LEDs for the modules all play together at the same time, what I want to achieve is when I press play it plays the LEDs in sequence from module 1 (leds 0,1,2,3) and module 2 (leds 4,5,6,7) and so on until it reaches the end and loops back around again.
#include "Adafruit_NeoKey_1x4.h"
#include "seesaw_neopixel.h"
// Create an array of NeoKey objects for each module
Adafruit_NeoKey_1x4 neokeys[] = {
Adafruit_NeoKey_1x4(), // Control module (0x38)
Adafruit_NeoKey_1x4(), // First LED sequence module (0x30)
Adafruit_NeoKey_1x4(), // Second LED sequence module (0x31)
Adafruit_NeoKey_1x4(), // Third LED sequence module (0x32)
Adafruit_NeoKey_1x4() // Fourth LED sequence module (0x34)
};
const uint8_t numModules = sizeof(neokeys) / sizeof(neokeys[0]);
// Control buttons (for 0x38 address)
const uint8_t playButton = 0; // Play button
const uint8_t stopButton = 1; // Stop button
bool sequenceRunning = false;
unsigned long lastMillis = 0;
unsigned long ledDelay = 110; // Delay between LED flashes in ms
int currentLed = 0; // Keeps track of the current LED being lit
bool lastPlayState = false;
bool lastStopState = false;
void setup() {
Serial.begin(115200);
// Initialize each NeoKey module with the appropriate I2C address
if (!neokeys[0].begin(0x38)) { // Control module
Serial.println("Could not start NeoKey 1, check wiring?");
while(1) delay(10);
}
if (!neokeys[1].begin(0x30)) { // LED sequence module 1
Serial.println("Could not start NeoKey 2, check wiring?");
while(1) delay(10);
}
if (!neokeys[2].begin(0x31)) { // LED sequence module 2
Serial.println("Could not start NeoKey 3, check wiring?");
while(1) delay(10);
}
if (!neokeys[3].begin(0x32)) { // LED sequence module 3
Serial.println("Could not start NeoKey 4, check wiring?");
while(1) delay(10);
}
if (!neokeys[4].begin(0x34)) { // LED sequence module 4
Serial.println("Could not start NeoKey 5, check wiring?");
while(1) delay(10);
}
Serial.println("All NeoKey modules started!");
// Turn off all LEDs at the beginning
for (uint8_t j = 0; j < numModules; j++) {
for (uint16_t i = 0; i < neokeys[j].pixels.numPixels(); i++) {
neokeys[j].pixels.setPixelColor(i, 0x000000);
neokeys[j].pixels.show();
}
}
}
void loop() {
// Control module (0x38): Check buttons for play and stop
uint8_t controlKey = neokeys[0].read(); // Read control module keys
// Check for play button press
bool currentPlayState = controlKey & (1 << playButton);
if (currentPlayState && !lastPlayState) {
Serial.println("Sequence started");
sequenceRunning = true;
currentLed = 0; // Reset to the first LED in the sequence
}
lastPlayState = currentPlayState;
// Check for stop button press
bool currentStopState = controlKey & (1 << stopButton);
if (currentStopState && !lastStopState) {
Serial.println("Sequence stopped");
sequenceRunning = false;
}
lastStopState = currentStopState;
// If the sequence is running, execute the LED sequence
if (sequenceRunning) {
unsigned long currentMillis = millis();
if (currentMillis - lastMillis >= ledDelay) {
lastMillis = currentMillis;
runSequence();
}
}
}
// LED sequence function
void runSequence() {
// Loop through all NeoKey modules except the control module
for (uint8_t j = 1; j < numModules; j++) { // Start from 1 to skip the control module
// Loop through LEDs on each NeoKey module
for (int i = 0; i < neokeys[j].pixels.numPixels(); i++) {
// Check if the current LED is the one being activated
if (i == currentLed) {
neokeys[j].pixels.setPixelColor(i, neokeys[j].pixels.Color(255, 0, 0)); // Red color
} else {
neokeys[j].pixels.setPixelColor(i, 0x000000); // Turn off the other LEDs
}
}
neokeys[j].pixels.show(); // Update the LEDs
}
// Move to the next LED
currentLed++;
// If we've gone through all LEDs, reset to the first LED
if (currentLed >= 16) {
currentLed = 0;
}
}
Any help would be appreciated and excuse the bulky code, I am a novice.
Thanks,
David