Program code.
#include <avr/sleep.h>
#include <Adafruit_NeoPixel.h>
// GPIO pin defines
#define Neo_Pin 3 // Data In pin of Neo Pixel
#define SOL A0 // Solar Panel input
#define Neo_Count 8 // number of Neo pixels in strip
#define NeoBrightness 1 // lowest brightness
uint8_t p[7][3] = { 255, 0, 0, // Red Tried difference numbers, dont really make a difference
255,255, 0, // Yellow 255,255,0 255,128,0 128,255,0 all looked the same at low intensity as expected
0,255, 0, // Green
0,255,255, // Cyan
0, 0,255, // Blue
255, 0,255, // Purple
255,255,255}; // White
// 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)
Adafruit_NeoPixel NeoStrip(Neo_Count, Neo_Pin, NEO_GRB + NEO_KHZ800);
//=================================================================================
void setup() {
//Serial.begin(115200);
//Serial.println("================");
//Serial.println(" LED Blink ");
//Serial.println("================");
// === Setup Devices ===
NeoStrip.begin(); // INITIALIZE NeoPixel 8 LED strip
NeoStrip.show(); // Turn OFF all pixels
NeoStrip.setBrightness(NeoBrightness); // Set BRIGHTNESS of NEO pixels
randomSeed(analogRead(1)+analogRead(2)+analogRead(3)+analogRead(4)+analogRead(5)+analogRead(6));
Clear_NEO();
delay(1000);
}
//=================================================================================
void loop() {
Set_Brightness();
uint8_t disp = random(5);
switch (disp) {
case 0:
for (int i = 0; i < 7; i++) {
ALL_Neo(i);
delay(250);
}
for (int i = 5; i > -1; i--) {
ALL_Neo(i);
delay(250);
}
delay(250);
break;
case 1:
for (uint8_t i = 0; i < 10; i++) {
ALL_Neo(random(7));
delay(500);
Clear_NEO();
delay(250);
}
delay(250);
break;
case 2:
Glow(random(7),0,random(1,4));
delay(500);
break;
case 3:
Cylon(random(7),random(1,4),random(3,9));
delay(500);
break;
case 4:
for (uint8_t i = 0; i < random(1,4); i++) {
rainbow(5, 10);
delay(50);
}
break;
default:
Glow(0,1,random(1,4)); // Red colour
delay(500);
break;
}
Clear_NEO();
delay(100);
}
//=================================================================================
void Set_Brightness(void) {
unsigned int solar = map(analogRead(SOL),0,1023,1,100);
NeoStrip.setBrightness(solar);
return;
}
//=================================================================================
// LED move across strip
// c16 = colour to use, n = number of pixels to move 1 2 3, l = number of loops
//=================================================================================
void Cylon(uint8_t c16, uint8_t n, uint8_t l) {
uint32_t color = NeoStrip.Color(p[c16][0],p[c16][1],p[c16][2]);
unsigned int t = NeoStrip.numPixels();
if (n < 1) n = 1;
if (n > 3) n = 3;
for(unsigned int y = 0; y < l; y++) {
for(unsigned int i = 0; i < t-n; i++) {
NeoStrip.setPixelColor(i, color);
if (n > 1) NeoStrip.setPixelColor(i+1, color);
if (n > 2) NeoStrip.setPixelColor(i+2, color);
NeoStrip.show();
delay(100);
Clear_NEO();
}
for(int i = t-1; i > n-1; i--) {
NeoStrip.setPixelColor(i, color);
if (n > 1) NeoStrip.setPixelColor(i-1, color);
if (n > 2) NeoStrip.setPixelColor(i-2, color);
NeoStrip.show();
delay(100);
Clear_NEO();
}
}
return;
}
//=================================================================================
// Increase and decrease the brightness of all LEDs
// r = 0 random colour each loop, r = 1 use Colour, l = number of loops
//=================================================================================
void Glow(uint8_t Colour, bool r, uint8_t l) {
for (uint8_t y = 0; y < l; y++) {
if (!r) { Colour = random(15); }
for (int i = 0; i < 100; i++) {
NeoStrip.setBrightness(i);
ALL_Neo(Colour);
delay(5);
}
for (int i = 99; i > -1; i--) {
NeoStrip.setBrightness(i);
ALL_Neo(Colour);
delay(5);
}
delay(250);
}
return;
}
//=================================================================================
// Set all pixels to off
//=================================================================================
void Clear_NEO(void) {
for(unsigned int i = 0; i < NeoStrip.numPixels(); i++) NeoStrip.setPixelColor(i, 0);
NeoStrip.show();
return;
}
//=================================================================================
// Display same colour on all pixels
//=================================================================================
void ALL_Neo(uint8_t c16) {
uint32_t color = NeoStrip.Color(p[c16][0],p[c16][1],p[c16][2]);
for(unsigned int i = 0; i < NeoStrip.numPixels(); i++) NeoStrip.setPixelColor(i, color);
NeoStrip.show();
return;
}
//=================================================================================
void rainbow(unsigned int wait, unsigned int rainbowLoops) {
int fadeVal=0, fadeMax=100;
for(uint32_t firstPixelHue = 0; firstPixelHue < rainbowLoops*65536; firstPixelHue += 512) {
for(unsigned int i = 0; i < NeoStrip.numPixels(); i++) {
uint32_t pixelHue = firstPixelHue + (i * 65536L / NeoStrip.numPixels());
NeoStrip.setPixelColor(i, NeoStrip.gamma32(NeoStrip.ColorHSV(pixelHue,255,255 * fadeVal / fadeMax)));
}
NeoStrip.show();
delay(wait);
if(firstPixelHue < 65536) {
if(fadeVal < fadeMax) fadeVal++;
} else {
if(firstPixelHue >= ((rainbowLoops-1) * 65536)) {
if(fadeVal > 0) { fadeVal--; }
} else {
fadeVal = fadeMax;
}
}
}
return;
}
//=================================================================================
//=================================================================================
//=================================================================================
Sketch uses 5900 bytes (18%) of program storage space. Maximum is 32256 bytes.
Global variables use 67 bytes (3%) of dynamic memory, leaving 1981 bytes for local variables. Maximum is 2048 bytes.
Edit: fixed error in code. Before I posted the code I changed part of it and did not test compile it. (void int
is not a valid function variable it should be just void
in Set_Brightness
)