Non blocking push button

Hi guys . Anyone would like modify code for me please and explain everything because this is still to difficult for me. I need to add button to change Led effects. Let me know if someone will be interesting to do it. I'll pay for it just say how much. Thank you.

Start by posting your current sketch

Here is an example using the state change detection method that allows the selection of an action based on the number of presses of a button switch. Put your actions in the case structures.


const byte  buttonPin = 7;    // the pin that the pushbutton is attached to
// pins for LEDs
const byte GLedPin = 3;
const byte YLedPin = 4;
const byte RLedPin = 5;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
   // initialize the button pin as a input, enable the internal pullup resistor:
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LEDs as outputs:
   pinMode(GLedPin, OUTPUT);
   pinMode(YLedPin, OUTPUT);
   pinMode(RLedPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}


void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);

      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:
            buttonPushCounter++;  // add one to counter
            if (buttonPushCounter > 3) // if couter over 3 reset the counter to 0 all off
            {
               buttonPushCounter = 0;
            }
            Serial.println(buttonPushCounter);
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
      switch (buttonPushCounter)
      {
         case 0:
            digitalWrite(GLedPin, LOW);
            digitalWrite(YLedPin, LOW);
            digitalWrite(RLedPin, LOW);
            break;
         case 1:
            digitalWrite(GLedPin, HIGH);
            digitalWrite(YLedPin, LOW);
            digitalWrite(RLedPin, LOW);
            break;
         case 2:
            digitalWrite(GLedPin, LOW);
            digitalWrite(YLedPin, HIGH);
            digitalWrite(RLedPin, LOW);
            break;
         case 3:
            digitalWrite(GLedPin, LOW);
            digitalWrite(YLedPin, LOW);
            digitalWrite(RLedPin, HIGH);
            break;
      }
   }
}









I am willing to bet that the effects have long blocking sequences that prevent the code returning to loop() when they are complete, if ever, thus preventing the frequent reading of an input to change effect from one effect to another

This topic Push button to switching Led effects is also probably relevant

Hi,
Is this thread associated with this?

Tom... :smiley: :+1: :coffee: :australia:

How many effects are there? Are they complicated effects? Did you write them, or are they library examples?

As suggested above, please repost your latest working (without button) sketch.

Have you tried changing interval to a small number? Even 0 should be OK(if the contacts aren't bouncy).

What interval? The problem is that the effects loops monopolize the processor. If you eliminate the delays in those, the effects will run 100x too fast. Nobody said anything about switch bouncing or debouncing...

Keep in mind, we haven't seen any sketch at all yet...