/* Attempting to combine PIR, Rotary Encoder & NeoPixels
*
* Design: Allow silent switching of bedside lamp, with PIR sensor;
* Control switching with rotary encoder push button;
* Brightness control with rotary;
* Light temperature control with double-push and rotary;
* Revert to brightness control with another double-push or elapsed time;
*
* Uses MultiButton to detect double-click
* Github page: https://github.com/poelstra/arduino-multi-button
*
* Uses FastLED for colour and brightness - works with WS2812, APA102 and others
* Has [Master Brightness] and [Full HSV Colour]
* Documentation: http://fastled.io/ |*| Examples: https://github.com/FastLED/FastLED
*
* Rotart Encoder section from ArduinoGetStarted.com
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-rotary-encoder
*/
#include <PinButton.h> //----> Reached here so far...
#include <FastLED.h>
//-- Rotary variables
#define CLK_PIN 10
#define DT_PIN 11
#define SW_PIN 12
#define DIRECTION_CW 0 // clockwise direction
#define DIRECTION_CCW 1 // counter-clockwise direction
int counter = 0;
int direction = DIRECTION_CW;
int CLK_state;
int prev_CLK_state;
//-- Button variables
PinButton button(SW_PIN);
//-- LED variables
#define NUM_LEDS 5
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define BRIGHTNESS 75 //Initial brightness ???
CRGB leds[NUM_LEDS]
CRGBPalette16 thePalette; //Declararion needed
TBlendType theBlending; //For all variables
void setup() {
Serial.begin(9600);
// configure encoder pins as inputs
pinMode(CLK_PIN, INPUT);
pinMode(DT_PIN, INPUT);
// read the initial state of the rotary encoder's CLK pin
prev_CLK_state = digitalRead(CLK_PIN);
//-- LED setup
FastLED.addLeds(<NEOPIXEL, 6>(leds, NUM_LEDS));
FastLED.setBrightness(BRIGHTNESS); //Startup brightness
thePalette = CloudColors_p; //Hoping this is whites ???
theBlend = NOBLEND; //Not sure this is needed ???
}
void loop() {
// MUST call the button function first
// ---> button.loop();
button.update();
//theBrightness = BRIGHTNESS; //Is this necessary? just use BRIGHTNESS ???
// read current state of the rotary encoder's CLK pin
CLK_state = digitalRead(CLK_PIN);
// If the state of CLK is changed, then pulse occurred
// React to only the rising edge (from LOW to HIGH) to avoid double count
if (CLK_state != prev_CLK_state && CLK_state == HIGH) {
// if the DT state is HIGH
// the encoder is rotating in counter-clockwise direction => decrease the counter
if (digitalRead(DT_PIN) == HIGH) {
counter--;
direction = DIRECTION_CCW;
} else {
// the encoder is rotating in clockwise direction => increase the counter
counter++;
direction = DIRECTION_CW;
}
Serial.print("DIRECTION: ");
if (direction == DIRECTION_CW)
Serial.print("Clockwise");
else
Serial.print("Counter-clockwise");
Serial.print(" | COUNTER: ");
Serial.println(counter);
}
// save last CLK state
prev_CLK_state = CLK_state;
if (button.isSingleClick()) {
Serial.println("The button is pressed - once");
}
if (button.isDoubleClick()) {
Serial.println("The button is pressed - twice");
}
if (button.isLongClick()) {
Serial.println("The button is held for a long click");
}
uint8_t colIndex = 1;
uint8_t theBrightness = 100; //This will be adjusted by the rotary
for (int c = 0; c == NUM_LEDS; c++) {
leds[c] = ColorFromPalette(thePalette, colIndex, theBrightness, theBlend);
}
}
/* Possible method to put all on at once?
for (int b = 0; b <= 17; b++)
{
//leds[t] = CRGB(rood, groen, blauw);
leds[b] = CRGB(255, 0, 0); FastLED.show(); delay(k);
leds[b] = CRGB(0, 0, 0); FastLED.show(); delay(k);
}
*/