NEOPIXLE buttoncycler sketch...help understanding the language

pretty new to coding but I’m trying to deconstruct some working code and understand what makes it tick. I am using the buttoncycler example and one of the patterns in particular.

Here is the bit of code that I have.

#include <Adafruit_NeoPixel.h>

#define BUTTON_PIN   5
#define PIXEL_PIN    6
#define PIXEL_COUNT 56


Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

bool oldState = HIGH;
int showType = 0;

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

void loop() {
  // Get current button state.
  bool 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) {
      showType++;
      if (showType > 9)
        showType = 0;
      startShow(showType);
    }
  }


  oldState = newState;
}

void startShow(int i) {
  switch (i) {

    case 0: colorWipe(strip.Color(0, 0, 255), 30);  // Blue
      break;



  }
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for (uint16_t i = 0; i < strip.numPixels(); i++) {
    strip.setPixelColor(c, i);
    strip.show();
    delay(wait);
  }



}

OK, pretty simple right? A button push fills each LED in the strand from #0-#56 with blue. I get it. I also get that by alter the following line

  for (uint16_t i = 0; i < strip.numPixels(); i++) {

and changing the 0 to let’s say 24, I could start filling from #24 on up. What I can’t figure is what makes it go in that direction…from 0 through 56 rather than 56 back down to 0. How would one reverse that and go backwards? I’ve tried a bunch of things but can’t quite figure it. Any insight is apreciated.

bruisedreed:

  for (uint16_t i = 0; i < strip.numPixels(); i++) {

and changing the 0 to let’s say 24, I could start filling from #24 on up. What I can’t figure is what makes it go in that direction…from 0 through 56 rather than 56 back down to 0. How would one reverse that and go backwards? I’ve tried a bunch of things but can’t quite figure it. Any insight is apreciated.

One way to do is is like so:

 for (uint16_t i = strip.numPixels(); i > 0; i--) {
    strip.setPixelColor(c, i - 1);
    ...

Similar to i++ , i– is shorthand for i=i-1
If i were a signed type, you could use >=0 and the subtraction could be made unnecessary.

bruisedreed:

  for (uint16_t i = 0; i < strip.numPixels(); i++) {

and changing the 0 to let’s say 24, I could start filling from #24 on up. What I can’t figure is what makes it go in that direction…from 0 through 56 rather than 56 back down to 0. How would one reverse that and go backwards? I’ve tried a bunch of things but can’t quite figure it. Any insight is apreciated.

Have you considered looking here:

https://www.arduino.cc/reference/en/language/structure/control-structure/for/

The reference area is a great place to learn about how this stuff works. Believe it or not, you’re not the first person to have questions and most of this has already been answered and answered well.

There are lots of things in life that are fun to learn by the, “I’m just going to tweak it and see what happens” method. Coding isn’t one of them. Take my advice and do some reading on this one.

Thanks so much! That makes perfect sense. yeah I have been looking through the reference. Im just a little lost on where to start sometimes. Deconstructing bits of code while wildly inefficient actually does teach me a few things. I’m wading through the reference tables as well I just have trouble sometimes without seeing it in action. Thanks again for the help.

Deconstructing bits of code while wildly inefficient actually does teach me a few things.

Google "C++ tutorial". Pick one of the top few hits. Spend a day or two going through until you hit classes and structs, gloss over those. Come back to looking at your code and realize how much more sense it makes after that.