I have a Pink LED moving across 16 pads,
some pads can be latched on (Blue LEDs)
how can i keep the Blue latched on LEDs on when the Pink LED passes?
Latched on =1 and off =0 in my array
int trellisButtonState[16] = {0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};
I want the previous LED to go off only if its array number is 0 (latched off)
and stay Blue if its array number is 1 (latched on)
I tried but i'm unsure how to read that array
if (trellisButtonState[16] = 1) {LED Blue} // because its latched
else {LED OFF}
#include "Adafruit_NeoTrellis.h"
//#define Y_DIM 4 //number of rows of key
//#define X_DIM 4 //number of columns of keys
// ----------------------------------------FSW NEOPIXELS---------------------------------------------
#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN 5 // (Led Pin)
#define PIXEL_COUNT 2
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
// ----------------------------------------FSW NEOPIXELS---------------------------------------------
Adafruit_NeoTrellis trellis;
int FSW2 = 2; // Start/stop footswitch
int FSW2State = HIGH;
int button1 = 3; // tap footswitch
int pad = -1;
int ssLedState = -1; //this variable tracks the startStop LED, negative if off, positive if on
int ttLedState = LOW; // ledState used to set the tempo LED
int lastTapState = LOW; //the last tap button state
int activeButton = 0;
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 500; // the debounce time; increase if the output flickers
unsigned long currentTimer[2] = { 1500, 1500 }; // array of most recent tap counts
unsigned long timeoutTime = 0; //this is when the timer will trigger next
unsigned long timeoutTime2 = 0; //this is when the timer will trigger next
unsigned long lastTap = 0; //this vairable determines the time of the most recent tap, used when recording data when button is pressed
unsigned long indicatorTimeout; //variable used to determine the duration of the led blinks on state
unsigned long indicatorTimeout2; //variable used to determine the duration of the pad X4 blinks on state
int trellisButtonState[16] = {0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
};
elapsedMillis button_hold_counter = 0;
int8_t held_button_id = -1;
void setup() {
Serial.begin(9600);
trellis.begin();
trellis.pixels.setBrightness(50);
pixels.begin(); // for start/stop and tempo LEDs
pixels.setBrightness(50);
pixels.setPixelColor(1, 220, 20, 60);
pixels.show();
pinMode( button1, INPUT_PULLUP ); //tempo button input
pinMode(FSW2, INPUT_PULLUP);
Serial.println("NeoPixel Trellis started");
//activate momentary pads and set callbacks
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) {
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
trellis.registerCallback(i, momentary);
}
//do a little animation to show we're on
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, Wheel(map(i, 0, trellis.pixels.numPixels(), 0, 255)));
trellis.pixels.show();
delay(25);
}
for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
trellis.pixels.setPixelColor(i, 0x000000);
trellis.pixels.show();
delay(25);
}
}
void loop() {
trellis.read(); // interrupt management does all the work! :)
startStopFSW();
if (ssLedState > 0) {
runSeq();
}
// if (ssLedState < 0) { // if stop led, clear last seq led
// trellis.pixels.setPixelColor(pad, 0x000000); // always turns last seq led off, no good
// trellis.pixels.show();
// return; // just run once? didnt help
// }
}
void runSeq() {
int tapState = digitalRead( button1 ); //button is first pressed down
//if (ssLedState < 0) {
// trellis.pixels.setPixelColor(pad, 0x000000); // doesnt work here either
// trellis.pixels.show();
//}
if ( tapState == LOW && tapState != lastTapState ) {
currentTimer[1] = currentTimer[0]; //this takes the tap time done before it (measured in millis), sets it one back in the array,
currentTimer[0] = millis() - lastTap; //and then sets the other slot of the array to the length of the most recent tap time
lastTap = millis();
timeoutTime = 0;
timeoutTime2 = 0;
}
lastTapState = tapState; //variable used to determine the most recent state of the button
if ( millis() >= timeoutTime ) { //checks to see if it is time to turn on the tap fsw LED
indicatorTimeout = millis() + 150; //determines the length of time that the LED is on
//this will set the new tempo
timeoutTime = millis() + ((currentTimer[0] + currentTimer[1]) / 2);
}
if ( millis() >= timeoutTime2 ) { //checks to see if it is time to turn on the trellis LEDs
indicatorTimeout2 = millis() + 10; //determines the length of time that the trellis LED is on
//this will set the new tempo
timeoutTime2 = millis() + ((currentTimer[0] + currentTimer[1]) / 8); // Divide by 8 to make it 4 times faster
pad++;
pad %= 16;
}
//turns on tempo led for the length of indicatorTimeout and then turns it off
if ( millis() < indicatorTimeout ) {
pixels.setPixelColor(0, 0, 255, 255); // Tempo LED on
pixels.show();
}
pixels.setPixelColor(0, 0, 0, 0); // Tempo LED off
pixels.show();
//turns on trellis led for the length of indicatorTimeout, then turns it off, then moves to next led
if ( millis() < indicatorTimeout2 ) {
//trellis.pixels.setPixelColor(pad, 0, 255, 255); // trellis led on
trellis.pixels.setPixelColor(pad, 220, 20, 60);
trellis.pixels.show();
}
trellis.pixels.setPixelColor(pad, 0); // trellis led off
}
void startStopFSW() {
FSW2State = digitalRead(FSW2); //sample the state of the button - is it pressed or not?
if ( (millis() - lastDebounceTime) > debounceDelay) { //filter out any noise by setting a time buffer
if ( (FSW2State == LOW) && (ssLedState < 0) ) { //if the button has been pressed, lets toggle the LED from "off to on" or "on to off"
pixels.setPixelColor(1, 124, 252, 0); // start LED lawn green:
pixels.show();
// runSeq(); // START runSeq FUNCTION
Serial.println("Start");
pad = -1;
ssLedState = -ssLedState; //now the LED is on, we need to change the state
lastDebounceTime = millis(); //set the current time
}
else if ( (FSW2State == LOW) && (ssLedState > 0) ) {
pixels.setPixelColor(1, 220, 20, 60); // stop LED crimson
pixels.show();
Serial.println("Stop");
//trellis.pixels.setPixelColor(0, 0); // trellis led off
// STOP runSeq FUNCTION
ssLedState = -ssLedState; //
lastDebounceTime = millis(); //set the current time
}
}//close if
}
TrellisCallback momentary(keyEvent evt) { // Momentary trellis buttons
// Check is the pad pressed?
if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
Serial.println("NeoPixel Pressed");
trellis.pixels.setPixelColor(evt.bit.NUM, Wheel(map(evt.bit.NUM, 0, trellis.pixels.numPixels(), 0, 255))); //on rising
//set corresponding button states array element to 1, so we know which button has been pressed
// button_presses[evt.bit.NUM] = 1;
//reset timer for detecting a long press, which will take us to latching pattern page if held for 0.8secs
button_hold_counter = 0;
held_button_id = evt.bit.NUM;
}
else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
// or is the pad released?
//Serial.println("NeoPixel Released");
trellis.pixels.setPixelColor(evt.bit.NUM, 0); //off falling
//if the held button has been released, Resetting the held button id means we are no longer tracking that long press.
if (held_button_id == evt.bit.NUM) {
if (button_hold_counter >= 800) {
Serial.println("Long press detected");
// *Load pattern input page for pressed pad HERE*
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) { // Activate latching trellis buttons
trellis.registerCallback(i, latching);
}
}
else {
held_button_id = -1;
}
}
}
// Turn on/off the neopixels!
trellis.pixels.show();
return 0;
}
TrellisCallback latching(keyEvent evt) { // Latching trellis buttons
if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
//reset timer for detecting a long press, which will take us to latching pattern page if held for 0.8secs
button_hold_counter = 0;
held_button_id = evt.bit.NUM;
Serial.println(evt.bit.NUM);
if (trellisButtonState[evt.bit.NUM] == 0) {
//trellis.setPixelColor(evt.bit.NUM, Wheel(map(evt.bit.NUM, 0, X_DIM*Y_DIM, 0, 255))); //on rising
trellis.pixels.setPixelColor(evt.bit.NUM, 0x0000FF); //blue on rising
trellisButtonState[evt.bit.NUM] = 1;
trellis.pixels.show();
}
else if (trellisButtonState[evt.bit.NUM] == 1) {
trellis.pixels.setPixelColor(evt.bit.NUM, 0); //turn off on rising
trellisButtonState[evt.bit.NUM] = 0;
trellis.pixels.show();
Serial.println(evt.bit.NUM);
}
}
else
//if the held button has been released, Resetting the held button id means we are no longer tracking that long press.
if (held_button_id == evt.bit.NUM) {
//
if (button_hold_counter >= 800) {
Serial.println("Long press detected");
// *Load main page for momentary pads HERE*
for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) { // Activate momentary trellis buttons
trellis.registerCallback(i, momentary);
}
}
else {
held_button_id = -1;
}
}
trellis.pixels.show(); // works without, leaving for now
return 0;
}
/******************************************/
// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if (WheelPos < 85) {
return trellis.pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return trellis.pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return trellis.pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
return 0;
}
