two rotary encoders for nightlight
This commit is contained in:
@@ -12,11 +12,20 @@ Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KH
|
|||||||
#define DT 9
|
#define DT 9
|
||||||
#define SW 10
|
#define SW 10
|
||||||
|
|
||||||
|
#define CLK2 5
|
||||||
|
#define DT2 4
|
||||||
|
#define SW2 3
|
||||||
|
|
||||||
int currentStateCLK;
|
int currentStateCLK;
|
||||||
|
int currentStateCLK2;
|
||||||
int lastStateCLK;
|
int lastStateCLK;
|
||||||
|
int lastStateCLK2;
|
||||||
String currentDir = "";
|
String currentDir = "";
|
||||||
|
int btnState;
|
||||||
unsigned long lastButtonPress = 0;
|
unsigned long lastButtonPress = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define HUE_STEP 1200
|
#define HUE_STEP 1200
|
||||||
#define VAL_STEP 10
|
#define VAL_STEP 10
|
||||||
|
|
||||||
@@ -27,8 +36,6 @@ int value = 60; // 0-255
|
|||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
|
||||||
|
|
||||||
pinMode(CLK,INPUT);
|
pinMode(CLK,INPUT);
|
||||||
pinMode(DT,INPUT);
|
pinMode(DT,INPUT);
|
||||||
pinMode(SW, INPUT_PULLUP);
|
pinMode(SW, INPUT_PULLUP);
|
||||||
@@ -39,19 +46,17 @@ void setup() {
|
|||||||
pixels.begin(); // initialize NeoPixel library.
|
pixels.begin(); // initialize NeoPixel library.
|
||||||
pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS);
|
pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS);
|
||||||
pixels.show();
|
pixels.show();
|
||||||
|
|
||||||
Serial.println("PIXELS");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
currentStateCLK = digitalRead(CLK);
|
currentStateCLK = digitalRead(CLK);
|
||||||
int btnState = digitalRead(SW);
|
currentStateCLK2 = digitalRead(CLK2);
|
||||||
|
|
||||||
// If last and current state of CLK are different, then pulse occurred
|
// If last and current state of CLK are different, then pulse occurred
|
||||||
if (currentStateCLK != lastStateCLK && currentStateCLK == 1){
|
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
|
||||||
|
btnState = digitalRead(SW);
|
||||||
|
|
||||||
if (digitalRead(DT) != currentStateCLK) {
|
if (digitalRead(DT) != currentStateCLK) {
|
||||||
currentDir ="CW";
|
|
||||||
if (btnState == LOW) {
|
if (btnState == LOW) {
|
||||||
hue += HUE_STEP;
|
hue += HUE_STEP;
|
||||||
if (hue > 65535) {
|
if (hue > 65535) {
|
||||||
@@ -69,7 +74,6 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
currentDir ="CCW";
|
|
||||||
if (btnState == LOW) {
|
if (btnState == LOW) {
|
||||||
hue -= HUE_STEP;
|
hue -= HUE_STEP;
|
||||||
if (hue < 0) {
|
if (hue < 0) {
|
||||||
@@ -88,30 +92,54 @@ void loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS);
|
||||||
|
pixels.show();
|
||||||
|
}
|
||||||
|
else if (currentStateCLK2 != lastStateCLK2 && currentStateCLK2 == 1) {
|
||||||
|
btnState = digitalRead(SW2);
|
||||||
|
|
||||||
Serial.print("Direction: ");
|
if (digitalRead(DT2) != currentStateCLK2) {
|
||||||
Serial.print(currentDir);
|
if (btnState == LOW) {
|
||||||
Serial.print(" | Hue: ");
|
hue += HUE_STEP;
|
||||||
Serial.print(hue);
|
if (hue > 65535) {
|
||||||
Serial.print(" | Sat: ");
|
hue = 65535;
|
||||||
Serial.print(saturation);
|
saturation = 0;
|
||||||
Serial.print(" | Val: ");
|
}
|
||||||
Serial.println(value);
|
else {
|
||||||
|
saturation = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
value += VAL_STEP;
|
||||||
|
if (value > 255) {
|
||||||
|
value = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (btnState == LOW) {
|
||||||
|
hue -= HUE_STEP;
|
||||||
|
if (hue < 0) {
|
||||||
|
hue = 0;
|
||||||
|
saturation = 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
saturation = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
value -= VAL_STEP;
|
||||||
|
if (value < 30) {
|
||||||
|
value = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS);
|
pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS);
|
||||||
pixels.show();
|
pixels.show();
|
||||||
}
|
}
|
||||||
/* else if (btnState == LOW) {
|
|
||||||
//if 50ms have passed since last LOW pulse, it means that the
|
|
||||||
//button has been pressed, released and pressed again
|
|
||||||
if (millis() - lastButtonPress > 50) {
|
|
||||||
Serial.println("Button pressed!");
|
|
||||||
}
|
|
||||||
lastButtonPress = millis();
|
|
||||||
}*/
|
|
||||||
|
|
||||||
lastStateCLK = currentStateCLK;
|
lastStateCLK = currentStateCLK;
|
||||||
|
lastStateCLK2 = currentStateCLK2;
|
||||||
|
|
||||||
// Put in a slight delay to help debounce the reading
|
// Put in a slight delay to help debounce the reading
|
||||||
delay(1);
|
delay(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user