#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#define BUTTON1_PIN 12
#define BUTTON2_PIN 14
#define BUTTON3_PIN 27
#define BUTTON4_PIN 26
#define NEOPIXEL_PIN 5
#define NEOPIXEL_COUNT 4
Adafruit_NeoPixel neopixels(NEOPIXEL_COUNT, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
#define PUMP1_PIN 25
#define PUMP2_PIN 33
#define PUMP3_PIN 32
#define PUMP4_PIN 35
#define LONG_PRESS_DURATION 2000 // 2 seconds
#define DEBOUNCE_DELAY 50
bool pumpState[] = {false, true, false, false};
bool lastButtonState[] = {false, false, false, false};
unsigned long lastPressTime[] = {0, 0, 0, 0};
bool buttonPressed = false;
bool longPress = false;
void setup() {
Serial.begin(115200);
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
pinMode(BUTTON3_PIN, INPUT_PULLUP);
pinMode(BUTTON4_PIN, INPUT_PULLUP);
pinMode(PUMP1_PIN, OUTPUT);
pinMode(PUMP2_PIN, OUTPUT);
pinMode(PUMP3_PIN, OUTPUT);
pinMode(PUMP4_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(BUTTON1_PIN), checkButtons, CHANGE);
attachInterrupt(digitalPinToInterrupt(BUTTON2_PIN), checkButtons, CHANGE);
attachInterrupt(digitalPinToInterrupt(BUTTON3_PIN), checkButtons, CHANGE);
attachInterrupt(digitalPinToInterrupt(BUTTON4_PIN), checkButtons, CHANGE);
neopixels.begin();
neopixels.show();
for (int i = 0; i < NEOPIXEL_COUNT; i++) {
neopixels.setPixelColor(i, 0, 0, 255);
}
neopixels.show();
delay(1000);
for (int i = 0; i < NEOPIXEL_COUNT; i++) {
neopixels.setPixelColor(i, 0);
}
neopixels.show();
}
void loop() {
if (buttonPressed) {
Serial.print("X");
for (int i = 0; i < NEOPIXEL_COUNT; i++) {
if (longPress) {
Serial.print("LAng");
// This part is left for you to implement based on your desired behavior
} else {
Serial.print("kurz ");// Blink the neopixel corresponding to the pressed button
// This part is left for you to implement based on your desired behavior
}
neopixels.setPixelColor(i, 255, 0, 100);
neopixels.show();// Blink all neopixels red or green according to pumpState
delay(100);
}
buttonPressed = false;
longPress = false;
}
else
{
for (int i = 0; i < NEOPIXEL_COUNT; i++) {
neopixels.setPixelColor(i, 0, 0, 0); // off
neopixels.show();// Blink all neopixels red or green according to pumpState
}
}
}
void checkButtons() {
for (int i = 0; i < 4; i++) {
int buttonPin;
switch (i) {
case 0:
buttonPin = BUTTON2_PIN;
break;
case 1:
buttonPin = BUTTON1_PIN;
break;
case 2:
buttonPin = BUTTON3_PIN;
break;
case 3:
buttonPin = BUTTON4_PIN;
break;
default:
break;
}
bool buttonState = digitalRead(buttonPin);
if (millis() - lastPressTime[i] > DEBOUNCE_DELAY) {
if (buttonState != lastButtonState[i]) {
if (buttonState == LOW) { // Button pressed
buttonPressed = true;
if (millis() - lastPressTime[i] > LONG_PRESS_DURATION) {
longPress = true;
Serial.print("L");
}
}
}
}
lastButtonState[i] = buttonState;
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4