Hi,
I am working on a project based on this Knight Rider Effect.
Original code: https://github.com/technobly/NeoPixel-KnightRider
The problem I have is that the lights are not displaying correctly, but the debug output on the serial is correct.
I use button NeoPixel RGBW. I build my code to use an array, because my final project is a 40 led matrix and the leds will light based on an array selection.
Arduino nano rp2040 connect
74AHC125 as Logic level shifter
Images of the Circuit and Breadboard are available below.
Example of the serial output:
cycle =
0
count = 0
led id = 6
color = 7F00
led color
6 7F00
5 10003DFB
4 20001578
3 100145B8
2 2
1 2
0 20001BE4
count = 1
led id = 5
color = 7F00
dimColor = 1F00
led color
6 1F00
5 7F00
4 20001578
3 100145B8
2 2
1 2
0 20001BE4
count = 2
led id = 4
color = 7F00
dimColor = 1F00
dimColor = 700
led color
6 700
5 1F00
4 7F00
3 100145B8
2 2
1 2
0 20001BE4
count = 3
led id = 3
color = 7F00
dimColor = 1F00
dimColor = 700
dimColor = 100
led color
6 100
5 700
4 1F00
3 7F00
2 2
1 2
0 20001BE4
count = 4
led id = 2
color = 7F00
dimColor = 1F00
dimColor = 700
dimColor = 100
dimColor = 0
led color
6 0
5 100
4 700
3 1F00
2 7F00
1 2
0 20001BE4
count = 5
led id = 1
color = 7F00
dimColor = 1F00
dimColor = 700
dimColor = 100
dimColor = 0
dimColor = 0
led color
6 0
5 0
4 100
3 700
2 1F00
1 7F00
0 20001BE4
count = 6
led id = 0
color = 7F00
dimColor = 1F00
dimColor = 700
dimColor = 100
dimColor = 0
dimColor = 0
dimColor = 0
led color
6 0
5 0
4 0
3 100
2 700
1 1F00
0 7F00
My full code is here, still a lot of serail.print for debugging and temp comments in it.
// NeoPixel test program showing use of the WHITE channel for RGBW
// pixels only (won't look correct on regular RGB NeoPixel strips).
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN 25
// How many NeoPixels are attached to the Arduino?
#define NUM_PIXELS 7
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 127 // Set BRIGHTNESS to about 1/2 (max = 255)
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(NUM_PIXELS, LED_PIN, NEO_WGRB + 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_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
// Define Arrays for Main Curves
uint32_t array1[] = {6, 5, 4, 3, 2, 1, 0};
uint32_t array2[] = {0, 1, 2, 3, 4, 5, 6};
uint8_t arrayLen = 7;
void setup() {
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
//#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
// clock_prescale_set(clock_div_1);
//#endif
// END of Trinket-specific code.
Serial.begin(9600);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
clearStrip(); // Initialize all pixels to 'off'
// strip.show(); // Turn OFF all pixels ASAP
delay(1000);
// strip.setBrightness(BRIGHTNESS);
}
void loop() {
uint8_t arrayLen = sizeof(array1) / sizeof(array1[0]);
Serial.print("arrayLen = ");
Serial.println(arrayLen);
knightRider(3, 500, 4, arrayLen, 0x00007F00); // Cycles, Speed, Width, RGB Color (light red) 0xWWGGRRBB
clearStrip();
delay(2000);
// Iterate through a whole rainbow of colors
// for(byte j=0; j<252; j+=7) {
// knightRider(1, 16, 2, colorWheel(j)); // Cycles, Speed, Width, RGB Color
// }
// clearStrip();
// delay(2000);
}
// Cycles - one cycle is scanning through all pixels left then right (or right then left)
// Speed - how fast one cycle is (32 with 16 pixels is default KnightRider speed)
// Width - how wide the trail effect is on the fading out LEDs. The original display used
// light bulbs, so they have a persistance when turning off. This creates a trail.
// Effective range is 2 - 8, 4 is default for 16 pixels. Play with this.
// Color - 32-bit packed RGB color value. All pixels will be this color.
// knightRider(cycles, speed, width, color);
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint8_t len, uint32_t color) {
uint32_t old_val[len]; // up to 256 lights!
Serial.print("old_val = ");
for(int val = 0; val < len; val++) {
Serial.println(old_val[val], HEX);
}
Serial.println();
// Larson time baby!
for(int i = 0; i < cycles; i++){ // décompte cycles 0 à cycles
Serial.println("cycle = ");
Serial.println(i);
Serial.println("\n");
for (int count = 0; count < len; count++) { //décompte position in array 0 to len
Serial.print("count = ");
Serial.println(count);
Serial.print("led id = ");
Serial.println(array1[count]);
// for (int count : curve1) { //décompte LED_ID de 1 à LED_ID
// for (int count = 1; count < NUM_PIXELS; count++) { //Original for function
strip.setPixelColor(array1[count], color);
old_val[count] = color;
Serial.print("color = ");
Serial.println(color, HEX);
for(int x = count; x > 0; x--) { //décompte LED_ID de LED_ID Actuel à 0
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(array1[count-1], old_val[x-1]);
Serial.print("dimColor = ");
Serial.println(old_val[x-1], HEX);
}
strip.show();
delay(speed);
Serial.print("led"); Serial.print("\t");Serial.println("color");
Serial.print(array1[0]); Serial.print("\t");Serial.println(old_val[0], HEX);
Serial.print(array1[1]); Serial.print("\t");Serial.println(old_val[1], HEX);
Serial.print(array1[2]); Serial.print("\t");Serial.println(old_val[2], HEX);
Serial.print(array1[3]); Serial.print("\t");Serial.println(old_val[3], HEX);
Serial.print(array1[4]); Serial.print("\t");Serial.println(old_val[4], HEX);
Serial.print(array1[5]); Serial.print("\t");Serial.println(old_val[5], HEX);
Serial.print(array1[6]); Serial.print("\t");Serial.println(old_val[6], HEX);
}
}
}
void clearStrip() {
for( int i = 0; i < arrayLen; i++){
strip.setPixelColor(i, 0x00000000); strip.show();
}
}
uint32_t dimColor(uint32_t color, uint8_t width) {
return (((color&0xFF000000)/width)&0xFF000000) + (((color&0x00FF0000)/width)&0x00FF0000) + (((color&0x0000FF00)/width)&0x0000FF00) + (((color&0x000000FF)/width)&0x000000FF);
}