Hi Everyone Hope all is well,ive recently begun to code(Total noobie)and wanted to add a momentary switch to my arduino which will be connected to my rgb led lights,what i'm trying to do is to be able to control the different colours of the knight rider,im using NeoPixel-KnightRider library as well as the Adafruit NeoPixel library,with the help of some examples code i was able to find an example code from the neo pixel library which showed me how to control the example code using the momentary switch its just that when ive added the code to example code i keep getting a bunch of errors,i'm hoping someone can point me in the right direction
link to the original knight rider code: NeoPixel-KnightRider/NeoPixel_KnightRider.ino at master · technobly/NeoPixel-KnightRider · GitHub
#include <Adafruit_NeoPixel.h>
// SETUP YOUR OUTPUT PIN AND NUMBER OF PIXELS/Switch
#define BUTTON_PIN 2
#define PIXEL_PIN 6
#define PIXEL_COUNT 16
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);
}
}
// Set the last button state to the old state.
oldState = newState;
}
// 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();
}
void knightRider(uint16_t cycles, uint16_t speed, uint8_t width, uint32_t color) {
uint32_t old_val[NUM_PIXELS]; // up to 256 lights!
// Larson time baby!
for(int i = 0; i < cycles; i++){
for (int count = 1; count<NUM_PIXELS; count++) {
strip.setPixelColor(count, color);
old_val[count] = color;
for(int x = count; x>0; x--) {
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(x-1, old_val[x-1]);
}
strip.show();
delay(speed);
}
for (int count = NUM_PIXELS-1; count>=0; count--) {
strip.setPixelColor(count, color);
old_val[count] = color;
for(int x = count; x<=NUM_PIXELS ;x++) {
old_val[x-1] = dimColor(old_val[x-1], width);
strip.setPixelColor(x+1, old_val[x+1]);
}
strip.show();
delay(speed);
}
}
}
void clearStrip() {
for( int i = 0; i<NUM_PIXELS; i++){
strip.setPixelColor(i, 0x000000); strip.show();
}
}
uint32_t dimColor(uint32_t color, uint8_t width) {
return (((color&0xFF0000)/width)&0xFF0000) + (((color&0x00FF00)/width)&0x00FF00) + (((color&0x0000FF)/width)&0x0000FF);
}
// Using a counter and for() loop, input a value 0 to 251 to get a color value.
// The colors transition like: red - org - ylw - grn - cyn - blue - vio - mag - back to red.
// Entering 255 will give you white, if you need it.
uint32_t colorWheel(byte WheelPos) {
byte state = WheelPos / 21;
void startShow(int i) {
switch(i){
case 0: colorWipe(strip.Color(0, 0, 0), 50); // Black/off
break;
case 1: return strip.Color(255, 0, 255 - ((((WheelPos % 21) + 1) * 6) + 127));
break;
case 2: return strip.Color(255, ((WheelPos % 21) + 1) * 6, 0);
break;
case 3: return strip.Color(255, (((WheelPos % 21) + 1) * 6) + 127, 0);
break;
case 4: return strip.Color(255 - (((WheelPos % 21) + 1) * 6), 255, 0);
break;
case 5: return strip.Color(255 - (((WheelPos % 21) + 1) * 6) + 127, 255,0);
break;
case 6: return strip.Color(0, 255, ((WheelPos % 21) + 1) * 6);
break;
case 7: return strip.Color(0, 255, (((WheelPos % 21) + 1) * 6) + 127);
break;
case 8: return strip.Color(0, 255 - (((WheelPos % 21) + 1) * 6), 255);
break;
case 9: return strip.Color(0, 255 - ((((WheelPos % 21) + 1) * 6) + 127), 255);
break;
case 10: return strip.Color(((WheelPos % 21) + 1) * 6, 0, 255);
break;
case 11: return strip.Color((((WheelPos % 21) + 1) * 6) + 127, 0, 255);
break;
case 12: return strip.Color(255, 0, 255 - (((WheelPos % 21) + 1) * 6));
break;
default l3: return strip.Color(0, 0, 0);
break;
}
}