#include "Arduino.h"
#include <FastLED.h>
#include <Bounce2.h>
#define LED_PIN 2 //LED Strip Signal Connection
#define PatternPin 3 //Pattern Change Push Button Pin
#define ColourPin 4 //Colour Change Push Button Pin
#define NUM_LEDS 24 //Number of LEDs in the strip
CRGB leds[NUM_LEDS];
int Brightness = 125; //Set the brightness value here MAX - 255
int r, g, b = 0;
int numb1=1;
int pres1=0;
int numb2=1;
int pres2=0;
int SysON = 2;
int SPEED1 = 60;
int SPEED2 = 40;
int SPEED3 = 150;
byte buttonState = 0;
const unsigned long debouncerInterval = 100;
unsigned long buttonPressedTime = 0;
unsigned long currentMillis = 0;
Bounce debouncer = Bounce();
void setup()
{
Serial.begin(9600);
pinMode(PatternPin, INPUT_PULLUP);
pinMode(ColourPin,INPUT_PULLUP);
//debouncer.attach(ColourPin, INPUT_PULLUP);
//debouncer.interval(debouncerInterval);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
}
void loop()
{
Serial.println(SysON);
ColourButtonScan();
LongPress();
if (SysON == 1)
{
PatternButtonScan();
switch (numb1)
{
case 1: //White
r = 200;
g = 200;
b = 255;
break;
case 2: //Red
r = 255;
g = b = 0;
break;
case 3: //Blue
b = 255;
r = g = 0;
break;
case 4: //Yellow
r = g = 255;
b = 0;
break;
case 5: //Orange
r = 255;
g = 71;
b = 0;
break;
case 6: //Green
g = 255;
r = b = 0;
break;
case 7: //Purple
r = 160;
g = 32;
b = 240;
break;
case 8: //Pink
r = 255;
g = 0;
b = 127;
break;
case 9: //Light Blue
r = 5;
g = 195;
b = 221;
break;
case 10: //Rainbow
r = 0;
g = 0;
b = 0;
break;
}
switch (numb2)
{
case 1:
if (numb1 < 10)
{
P1(r, g, b);
}
if (numb1 >= 10)
{
rainbow_solid();
}
break;
case 2:
if (numb1 < 10)
{
P2(r, g, b);
}
if (numb1 >= 10)
{
uint8_t thisSpeed = 80;
uint8_t deltaHue= 10;
uint8_t thisHue = beat8(thisSpeed,255);
rainbow_shade(leds, NUM_LEDS, thisHue, deltaHue);
FastLED.show();
}
break;
case 3:
if (numb1 < 10)
{
P3(r, g, b);
}
if (numb1 >= 10)
{
rainbow_flash();
}
break;
case 4:
if (numb1 < 10)
{
P4(r, g, b);
}
if (numb1 >= 10)
{
rainbow_scroll();
}
break;
}
}
if (SysON == 2)
{
Full(0,0,0);
}
}
void P1(int r, int g, int b)
{
FastLED.setBrightness(Brightness);
Full(r,g,b);PatternButtonScan();ColourButtonScan();
}
void P2(int r, int g, int b)
{
FastLED.setBrightness(Brightness);
for (int j = 0; j <= (Brightness/2); j++)
{
Full(r,g,b);PatternButtonScan();ColourButtonScan();
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(r,g,b);PatternButtonScan();ColourButtonScan();
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
}
void P3(int r, int g, int b)
{
FastLED.setBrightness(Brightness);
FastLED.clear();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(r/4,g/4,b/4);
leds[i] = CRGB(r/2,g/2,b/2);
leds[i+1] = CRGB(r,g,b);
leds[i+2] = CRGB(r,g,b);
leds[i+3] = CRGB(r,g,b);
FastLED.show();PatternButtonScan(); ColourButtonScan();
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
}
void P4(int r, int g, int b)
{
FastLED.setBrightness(Brightness);
FastLED.clear();
for (int i=0; i < NUM_LEDS-2; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[NUM_LEDS-i+1] = CRGB(0, 0, 0);
leds[i-1] = CRGB(r/4,g/4,b/4);
leds[NUM_LEDS-i] = CRGB(r/4,g/4,b/4);
leds[i] = CRGB(r/2,g/2,b/2);
leds[NUM_LEDS-i-1] = CRGB(r/2,g/2,b/2);
leds[i+1] = CRGB(r,g,b);
leds[NUM_LEDS-i-2] = CRGB(r,g,b);
leds[i+2] = CRGB(r,g,b);
leds[NUM_LEDS-i-3] = CRGB(r,g,b);
leds[i+3] = CRGB(r,g,b);
leds[NUM_LEDS-i-4] = CRGB(r,g,b);
FastLED.show();PatternButtonScan(); ColourButtonScan();
delay(SPEED2);
}
FastLED.show();
}
int Full(int i, int j, int k)
{
fill_solid(leds, NUM_LEDS, CRGB(i,j,k));
FastLED.show();
}
void rainbow_solid()
{
FastLED.setBrightness(Brightness);
FastLED.clear();
leds[0] = CRGB(255, 0, 0);
leds[1] = CRGB(255, 0, 0);
leds[2] = CRGB(255, 0, 0);
leds[3] = CRGB(255, 0, 0);
leds[4] = CRGB(255, 71, 0);
leds[5] = CRGB(255, 71, 0);
leds[6] = CRGB(255, 71, 0);
leds[7] = CRGB(255, 71, 0);
leds[8] = CRGB(255, 255, 0);
leds[9] = CRGB(255, 255, 0);
leds[10] = CRGB(255, 255, 0);
leds[11] = CRGB(255, 255, 0);
leds[12] = CRGB(0, 255, 0);
leds[13] = CRGB(0, 255, 0);
leds[14] = CRGB(0, 255, 0);
leds[15] = CRGB(0, 255, 0);
leds[16] = CRGB(0, 0, 255);
leds[17] = CRGB(0, 0, 255);
leds[18] = CRGB(0, 0, 255);
leds[19] = CRGB(0, 0, 255);
leds[20] = CRGB(160, 32, 240);
leds[21] = CRGB(160, 32, 240);
leds[22] = CRGB(160, 32, 240);
leds[23] = CRGB(160, 32, 240);
FastLED.show();PatternButtonScan();ColourButtonScan();
}
void rainbow_shade( struct CRGB * pFirstLED, int numToFill, uint8_t initialhue, uint8_t deltahue )
{
CHSV hsv;
hsv.hue = initialhue;
hsv.val = 255;
hsv.sat = 240;
for( int i = 0; i < numToFill; i++) {
pFirstLED[i] = hsv;
hsv.hue -= deltahue;
}
}
void rainbow_flash()
{
for (int j = 0; j <= (Brightness/2); j++)
{
Full(255,0,0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(255,0,0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = 0; j <= (Brightness/2); j++)
{
Full(255, 71, 0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(255, 71, 0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = 0; j <= (Brightness/2); j++)
{
Full(255,255,0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(255,255,0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = 0; j <= (Brightness/2); j++)
{
Full(0,255,0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(0,255,0);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = 0; j <= (Brightness/2); j++)
{
Full(0,0,255);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(0,0,255);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = 0; j <= (Brightness/2); j++)
{
Full(160,32,240);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
for (int j = (Brightness/2); j >= 0; j--)
{
Full(160,32,240);PatternButtonScan();ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
FastLED.setBrightness(j*2);
FastLED.show();
delay (1);
}
}
void rainbow_scroll()
{
FastLED.setBrightness(Brightness);
FastLED.clear();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(255/4,0/4,0/4);
leds[i] = CRGB(255/2,0/2,0/2);
leds[i+1] = CRGB(255,0,0);
leds[i+2] = CRGB(255,0,0);
leds[i+3] = CRGB(255,0,0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(255/4,71/4,0/4);
leds[i] = CRGB(255/2,71/2,0/2);
leds[i+1] = CRGB(255,71,0);
leds[i+2] = CRGB(255,71,0);
leds[i+3] = CRGB(255,71,0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(255/4,255/4,0/4);
leds[i] = CRGB(255/2,255/2,0/2);
leds[i+1] = CRGB(255,255,0);
leds[i+2] = CRGB(255,255,0);
leds[i+3] = CRGB(255,255,0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(0/4,255/4,0/4);
leds[i] = CRGB(0/2,255/2,0/2);
leds[i+1] = CRGB(0,255,0);
leds[i+2] = CRGB(0,255,0);
leds[i+3] = CRGB(0,255,0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(0/4,0/4,255/4);
leds[i] = CRGB(0/2,0/2,255/2);
leds[i+1] = CRGB(0,0,255);
leds[i+2] = CRGB(0,0,255);
leds[i+3] = CRGB(0,0,255);
FastLED.show();PatternButtonScan(); ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
for (int i=0; i < NUM_LEDS; i++)
{
leds[i-2] = CRGB(0, 0, 0);
leds[i-1] = CRGB(160/4,32/4,240/4);
leds[i] = CRGB(160/2,32/2,240/2);
leds[i+1] = CRGB(160,32,240);
leds[i+2] = CRGB(160,32,240);
leds[i+3] = CRGB(160,32,240);
FastLED.show();PatternButtonScan(); ColourButtonScan();
if(digitalRead(PatternPin)==0)
{
break;
}
delay(SPEED2);
}
FastLED.show();
leds[NUM_LEDS-1] = CRGB(0, 0, 0);
leds[NUM_LEDS-2] = CRGB(0, 0, 0);
FastLED.show();PatternButtonScan(); ColourButtonScan();
}
void ColourButtonScan()
{
if(digitalRead(ColourPin)==0)
{
if(pres1==0)
{
numb1++;
pres1=1;
}
}
else
{
pres1=0;
}
if(numb1 >= 11)
{
numb1 = 1;
}
}
void PatternButtonScan()
{
if(digitalRead(PatternPin)==0)
{
if(pres2==0)
{
numb2++;
pres2=1;
}
}
else
{
pres2=0;
}
if(numb2 >= 5)
{
numb2 = 1;
}
}
void LongPress()
{
int x=digitalRead(ColourPin);
if (x==0)
{
if(buttonState == 0){
buttonState = 1;
currentMillis = millis();
while(x==0)
{
x=digitalRead(ColourPin);
}
if ((millis()-currentMillis)>800)
{
SysON++;
if (SysON == 3)
{
SysON = 1;
numb1 = 1;
numb2 = 1;
}
}
}
}
else{
buttonState = 0;
}
/*
currentMillis = millis();
int
if(debouncer.update()){
if(debouncer.read() == 0){
if(buttonState == 0){
buttonState = 1;
buttonPressedTime = currentMillis;
}
}
else{
buttonState = 0;
}
}
if(buttonState == 1)
{
if(currentMillis - buttonPressedTime >= 800)
{
buttonState = 0;
SysON++;
}
else if(currentMillis - buttonPressedTime >= 100)
{
}
}
if (SysON == 3)
{
SysON = 1;
numb1 = 1;
numb2 = 1;
}
*/
}