Hello, I'm having trouble understanding what I need to change in my program regarding a momentary switch. What I'm looking to do is have a momentary switch execute two modes for an led strip. One mode is a simple flash, the other is a fade followed by flashing. When the switch is not pressed (LOW) I want it to flash, when the switch is pressed i want it to start the second mode and stay in that mode until powered off. So do i need to write some sort of latch when the switch is high? In my program now i have to hold the switch closed for a few seconds before it executes the second mode, but as soon as i release the switch it goes back to the first mode. Here is a sample of my program:
#include "Adafruit_NeoPixel.h"
#define PIN 6
#define NUMPIXELS 4
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS,PIN,NEO_GRB +
NEO_KHZ800);
enum STATE {
STATE_FADE_OUT,
STATE_LIGHTNING
};
STATE currentState;
unsigned long time;
int brightness;
int switchState = 0;
int count = 0;
int lastState = 0;
int pushCount =0;
void setup() {
pinMode(2, INPUT);
currentState = STATE_FADE_OUT;
brightness = 255;
pixels.begin();
}
void loop() {
time = millis();
switchState = digitalRead(2);
if (lastState == switchState) {
for(int i=0;i<NUMPIXELS;i++){ //FIRST MODE, REPEATS UNTIL POWERED OFF IF SWITCH IS LOW
pixels.setPixelColor(0,255,255,225);
pixels.setPixelColor(1,200,200,200);
pixels.show();
}
delay(110);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,0,0));
pixels.show();
}
delay(100);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(1,180,180,180);
pixels.setPixelColor(2,255,255,225);
pixels.setPixelColor(3,200,200,200);
pixels.show();
}
delay(50);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,0,0));
pixels.show();
}
delay(3000);
}
// 2ND MODE, REPEATS UNTIL POWER OFF IF SWITCH IS PRESSED
if(lastState != switchState){ / 2ND MODE, REPEATS UNTIL POWER OFF IF SWITCH IS PRESSED s switch(currentState){
case STATE_FADE_OUT:
if(time % 1000 == 0){
brightness--;
pixels.setPixelColor(0,(brightness250/255),(brightness38/255),(brightness0/255));
pixels.setPixelColor(1,(brightness250/255),(brightness38/255),(brightness0/255));
pixels.show();
if(brightness==5)
{
currentState = STATE_LIGHTNING;
}
}
break;
case STATE_LIGHTNING:
while(true)
{
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(0,255,255,225);
pixels.setPixelColor(1,200,200,200);
//pixels.setPixelColor(2,140,140,225);
pixels.show();
}
delay(110);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,0,0));
pixels.show();
}
delay(100);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(2,255,255,225);
pixels.setPixelColor(3,200,200,200);
pixels.show();
}
delay(50);
for(int i=0;i<NUMPIXELS;i++){
pixels.setPixelColor(i, pixels.Color(0,0,0));
pixels.show();
}
delay(9000);
}
break;
}
}
}