I used this: Arduino Code | Larson Scanner Shades (Trinket-Powered NeoPixel LED Strip Glasses) | Adafruit Learning System and changed to use fastled library as it'd be easier to do color cycling.
#include <FastLED.h>
#define N_LEDS 10 // total number of LEDs on the strip. Higher LED makes smoother runs but uses more power
#define PIN 3 // usually any digital pin other than 0 and 1 works
#define LED_TYPE WS2812B // I assume you have WS2812B leds, if not just change it to whatever you have
#define HIBRIGHT 255 // Control the brightness of your leds, this is for center
#define MIDBRIGHT 127 // medium brightness for those on either side of the center
#define LOWBRIGHT 63 // low brightness for the fading part
#define SATURATION 127 // Control the saturation of your leds
int pos = 0;
int dir = 1; // Position, direction of "eye"
byte j = 0;
CRGB leds[N_LEDS]; // initial setup of LED array
void setup() {
Serial.begin(9600);
FastLED.addLeds<LED_TYPE, PIN>(leds, N_LEDS);
}
void loop() {
// Draw 5 pixels centered on pos. code will clip any LED
// off the ends of the strip, we don't need to watch for that.
leds[pos - 2] = CHSV(j++, SATURATION, LOWBRIGHT);
leds[pos - 1] = CHSV(j++, SATURATION, MIDBRIGHT);
leds[pos] = CHSV(j++, SATURATION, HIBRIGHT);
leds[pos + 1] = CHSV(j++, SATURATION, MIDBRIGHT);
leds[pos + 2] = CHSV(j++, SATURATION, LOWBRIGHT);
FastLED.show();
// since I'm using byte variable for J, it will roll over to 0
pos += dir; // Time to check and bounce off ends of strip
if (pos <= 0) { // reached one end, change direction
pos = 1;
dir = 1;
}
if (pos >= N_LEDS) { // reached other end, change direction
pos = N_LEDS - 2;
dir = -1;
}
delay(80); // smaller number for faster sweep
FastLED.clear(); // clear all pixel data
FastLED.show(); // code cycles back to top and displays LED before you can see it going off
Serial.print(" Hue ");
Serial.print(j);
Serial.print(" Pos ");
Serial.print(pos);
Serial.print(" Dir ");
Serial.println(dir);
}
Problem #1: checking the serial monitor when LED pos reached N_LEDS (set to 10), it's supposed to change dir to -1, set POS to 8 and also add dir to pos (8 + -1 should be 7). So when LED is chasing forward, dir is adding +1 to pos but when LED reaches the end and dir changes to -1 pos does not get deducted. It remains stuck at 8 (N_LEDS - 2).
For some reason the exact same code works as originally used with Neopixel library, maybe something didn't get translated over or I need to change variable?
#2 Also I don't think j variable is adding up correctly. When I skip the reversing and let it run forward and cycle back to 1 after reaching 255 (used byte variable to auto rollover to 0), j resets after reaching end of cycle rather than continuing toward 255
example of serial monitor: (Hue is j variable for LED color, Pos is pos variable, which LED is the center, and Dir is dir, 1 or -1 to indicate direction of scan
Hue 0 Pos 0 Dir 1
Hue 5 Pos 1 Dir 1
Hue 10 Pos 2 Dir 1
Hue 15 Pos 3 Dir 1
Hue 20 Pos 4 Dir 1
Hue 25 Pos 5 Dir 1
Hue 30 Pos 6 Dir 1
Hue 35 Pos 7 Dir 1
Hue 40 Pos 8 Dir 1
Looks normal up to here then dir changes as expected, POS does not change at all Also Hue resets to 1 and doesn't change while LED remains stuck at the end
Hue 1 Pos 8 Dir -1
Hue 1 Pos 8 Dir -1
Hue 1 Pos 8 Dir -1
Hue 1 Pos 8 Dir -1
Hue 1 Pos 8 Dir -1
Can anyone look at the code and see what I messed up so I can get the Larson scanner working correctly and also color cycling smoothly?