From 46ca7f92e9f453b418f4355886f50144991ef0e3 Mon Sep 17 00:00:00 2001 From: Tilman Date: Fri, 27 Jan 2023 09:43:10 +0100 Subject: [PATCH] esp rotary encoder --- .../nano_ky-040_rotary/nano_ky-040_rotary.ino | 118 ++++++++++++++++++ .../nano_simple_rotary/nano_simple_rotary.ino | 71 +++++++++++ 2 files changed, 189 insertions(+) create mode 100644 nano/nano_ky-040_rotary/nano_ky-040_rotary.ino create mode 100644 nano/nano_simple_rotary/nano_simple_rotary.ino diff --git a/nano/nano_ky-040_rotary/nano_ky-040_rotary.ino b/nano/nano_ky-040_rotary/nano_ky-040_rotary.ino new file mode 100644 index 0000000..08e29ac --- /dev/null +++ b/nano/nano_ky-040_rotary/nano_ky-040_rotary.ino @@ -0,0 +1,118 @@ +// https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/ + +#include + +#define NUMPIXELS 12 +#define LEDPIN 2 + +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800); + +// Rotary Encoder Inputs +#define CLK 8 +#define DT 9 +#define SW 10 + +int currentStateCLK; +int lastStateCLK; +String currentDir = ""; +unsigned long lastButtonPress = 0; + +#define HUE_STEP 1200 +#define VAL_STEP 10 + +long hue = 0; // 0-65535 https://adafruit.github.io/Adafruit_NeoPixel/html/class_adafruit___neo_pixel.html#a1f16aee5b96e16e62598f826e292b23b +int saturation = 0; // 0-255 +int value = 60; // 0-255 + + + +void setup() { + Serial.begin(115200); + + pinMode(CLK,INPUT); + pinMode(DT,INPUT); + pinMode(SW, INPUT_PULLUP); + + // Read the initial state of CLK + lastStateCLK = digitalRead(CLK); + + pixels.begin(); // initialize NeoPixel library. + pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS); + pixels.show(); + + Serial.println("PIXELS"); +} + +void loop() { + currentStateCLK = digitalRead(CLK); + int btnState = digitalRead(SW); + + // If last and current state of CLK are different, then pulse occurred + if (currentStateCLK != lastStateCLK && currentStateCLK == 1){ + + if (digitalRead(DT) != currentStateCLK) { + currentDir ="CW"; + if (btnState == LOW) { + hue += HUE_STEP; + if (hue > 65535) { + hue = 65535; + saturation = 0; + } + else { + saturation = 255; + } + } + else { + value += VAL_STEP; + if (value > 255) { + value = 255; + } + } + } else { + currentDir ="CCW"; + 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; + } + } + } + + + Serial.print("Direction: "); + Serial.print(currentDir); + Serial.print(" | Hue: "); + Serial.print(hue); + Serial.print(" | Sat: "); + Serial.print(saturation); + Serial.print(" | Val: "); + Serial.println(value); + + + pixels.fill(pixels.gamma32(pixels.ColorHSV(hue, saturation, value)), 0, NUMPIXELS); + 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; + + // Put in a slight delay to help debounce the reading + delay(1); +} diff --git a/nano/nano_simple_rotary/nano_simple_rotary.ino b/nano/nano_simple_rotary/nano_simple_rotary.ino new file mode 100644 index 0000000..77984bc --- /dev/null +++ b/nano/nano_simple_rotary/nano_simple_rotary.ino @@ -0,0 +1,71 @@ +// https://starthardware.org/arduino-und-encoder-schaltplan-erklaerung-code/ + +#include + +#define NUMPIXELS 12 +#define LEDPIN 2 + +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, LEDPIN, NEO_GRB + NEO_KHZ800); + +int encoderPinA = 4; +int encoderPinB = 5; +int encoderPos = 0; +int encoderPinALast = LOW; +int encoderPinANow = LOW; + +long colorNew = 65535; +long color = 65535; + + +void setup() { + pinMode (encoderPinA, INPUT_PULLUP); + pinMode (encoderPinB, INPUT_PULLUP); + Serial.begin (115200); + + delay(150); + Serial.println("PIXELS"); + + pixels.begin(); // initialize NeoPixel library. + + //pixels.setBrightness(40); + //pixels.fill(pixels.Color(217, 101, 0), 0, NUMPIXELS); + + pixels.fill(pixels.gamma32(pixels.ColorHSV(65535, 255, 190)), 0, NUMPIXELS); + pixels.show(); + + /* + for(long firstPixelHue = 0; firstPixelHue < 5*65536; firstPixelHue += 256) { + pixels.rainbow(firstPixelHue); + pixels.show(); + delay(10); + } + */ +} + +void loop() { + encoderPinANow = digitalRead(encoderPinA); + if ((encoderPinALast == HIGH) && (encoderPinANow == LOW)) { + if (digitalRead(encoderPinB) == HIGH) { + encoderPos++; + colorNew += 4096; + } else { + encoderPos--; + colorNew -= 4096; + } + + if (colorNew > 65535) { + colorNew = 0; + } + else if (colorNew < 0) { + colorNew = 65535; + } + } + encoderPinALast = encoderPinANow; + + if (colorNew != color) { + color = colorNew; + pixels.fill(pixels.gamma32(pixels.ColorHSV(color, 255, 100)), 0, NUMPIXELS); + pixels.show(); + Serial.println(color); + } +}