Go Down

Topic: Toggle Switch To Change LED Animation Not Working (Read 91 times) previous topic - next topic

exitalterego

Hi all,

I'm in the process of making some lighting for a cosplay demo that's taking place this weekend. I've got the desired lighting effect for use while walking around the show but for photo shoots I wanted to halt the animation and just set LED's to a single colour.

To achieve this, I've wired a toggle switch between ground and a digital pin on my UNO and added pinMode, digitalRead and an if statement into my code that, if I understand correctly should toggle between the two LED states based on either a HIGH or LOW state of the switch.

However, what is actually happening is that the LED's animate in one switch state, but simply set themselves to a very low brightness in the other state. They don't change colour or come to full brightness.

Here is my testing sketch:

Code: [Select]
#include <Adafruit_NeoPixel.h>

#define PINL 5
#define PIXL 8  //Number of pixels
#define DELL 2  //Time delay. Lower equals faster
#define BUTPIN 2

Adafruit_NeoPixel stripL = Adafruit_NeoPixel(PIXL, PINL, NEO_GRBW + NEO_KHZ800);

int buttonState=0;

void setup() {
  pinMode(BUTPIN,INPUT_PULLUP);
  stripL.begin();
  stripL.show(); // initialize all pixels to "off"

}

void loop() {
  buttonState=digitalRead(BUTPIN);

  if(buttonState==HIGH){
    stripL.setPixelColor(8,0,255,0,0);
    stripL.setBrightness(255);
  } else {
    brightenL();
    darkenL();
  }
}

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

  for (j = 1; j < 255; j++) {
    for (i = 0; i < stripL.numPixels(); i++) {
      stripL.setPixelColor(i, 255, 22, 197, 0);
      stripL.setBrightness(j);
    }
    stripL.show();
    delay(DELL);
  }
    delay(DELL);
}

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

  for (j = 255; j > 1; j--) {
    for (i = 0; i < stripL.numPixels(); i++) {
      stripL.setPixelColor(i, 255, 22, 197, 0);
      stripL.setBrightness(j);
    }
    stripL.show();
    delay(DELL);
  }
  delay(DELL);
}


If anyone can tell me what's going wrong it would be greatly appreciated!  :smiley:

Robin2

Perhaps in this piece
Code: [Select]
  if(buttonState==HIGH){
    stripL.setPixelColor(8,0,255,0,0);
    stripL.setBrightness(255);
  }

you also need a line
Code: [Select]
stripL.show();

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

exitalterego

Thanks for that! It's doesn't quite work how I was expecting, (the setPixelColor in the if doesn't change the pixel colour), but it does achieve the desired output of setting the LED's to their last programmed colour so I'll take that as a win.

It might not be elegant or best practice, but it works so I'm happy :P

PaulS

Quote
It's doesn't quite work how I was expecting, (the setPixelColor in the if doesn't change the pixel colour
It does, but only for one pixel. Just like in brightenL() and darkenL(), you need a for loop to iterate through the pixels, and set the color of each of them.
The art of getting good answers lies in asking good questions.

Go Up