Neopixle if, else if, else with button push

Hi
I am both new on here and new to Arduino programming so please excuse any inadvertent fopars

I am trying to write a simple sketch that switches from one routine to another on a button push

I can get a button to work

I can get both routines to work flawlessly in separate sketches without adding a button but as soon as I try to use a button to call either routine the routine freezes

what I want is

button LOW - routine 1 runs
button High - routine 2 runs

To my way of thinking it should go something like this:-

get button state - if high then routine 2
else routine 1

Here is routine 1
I have rem'd out the button test here
I have tried running serial write to sus whats going on but I can't get that to work on this sketch either :confused:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

const int buttonPin = 2;
int buttonState = 0;
int dimFlag = 0;
int tOff = 0;
int stb = 0;

#define PIN 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);


void setup() {
  

  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  buttonState = digitalRead(buttonPin);
  
//if (buttonState == HIGH)
  
 testOn(strip.Color(222,0,0),50);
  delay(750);
//else
}

//Test On

void testOn(uint32_t c, uint8_t wait) {
  
 if (dimFlag == 0)
       strip.setBrightness(20);
         else if  (dimFlag == 1)
          strip.setBrightness (75);
             else  strip.setBrightness (255);
     
      dimFlag++;

   if (dimFlag >2)
        dimFlag = 0;

      for(uint16_t i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i, c);
          strip.show();
          delay(wait);
}
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);

and here is the second routine

{
 testOff(strip.Color(222,0,0),50);
stb++;
}

void testOff(uint32_t c, uint8_t wait) {

stb == 0;
  if (stb == 0)
  colorWipe(strip.Color(50, 30, 0), 200);
  else 
  
 if (tOff == 0)
       strip.setBrightness(255);
        if (tOff == 5) strip.setBrightness (5);
     
      tOff++;

   //if (tOff >6)
       // tOff = 0;

      for(uint16_t i=0; i<strip.numPixels(); i++) {
          strip.setPixelColor(i, c);
          strip.show();
          delay(wait);

      }
}

Been on this all day now and I just can't see it :frowning:

Many thanks for any help

Welcome to the forums. +1 for using code tags. It is generally better to post the complete sketch inside one set of code tags so people can easily copy and paste it into the IDE.

How do you have the button wired up? You do not declare it inside setup() so it would be of type INPUT which means it requires a pull up or pull down resistor. Do you have one present?

Also, if you want your button to be responsive, you can't do the entire TestOn() or TestOff() routine and then come back and check for the button. A better approach is do only do one step each time through loop() which means you would get rid of all those for() loops. Check out several things at the same time

You may also want to get into the habit of enclosing your if() statements inside curly braces so you know what code goes with the if()/else. Without them, the code is fragile and you can easily break it by inserting a new line of code. You can also use the Ctrl-T in the IDE to auto format your code which will line everything up and show you proper indentation.

Use CTRL T to format your code.

Attach your ‘complete’ sketch.

FYI

Suggest you stick with looking at ‘when a switch changes state’ rather than its level.

blh64:
Welcome to the forums. +1 for using code tags. It is generally better to post the complete sketch inside one set of code tags so people can easily copy and paste it into the IDE.

Thanks for the reply
I posted 2 sets of code because i have both of these running as 2 different sketches
On there own, they both work
The problem i am having is getting the switch to operate the way i want so i can join the 2 sketches together
Evidently it would seem i neef to look at the structure of both sketches
My brain is fried today but i will study the article you pointed me too tomoz and see if i csn work out whats going on

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