Push button to switching Led effects

Oops never mind, don't use my code, use the one you found...

Change this part so that it only happens with a key press.

Ok . So those millis from above won't be nessesery? With this void display button will swiching those animations? If understand this right.

The code you found uses millis() correctly, as far as I can see. That means normal button press code should work.

Stop calling functions "voids"... they are "functions".

In this program, the operation of millis() is hidden inside macros

EVERY_N_MILLISECONDS()
EVERY_N_SECONDS()

Those must be utilities provided for your convenience by the FastLED library...

Yes. Sorry. Functions i was meaning. Thanks for looking this code and your help. I will try this code and let You know how is work. Thank you again.

Don't forget that any push button switch code you include, must also be non-blocking for it to work.

You probably mean this if I'm right.

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'
}

void loop() {
  // Get current button state.
  boolean newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if((newState == LOW) && (oldState == HIGH)) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if(newState == LOW) {      // Yes, still low
      if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
      switch(mode) {           // Start the new animation...
        case 0:
          colorWipe(strip.Color(  0,   0,   0), 50);    // Black/off
          break;
        case 1:
          colorWipe(strip.Color(255,   0,   0), 50);    // Red
          break;

to include in this bit above what you was mention.

No, please put all your button code in a function, like 'readButton()'. Also you used a blocking 'delay(20)'. I told you not to use blocking code.

Also, it seems like you're leaning back on the blocking effect routines (colorWipe...) from the original example. I thought I explained clearly that those can not run without extensive re-writes.

In any case, you have to ensure that any effects code that you use, is non-blocking.

1 Like

Ok . I will assebly everything. And see it. I'm very appreciate for help. Imust to do that step by step because is still so many information to remeber. Thank you aarg. You are good man.

aarg look what i found. I start to read this yesteday and finished today. I guees this what you mean i must to do . So happy i found it now I wiil to write this way to my sketch and no delay() there. Thanks again. Just wanted to share with that news.

unsigned long periodStartMillis;
unsigned long currentMillis;
const unsigned long period = 5000;  //period during which button input is valid
const byte buttonPin1 = A1;    //button on pin A1
byte currentButtonState;
byte previousButtonState;
int count = 0;
boolean printFinalMessage = true;
unsigned long debounceStartMillis;
unsigned long debouncePeriod = 20;
boolean debouncing = false;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin1, INPUT_PULLUP);
  Serial.println("Press the button as many times a possible in 5 seconds");
  periodStartMillis = millis();
}

void loop()
{
  currentMillis = millis();
  if (currentMillis - periodStartMillis <= period)  //true until the period elapses
  {
    previousButtonState = currentButtonState;    //save the previous button state
    currentButtonState = digitalRead(buttonPin1);  //read the current state of the input
    if (currentButtonState != previousButtonState) //if the button state has changed
    {
      debounceStartMillis = currentMillis;  //save the time that the state change occured
      debouncing = true;  //flag that debouncing in progress
    }    //end state change check
    
    if (currentMillis - debounceStartMillis >= debouncePeriod)  //if the debounce period has elapsed
    {
      if (debouncing == true)    //debouncing taking place
      {
        if (currentButtonState == LOW)  //if the button is currently pressed
        {
          debouncing = false;    //debouncing is finished
          count++;               //increment the count
          Serial.println(count);
        }    //end count increment
      }  //end debouncing in progress check
    }    //end debounce time elapsed check
  }  //end timing period check
  else  //period has ended
  {
    if (printFinalMessage == true)
    {
      Serial.println("Time is up");
      Serial.print("Button pressed count : ");
      Serial.println(count);
      printFinalMessage = false;    //prevent the final message being displayed again
    }    //end printing final message
  }    //end final message check
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.