Go Down

Topic: Using Neopixel Strip to count score (Read 66 times) previous topic - next topic

waynevanwijk

Hello all,
I have learnt a lot from reading these forums over the last few months (thanks to all) and so far I have not had to ask any direct questions... until now.

What I am trying to do should be simple but I can't figure it out.

The goal...
I am trying to use an arduino with a momentary button and a 8 LED NeoPixel strip to act as a score counter.

When the arduino starts up, the neopixel array is off.

Press the button and the first light comes on and stays on.

Press the button a second time and the second light also comes on and stays on.

Each time the button is pressed the next light comes on.

When all the lights are on the sequence stops (in the future I'll make it play an alarm, but that is a job for later.

I have done the wiring correctly and have it running a different bit of code, so I know that the hardware is OK.

Anyway, here is the code that I have stitched together from other people work...

// Keep score with 2 buttons and two neopixel strips.
// press a button and the corresponding strip lights by one LED
// when max score is reached an alarm sounds

// for starters, I'll try to make it work with one button and one neopixel strip



#include <Adafruit_NeoPixel.h>


// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.

#define BUTTON_PIN   2 // Defines the button pin

#define PIXEL_PIN    6  // Digital IO pin connected to the NeoPixels.

#define PIXEL_COUNT   8 // Number of NeoPixels

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_RGBW + NEO_KHZ800);

// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

boolean oldState = HIGH;
int     mode     = 0;    // Currently-active animation mode, 0-9

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'

}

void loop() {
  // Get current button state.
  boolean newState = digitalRead(BUTTON_PIN);

  // Check if state changed from high to low (button press).
  if ((newState == LOW) && (oldState == HIGH)) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(BUTTON_PIN);
    if (newState == LOW) {     // Yes, still low
      switch (mode) {
        case 0:
          colorWipe(strip.Color(0, 255, 0)); // turns the LED on RED
          break;
      }
    }
  }

  // Set the last-read button state to the old state.
  oldState = newState;
}

// Fill strip pixels one after another with a color.

void colorWipe(uint32_t color) {
  for (int i; i < strip.numPixels(); i + 1) { // add 1 pixel to strip
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
  }
}


I was using the colorWipe for my previous project (a time keeper) so tried to make changes to that for my new purpose.

All I am getting is a blank neopixel on startup - good.
When I press the button, the first LED lights up - also good.
When I press the button again no further lights come on.

What am I doing wrong?

wvmarle

Please use code tags (don't tell me you were too good to read the "how to read this forum" sticky - your failure to do so, and follow instructions, is not helping you getting more replies).

I see you're using a switch statement with just a single case - that doesn't make sense, especially as you never change the value of mode.

You do not appear to set any kind of counter for how many LEDs should be on upon button presses.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up