#include <FastLED.h>
#define LED_PIN 6
#define NUM_LEDS 39
#define LED_DELAY 40
#define BUTTON_PIN 2
#define PAUSE_PIN 3
#define BUTTON_PIN_SPEED 7
#define BUTTON_PIN_BRIGHTNESS 4
boolean oldState = HIGH;
boolean oldStateSpeed = HIGH;
boolean oldbrightness = HIGH;
int mode = 0; // Currently-active animation mode, 0-9
int modepause=0;
int R_col = 255;
int G_col = 255;
int B_col = 255;
float wait =20;
float BRIGHTNESS = 255;
float pulsatordwell = 20;
int vibStartTime = 0;
int vibStartMIlls = 0;
int vibOnInterval = 50;
int pausefalse=1;
const int debounceDelay = 10; // milliseconds to wait until button input stable *
int buttonpresscounter = 0;
int pausebuttonpresscounter=0;
CRGB leds[NUM_LEDS];
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(PAUSE_PIN,INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(PAUSE_PIN), pausebuttonPushed, LOW); // assign interrupt to button, procedure to be run and set state to LOW when pushed *
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), buttonPushed, LOW); // assign interrupt to button, procedure to be run and set state to LOW when pushed *
attachInterrupt(digitalPinToInterrupt(PAUSE_PIN), pausebuttonPushed, LOW); // assign interrupt to button, procedure to be run and set state to LOW when pushed *
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
vibStartMIlls = millis();
}
void loop() {
digitalWrite(5, LOW);
digitalWrite(4, LOW);
for (int i = (NUM_LEDS-1); i >= 0; i--) {
float Potvalue = 1024-analogRead(A2);
float BrightPotvalue = analogRead(A0);
BRIGHTNESS = (BrightPotvalue+10)/1024*250;
wait = (((Potvalue)/1024)*100)+5;
//Serial.println(buttonpresscounter);
leds[i] = CRGB ( R_col, G_col, B_col);
if(pausefalse==0)
{
BRIGHTNESS =0;
}
FastLED.setBrightness(BRIGHTNESS);
FastLED.show();
//Serial.println(pausefalse);
if(pausefalse==1) {
if(i> NUM_LEDS-(pulsatordwell-((wait/100)*(pulsatordwell/2)))) {
digitalWrite(5, HIGH);
}
else if (i<= NUM_LEDS-(pulsatordwell-((wait/100)*(pulsatordwell/2)))) {
digitalWrite(5, LOW);
}
}
FastLED.clear();
//˜
//
delay(wait);
//
}
for (int i = 0; i <= (NUM_LEDS-1); i++) {
float Potvalue = 1024-analogRead(A2);
float BrightPotvalue = analogRead(A0);
BRIGHTNESS = (BrightPotvalue+10)/1024*250;
wait = (((Potvalue)/1024)*100)+5;
//Serial.println(pulsatordwell-((wait/100)*(pulsatordwell/2)));
leds[i] = CRGB ( R_col, G_col, B_col);
if(pausefalse==0)
{
BRIGHTNESS =0;
}
FastLED.setBrightness(BRIGHTNESS);
FastLED.show();
//Serial.println(pausefalse);
//if (i=1) { // assumes LOW when pressed
// vibStartTime = millis();
//digitalWrite(3, HIGH);
//}
//if (vibStartTime - vibStartMIlls >= vibOnInterval) {
// digitalWrite(3, LOW);
//vibStartMIlls = vibStartTime;
//}
if(pausefalse==1) {
if(i<= (pulsatordwell-((wait/100)*(pulsatordwell/2)))) {
digitalWrite(4, HIGH);
}
else if (i> (pulsatordwell-((wait/100)*(pulsatordwell/2)))) {
digitalWrite(4, LOW);
}
}
delay(wait);
FastLED.clear();
}
}
void buttonPushed() { // detect and debounce button push using assigned interrupt *
if (debounce(BUTTON_PIN)) { // calls debounce procedure *
//Serial.println("Debounce Colour Button Pushed"); // shows button pushed on serial monitor
//digitalWrite(LED, HIGH); // Turn the button-LED on for feedback
if(++mode > 6) mode = 0; // Advance to next mode, wrap around after #8
switch(mode) { // Start the new animation...
case 0: //white
R_col = 255;
G_col = 255;
B_col = 134;
//wait = 20;
break ;
case 1: //red
R_col = 255;
G_col = 0 ;
B_col = 0;
//wait = 20;
break;
case 2: //green
R_col = 0;
G_col = 255;
B_col = 0;
//wait = 20;
break;
case 3: //blue
R_col = 0;
G_col = 0;
B_col = 255;
//wait = 20;
break;
case 4: //pink
R_col = 255;
G_col = 0;
B_col = 127;
//wait = 20;
break;
case 5: //yellow
R_col = 255;
G_col = 128;
B_col = 0;
//wait = 20;
break;
case 6: //purple
R_col = 183;
G_col = 0;
B_col = 204;
//wait = 20;
break;
}
buttonpresscounter = buttonpresscounter +1 ;
delay(1000); // delay to read serial monitor and see LED
}
}
void pausebuttonPushed() { // detect and debounce button push using assigned interrupt *
//Serial.println("Debounce Pause Pushed but if not reached");
if (debounce(PAUSE_PIN)) { // calls debounce procedure *
Serial.println("Debounce Pause Pushed"); // shows button pushed on serial monitor
//digitalWrite(LED, HIGH); // Turn the button-LED on for feedback
//Serial.println(modepause);
if(++modepause > 1) modepause = 0; // Advance to next mode, wrap around after #8
switch(modepause) { // Start the new animation...
case 0:
pausefalse = 1;
break ;
case 1:
pausefalse = 0;
//wait = 20;
break;
}
pausebuttonpresscounter = pausebuttonpresscounter +1 ;
delay(1000); // delay to read serial monitor and see LED
Serial.println(pausefalse); // shows button pushed on serial monitor
}
}
boolean debounce(int pin)
{
boolean state;
boolean previousState;
previousState = digitalRead(pin); // store switch state
for (int counter = 0; counter < debounceDelay; counter++)
{
delay(1); // wait for 1 millisecond
state = digitalRead(pin); // read the pin
if ( state != previousState)
{
counter = 0; // reset the counter if the state changes
previousState = state; // and save the current state
}
}
// here when the switch state has been stable longer than the debounce period
return state;
}