Problem with push button

I’m new to Arduino and after reading a lot of tutorials I started my first build and came across the following problem.

  • Arduino Nano ATMega328P
  • WS2812B LED stripes
  • Push button from starter kit

I attached a picture of my current build. It’s supposed to change all the LED’s colours on a button press.

When I used it without the button, everything worked fine.
But then I wanted to add the button and I did not really find clear instructions on how to solder it. Maybe this is already the problem? Or can a missing resistor cause the button not to work at all?
The funny thing is, the button does nothing, but sometimes when I touch the cable around the button it gives me a HIGH button state, verifying my code by switching colours.

I’m thankful for any help.
Here is my code:

#include <FastLED.h>

#define DATA_PIN 5
#define BUTTON_PIN 11
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 29                                                                                                                       
#define BRIGHTNESS 96

CRGB leds[NUM_LEDS];

int button_state = 0; //variable for reading the pin status HIGH or LOW
int cycle_cnt = 0; //counts the cycle which we are currently in

void setup() {
  delay(3000); // initial delay of a few seconds is recommended
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // initializes LED strip
  FastLED.setBrightness(BRIGHTNESS);// global brightness
  pinMode(BUTTON_PIN, INPUT);
}

// switches off all LEDs
void showProgramCleanUp(long delayTime) {
  for (int i = 0; i < NUM_LEDS; ++i) {
    leds[i] = CRGB::Black;
  }
  FastLED.show();
  delay(delayTime);
}

void switchColor(CRGB crgb) {
    for (int i = 0; i < NUM_LEDS; ++i) {
      leds[i] = (crgb); // hue, saturation, value
    }
    FastLED.show();
    delay(100);
}

void switchColorDisco() {
    for (int i = 0; i < NUM_LEDS; ++i) {
      leds[i] = (CHSV(random8(),255,255)); // hue, saturation, value
    }
    FastLED.show();
    delay(100);
}

// main program
void loop() {

  button_state = digitalRead(BUTTON_PIN);

  if (button_state == HIGH) { //button released
    showProgramCleanUp(1000);
    cycle_cnt++;
  }
  
  switch(cycle_cnt) {
      case 1: switchColor(CRGB::Yellow);
              break;
      case 2: switchColor(CRGB::Red);            
              break;
      case 3: switchColor(CRGB::Purple);
              break;
      case 4: switchColorDisco();
              break;
      case 5: showProgramCleanUp(1000);
              cycle_cnt = 0;
    }  
}

How exactly is the button connected, one side to the pin and the other to 5V or to ground? Do you have any pull-up or pull-down resistor? The fact that you're looking for a HIGH but the comment says the button is released is a bit confusing.

Steve

You can test the button with the [u]Digital Read Serial Example[/u].

The button can be wired the opposite way (with a pull-up resistor) and the readings will be reversed (high when the button is not pushed, low when it's pushed). The advantage of a pull-up is that there are [u]optional internal pull-ups[/u] that you can enable so you don't need the external resistor.

You can wire the button either way as long as you adjust the software logic to match what you're doing.

See input examples:

2017-08-05_20-33-24.jpg

You have the button connected between D11 (BUTTON_PIN) and ground. That's fine, except you gave no means for the pin to ever go high with "pinMode(BUTTON_PIN, INPUT);". That will not apply 5V to the pin, ever.

I think what you meant to do was

pinMode(BUTTON_PIN, INPUT_PULLUP);

That places the pin in a HIGH state until you push the button and pull the pin LOW.

You could also accomplish this with a 10K resistor connected between 5V and D11, keeping the button hooked up as is.

Thanks for your help, guys.

So, regarding to @larryd’s input example (S3) I have connected it correctly. One cable to the pin, one to the ground. And that makes my button an internal pullup button?
That’s why I have to use pinMode(BUTTON_PIN, INPUT_PULLUP) in my code like @tinman13kup suggested and check for LOWs instead of HIGHS. Right so far?
And with that I would not need any resistors.

Another dumb question just to be sure: the button has those 4 spiderlegs. And I solded the cables diagonally, like it looked on the breadboard. How do these buttons work internally? Could I have solded the cables to the wrong legs?

Because I adjusted the code and it does not seem to work still.

#include <FastLED.h>

#define DATA_PIN 5
#define BUTTON_PIN 11
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 29
#define BRIGHTNESS 96

CRGB leds[NUM_LEDS];

int button_state = 0; //variable for reading the pin status HIGH or LOW
int cycle_cnt = 0; //counts the cycle which we are currently in

void setup() {
  delay(3000); // initial delay of a few seconds is recommended
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // initializes LED strip
  FastLED.setBrightness(BRIGHTNESS);// global brightness
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

// switches off all LEDs
void showProgramCleanUp(long delayTime) {
  for (int i = 0; i < NUM_LEDS; ++i) {
    leds[i] = CRGB::Black;
  }
  FastLED.show();
  delay(delayTime);
}

void switchColor(CRGB crgb) {
    for (int i = 0; i < NUM_LEDS; ++i) {
      leds[i] = (crgb); // hue, saturation, value
    }
    FastLED.show();
    delay(100);
}

void switchColorDisco() {
    for (int i = 0; i < NUM_LEDS; ++i) {
      leds[i] = (CHSV(random8(),255,255)); // hue, saturation, value
    }
    FastLED.show();
    delay(100);
}

// main program
void loop() {

  button_state = digitalRead(BUTTON_PIN);

  if (button_state == LOW) { //button pressed
    showProgramCleanUp(1000);
    cycle_cnt++;
  }
  
  switch(cycle_cnt) {
      case 1: switchColor(CRGB::Yellow);
              break;
      case 2: switchColor(CRGB::Red);            
              break;
      case 3: switchColor(CRGB::Purple);
              break;
      case 4: switchColorDisco();
              break;
      case 5: showProgramCleanUp(1000);
              cycle_cnt = 0;
    }  
}
[code]

[/code]

donesteban:
How do these buttons work internally?

See bottom-right of this pic

square buttons.jpg

wilfredmedlin:
See bottom-right of this pic

Thanks, good to know. So I must always connect leg 4 with 1 or leg 2 with 3 and best cut off the unused legs.

donesteban:
best cut off the unused legs.

Or buy two-legged rectangular ones.

rect button.GIF

But I think the square ones probably have 4 legs for mechanical stability?

Everything is working now!
Thanks everyone.

Suggest you look at this type of switch.
Modify some header pins as seen in the image.

2017-01-04_10-07-45.jpg