#define DEBUG
#include <FastLED.h>
#include "Potentiometer.h"
// LEDs
#define NUM_LEDS 120
#define LED_PIN 11
CRGB leds [NUM_LEDS];
uint8_t brightness = 50;
uint8_t hue = 100;
uint8_t saturation = 255;
uint8_t value = 255;
uint8_t red;
uint8_t green;
uint8_t blue;
//keyLED states
uint8_t colourMode = 0;
uint8_t keyLedMode = 0;
int debounceDelay = 250; // button debounce delay
unsigned long lastColourControlCheck = 0;
int colourModeButtonState;
int colourModeButtonPState = HIGH;
unsigned long lastKeyLedModeCheck = 0;
int keyLedModeButtonState;
int keyLedModeButtonPState = HIGH;
//POTENTIOMETERS
#define POT_NUM 4 // number of potentiometers
#define POT_THRESHOLD 5 // sets the sensibility of the potentiometers, increase value if potentiometer is jittering
#define ALPHA 0.75 // set the Alpha veriable for the for leaky integrator to smooth output, between 0.01 and 1.00
Potentiometer Pot[POT_NUM] = {(A1), (A2), (A3), (A4)}; // creates a number (POT_NUM) of objects of Potentiometer class and passes the pin number as argument
int potReading [POT_NUM]; // variable for the potentiometer reading values returned
//BUTTONS
#define COLOUR_BUTTON_PIN 10
#define MODE_BUTTON_PIN 5
//**********************************************************************************************************************************************
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
// mode buttons setup
pinMode(COLOUR_BUTTON_PIN, INPUT_PULLUP); // colour control button initialization
pinMode(MODE_BUTTON_PIN, INPUT_PULLUP); // keyLED mode button initialization
//Potentiometers
for (int i = 0 ; i < POT_NUM; i ++) { // pots setup
Pot[i].setThreshold(POT_THRESHOLD); // sets threshold
Pot[i].setAlphaValue(ALPHA); // sets alpha value
}
//LEDs
FastLED.addLeds<WS2812B, LED_PIN, GRB> (leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
delay(1000);
FastLED.setBrightness(50);
for (int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGB( 50, 100, 150);
}
FastLED.show();
}
//**********************************************************************************************************************************************
void loop() {
colourStateCheck();
modeStateCheck();
potCheck();
if (colourMode == 0) {
StaticHue();
}
if (colourMode == 1) {
ChangingHue ();
}
if (colourMode == 2) {
MusicMode();
}
}
void colourStateCheck() {
unsigned long currentMillis = millis();
colourModeButtonState = digitalRead(COLOUR_BUTTON_PIN);
if ((currentMillis - lastColourControlCheck) > debounceDelay) { // DEBOUNCE FUNCTION FOR colour control mode
if (colourModeButtonState != colourModeButtonPState) { // checks that button state is different from its previous state
lastColourControlCheck = currentMillis; // updates debounce time
if (colourModeButtonState == LOW) {
colourMode++; // if button is pressed
if (colourMode == 2) {
colourMode = 0;
}
}
#ifdef DEBUG
Serial.print("colour mode selected: ");
Serial.println(colourMode); // prints selected mode
#endif
}
colourModeButtonPState = colourModeButtonState; // updates mode state of th encoder
}
}
void modeStateCheck() {
unsigned long currentMillis = millis();
keyLedModeButtonState = digitalRead(MODE_BUTTON_PIN);
if ((currentMillis - lastKeyLedModeCheck) > debounceDelay) { // DEBOUNCE FUNCTION FOR colour control mode
if (keyLedModeButtonState != keyLedModeButtonPState) { // checks that button state is different from its previous state
lastKeyLedModeCheck = currentMillis; // updates debounce time
if (keyLedModeButtonState == LOW) {
keyLedMode++; // if button is pressed
if (keyLedMode == 3) {
keyLedMode = 0;
}
}
#ifdef DEBUG
Serial.print("mode selected: ");
Serial.println(keyLedMode); // prints selected mode
#endif
}
keyLedModeButtonPState = keyLedModeButtonState; // updates mode state of th encoder
}
}
void potCheck(){
for (int i = 0 ; i < POT_NUM; i ++) { // read the pot values
potReading [i] = Pot[i].readValue();
brightness = potReading [0];
if (colourMode == 0) {
hue = potReading [1];
saturation = potReading [2];
value = potReading [3];
}
else if (colourMode == 1) {
red = potReading [1];
green = potReading [2];
blue = potReading [3];
}
}
#ifdef DEBUG
// Serial.print("Slider: "); // prints current potentiometers reading
// Serial.print(potReading [0]);
// Serial.print(" Pot n1: "); // prints current potentiometers reading
// Serial.print(potReading [1]);
// Serial.print(" Pot n3: "); // prints current potentiometers reading
// Serial.print(potReading [2]);
// Serial.print(" Pot n3: "); // prints current potentiometers reading
// Serial.println(potReading [3]);
// Serial.print("brightness: "); // prints current potentiometers reading
// Serial.print(brightness);
// Serial.print(" Hue: "); // prints current potentiometers reading
// Serial.print(hue);
// Serial.print(" Sat: "); // prints current potentiometers reading
// Serial.print(saturation);
// Serial.print(" Val: "); // prints current potentiometers reading
// Serial.print(value);
// Serial.print(" Red: "); // prints current potentiometers reading
// Serial.print(red);
// Serial.print(" Green: "); // prints current potentiometers reading
// Serial.print(green);
// Serial.print(" Blue: "); // prints current potentiometers reading
// Serial.println(blue);
#endif
}
void ChangingHue(){
for(int i = 0;i < NUM_LEDS; i++){
leds[i] = CHSV(hue, saturation, value); // also: leds[i] = CHSV(hue + (i * 5), 255,255);
}
EVERY_N_MILLISECONDS (15){
hue++;
}
FastLED.show();
}
void StaticHue(){
for(int i = 0;i < NUM_LEDS; i++){
leds[i] = CHSV(hue, saturation, value); // also: leds[i] = CHSV(hue + (i * 5), 255,255);
}
FastLED.show();
}
void MusicMode(){
}