2

Okay, I'm having one baffling issue with this code. The counter pushCounterz (named with a z only to rule out a conflicting variable) will start with the correct counter (1 or 0 or whichever) and one of a few things will happen when the button is pushed:

  1. does nothing
  2. correctly toggles the LED but the counter malfunctions in 1 of 2 ways
    1. counter will jump to -255 then on 2nd press resets to 1 and flips with each press between 1 and -255
    2. counter will not increment
    3. counter and/or LED will randomly increment w/o touching anything.
    #include <FastLED.h>
    #define AnalogIn A0
    #define SwIn 2
    #define LED_Out 12
    #define NUM_LEDS 100
    int pushCounterz = 0;
    int buttonState;
    int lastButtonState;   // the previous reading from the input pin
    int ledPin = 13;
    int ledState = HIGH; 

    CRGB leds[NUM_LEDS];

    void setup() {
      // put your setup code here, to run once:
      FastLED.addLeds<WS2812, LED_Out, GRB>(leds, NUM_LEDS);
      //pinMode(SwIn, INPUT);
      pinMode(LED_Out, OUTPUT);
      pinMode(ledPin, OUTPUT);
      //Turn Off strip
      for (int i = 0; i <= NUM_LEDS; i++) {
        leds[i] = CRGB ( 255, 0, 0 );
        FastLED.show();
      }
        digitalWrite(ledPin, ledState);
        Serial.begin(115200);
        Serial.println(pushCounterz);
        lastButtonState = digitalRead(SwIn); // Set the button state to the startup state 
    }

    void loop() {

      buttonState = digitalRead(SwIn);

   if (buttonState == LOW && buttonState != lastButtonState) {
    ledState = !ledState;
   }
   if (buttonState == LOW && buttonState != lastButtonState) {
        if (pushCounterz > 3) {
          Serial.println("Reset to 0: ");
          pushCounterz = 0;
        } else {
          pushCounterz = pushCounterz + 1;
          Serial.println("Incerment");
        }
        Serial.println(pushCounterz);
        switch (pushCounterz) {
          case 0:
            for (int i = 0; i <= NUM_LEDS; i++) {
              leds[i] = CRGB (255, 0, 0);
            }
            break;
          case 1:
            for (int i = 0; i <= NUM_LEDS; i++) {
              leds[i] = CRGB ( 0, 255, 0);
             }
             break;
          case 2:
            for (int i = 0; i <= NUM_LEDS; i++) {
               leds[i] = CRGB ( 0, 0, 255);
             }
            break;
          case 3:
           // theaterChaseRainbow(1,50);
            break;
          default:
           for (int i = 0; i <= NUM_LEDS; i++) {
             leds[i] = CRGB ( 0, 0, 0);
           }
           break;
        }
      }
    FastLED.show();
    digitalWrite(ledPin, ledState);
    lastButtonState = buttonState;
    }

However if I disable the switch code block, everything works flawlessly. I'm suspecting this is a bug in the FastLED library, however I wanted to ask here since I'm decently new to Arduino programming.

EDIT: Code above fixed one issue with the button. I had changed the circuit to be HIGH and drop LOW, however didn't change the code. HOWEVER the state remains inconsistent, sometimes working and sometimes flipping between 1 and -255 (more-so the latter). Summary of change:

void setup() {
...
  lastButtonState = digitalRead(SwIn); // Set the button state to the startup state 
}

  */
  buttonState = digitalRead(SwIn);

  if (buttonState == LOW && buttonState != lastButtonState) {
    ledState = !ledState;
  }
  if (buttonState == LOW && buttonState != lastButtonState) {

EDIT: Serial output to show current craziness (with notes) after above edit:

0 <- initial startup correct
Incerment <- button push
1 <- correct increment
Incerment <- 2nd button push
-255 <- 1 + 1 does NOT equal -255
Incerment <- 3rd button push
1 <- ??? Assuming -255 +1 = 1?
Incerment
-255
Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
Beeker
  • 383
  • 4
  • 20
  • Why is the code outside the function? Where is `loop()`? Why is `pinMode(SwIn, INPUT);` commented out? Do you even have a pulldown resistor like the button tutorial told you to? – gre_gor May 11 '18 at 18:28
  • Sorry, fixed the code to show the entire thing minus the disabled function(s) called in case 3:. I had disabled the pinMode for a test but enabling it does not change anything. Need a one of those fancy arduino molders to show the circuit diagram but it's hooked up like https://www.tinkercad.com/things/9IFe140QD43-button#/ minus the additional LED as I'm using the onboard one. I was originally using the basic tutorial, but it was acting disconnected and constantly flipping states. – Beeker May 11 '18 at 18:50
  • @gre_gor Here's an example of the serial output on startup:1 Incerment 2 Incerment 3 Every button press runs through a reset and then all increments stopping at 3 – Beeker May 11 '18 at 19:40
  • Why are you setting 101 LEDs? You only have 100. – gre_gor May 12 '18 at 02:35
  • There is actually 300 in the string, but just testing 100. I'm baffled by the -255. Not understanding the registers might be my downfall here and why 1+1 = -255 when the case statements are enabled – Beeker May 12 '18 at 02:49

1 Answers1

2

With

for (int i = 0; i <= NUM_LEDS; i++)

you are using the index 100, which is the 101th element of the array.

This is undefined behavior, which in your case causes trouble with pushCounterz.

Fix all your for loops that iterate through leds, by changing the <= operator with <.

for (int i = 0; i < NUM_LEDS; i++)
gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • Yup, that fixed it alright. Sort of a buffer overflow in the sense its writing over some area of memory containing my pushCounterz. It explains the non-deterministic behavior of how adding/removing code blocks changed it's issues. I *KNEW* it was going to be something generally dumb I wasn't seeing. – Beeker May 12 '18 at 05:52