Button/Switch Troubles

Hello! I'm trying to modify the example button code two switch between two LED modes on an LED strip by using a slide switch. When I try to switch modes, however, only one of them stays on. I'm thinking this may be a time element, thing, so if someone doesn't mind looking at my code I'd appreciate it! The wiring/materials I'm working with are identical to the ones found in this video: How to control LED Strips with Arduino - Cosplay Tutorial - YouTube

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

#define PIN 6

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

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code

  strip.begin();
  strip.setBrightness(50);
  strip.show(); // Initialize all pixels to 'off'
  
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    brighten();
    darken();
  } else {
    colorWipe(strip.Color(255, 245, 0), 50);
  }
}

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);
  }
}

//Pulse
// 0 to 255
void brighten() {
  uint16_t i, j;

  for (j = 0; j < 255; j++) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j, 0);
    }
    strip.show();
    delay(10);
  }
  delay(100);
}

// 255 to 0
void darken() {
  Serial.begin(9600);
  uint16_t i, j;

  for (j = 255; j > 0; j--) {
    for (i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, j, j, 0);
    }
    strip.show();
    delay(10);
    Serial.println(j);
  }
  delay(100);
}

Wiring:

Your post was MOVED to its current location as it is more suitable

How is the switch wired ?

Is there a resistor keeping the pin in a known state at all times or is it floating at an unknown voltage (maybe HIGH, maybe LOW) when the switch contacts are open ?

If there is no resistor present then try using INPUT_PULUP in pinMode() to activate the built in pullup resistor and gave the switch take the pin to GND when its contacts are closed

What if we don't want to take the time to dig through a video to see the wiring? Please post a schematic, here.

I was unaware that you could attach images as this is my first time posting on an arduino forum. A schematic is now included, but I felt that including the link is a good way of providing extra information, especially if someone else comes looking for a similar answer.

Hello. I attached the schematic to the og post, but my set up doesn't include the resistors shown (I didn't have the correct resistors available to me). Would I use INPUT_PULUP in both the LED and button pinModes?

I would wire the switch as shown.

With the switch in the position shown it will read LOW because the pin is connected to ground. In the other position it will read HIGH because the pin is connected to 5V through the internal pullup.

Schematic:

SPST DIG IN

You're a lifesaver, tysm!

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