push button input no longer working

hi guys!

since i’ve added a new piece of code (the “rainbow” part, under the void loop) my push button doesnt give any input anymore. can someone please check my code and see if anything is wrong?

btw i am using pin 2 (set HIGH) as my input pin, the negative side of the button runs to GND.

#include <Adafruit_NeoPixel.h>

#define PIN 6
#define NUM_LEDS 45

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);

// constants used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin

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


void setup() {
  
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  //initialize the serial port
  Serial.begin(9600);
   pinMode(buttonPin, INPUT);  
  //initialize the buttonPin as output
digitalWrite(buttonPin, HIGH);  
}

void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
      if(buttonPushCounter==5) {
      buttonPushCounter=0;}
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;


switch (buttonPushCounter){
    
  case 1:
      while (buttonPushCounter==1) {
        rainbow(500);
        break;}
  case 2:
     while (buttonPushCounter==2) {
       for(int i=0 ; i < NUM_LEDS; i++)
        {strip.setPixelColor(i, 128 ,0, 0);
         strip.show();
         delay(50);}
        break;}
  case 3:
     while (buttonPushCounter==3) {
        for(int i=0 ; i < NUM_LEDS; i++)
        {strip.setPixelColor(i, 0 ,128, 0);
         strip.show();
          delay(50);}
        break;} 
  case 4:
     while (buttonPushCounter==4) {
        for(int i=0 ; i < NUM_LEDS; i++)
        {strip.setPixelColor(i, 0 ,0, 128);
       strip.show();
        delay(50);}
        break;}
   }
}
void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

If you haven't installed an external pull up resistor for pin 2, then you'll need to use

pinMode(buttonPin, INPUT_PULLUP);

EDIT: note to self: review more of the code provided by using the scroll box slider.

dlloyd: If you haven't installed an external pull up resistor for pin 2, then you'll need to use

pinMode(buttonPin, INPUT_PULLUP);

Yes but he has done:-

  pinMode(buttonPin, INPUT);  
  //initialize the buttonPin as output
digitalWrite(buttonPin, HIGH);

Which does exactly the same thing.

Maybe the added half second delay is preventing your code from seeing the button press.

The rainbow function takes 128 seconds to complete so the push button is hardly being looked at. Just once every 128 seconds. So with that code you have to keep your finger on the button for a long time.

By the way the while loops in the case statements contain a break in them so the while is totally useless, remove them.

Grumpy_Mike: The rainbow function takes 128 seconds to complete so the push button is hardly being looked at. Just once every 128 seconds. So with that code you have to keep your finger on the button for a long time.

By the way the while loops in the case statements contain a break in them so the while is totally useless, remove them.

i there anyway i can get a hue changing program in my switch case?

i ve googled and tried a lot, but all didnt work out or where to complex to begin with.

You can call a hue changing function from the switch/case but the function must not use delay().

Instead, each time you call the function test how much time has elapsed since the display was last updated by comparing the current value returned by millis() with the value that was returned the previous time the display was updated. If the required period has elapsed then update the variables and output them to strip.setPixelColor() and save the time from millis() ready for next time check.

This is often referred to as the BlinkWithoutDelay method named after the example sketch of the same name in the IDE.

i there anyway i can get a hue changing program in my switch case?

Or you could keep checking the status of the push button inside that very long loop and breaking out if it is pressed. But that is just a kludg, do it right with millis.

UKHeliBob:
You can call a hue changing function from the switch/case but the function must not use delay().

Instead, each time you call the function test how much time has elapsed since the display was last updated by comparing the current value returned by millis() with the value that was returned the previous time the display was updated. If the required period has elapsed then update the variables and output them to strip.setPixelColor() and save the time from millis() ready for next time check.

This is often referred to as the BlinkWithoutDelay method named after the example sketch of the same name in the IDE.

i do not really have a good idea of how to make this… can you show me an example?

See my http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html Or Robin2's several things at once http://forum.arduino.cc/index.php?topic=223286.0

i tried some stuff with the links provided by Mike, but i cannot really get my head around the Millis structure :(

if someone could give me some help with getting this thing start?

let me explain first what i really want:

i have made a lamp with 2 ledstrips of each 45 leds (90 total). i want the lamp to have some nice programs which you can skip through with a button of capacitive touch (the lamp is made fully out of metal). i though switch case would be the best way to program it. but maybe there is a way easier way?

a few steady colors (brightness maybe adjustable with touch or potentiometer), that will build nicely up instead of all leds full on instantaneously. a hue changing program (which speed can be adjusted with touch or potentiometer)

|282x500

edit: is it better if i start a new topic on this?

If you are in a hurry, do what Grumpy_Mike said.

Change your display routine to:

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    // check if a button pressed
    if (digitalRead(buttonPin) != lastButtonState)  // <------------- add this
       return;         // <------------ and this
    delay(wait);
  }
}

thanx you so much nick! this basically solved my issue!

okay next step!

i got my capacitive touch sensor working. whit this code:

#include <CapacitiveSensor.h>
 
 
boolean lightOn=false; // I like booleans
CapacitiveSensor   tapper = CapacitiveSensor(4,2); // 1M resistor between pins 2 & 4, pin 2 is sensor pin
unsigned long lastTap; // Used as a kind of a debounce effect
 
void setup()                    
{
pinMode(13, OUTPUT);
digitalWrite(13, lightOn);
lastTap=millis();
}
 
void loop()                    
{
 
    if (tapper.capacitiveSensor(30) > 1000) {
      if ((millis()-lastTap)>8) {
      lightOn=!lightOn;
      lastTap=millis();
      }
    }
    delay(50);
    digitalWrite(13, lightOn);
 
    delay(100);                             
}

ive tried some stuff to get my lamp to go on and off with touching it for at least 2 seconds. a brief touch should make it show to light programs. but i cant get this working… if someone can help me out ? :slight_smile:

can anyone recommend me a good book on programming arduinos?