#include #include // Define pins for rotary encoder, button, LED, and rocker switches #define pinCLK 32 #define pinDT 35 #define pinSW 34 #define pinLED 23 #define pinRocker1 25 #define pinRocker2 26 #define pinRocker3 27 #define pinRocker4 14 // Network credentials const char* ssid = "your_ssid"; const char* password = "your_password"; // Create a web server on port 80 WebServer server(80); // Variables to track the count, states, and direction int rotaryCount = 0; int previousStateSW; int previousStateCLK; bool isClockwise = false; int previousRockerState1; int previousRockerState2; int previousRockerState3; int previousRockerState4; int activatedRockerCount = 0; // HTML content with JavaScript for dynamic updates const char index_html[] PROGMEM = R"rawliteral( Mindset Selector
Rotary Count:
Activated Rocker Switches:
Direction:
)rawliteral"; void setup() { Serial.begin(115200); pinMode(pinSW, INPUT_PULLUP); pinMode(pinDT, INPUT); pinMode(pinCLK, INPUT); pinMode(pinLED, OUTPUT); pinMode(pinRocker1, INPUT_PULLUP); pinMode(pinRocker2, INPUT_PULLUP); pinMode(pinRocker3, INPUT_PULLUP); pinMode(pinRocker4, INPUT_PULLUP); previousStateSW = digitalRead(pinSW); previousStateCLK = digitalRead(pinCLK); previousRockerState1 = digitalRead(pinRocker1); previousRockerState2 = digitalRead(pinRocker2); previousRockerState3 = digitalRead(pinRocker3); previousRockerState4 = digitalRead(pinRocker4); analogWrite(pinLED, 0); // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Define server routes server.on("/", []() { server.send_P(200, "text/html", index_html); }); server.on("/data", []() { String direction = isClockwise ? "Clockwise" : "Counterclockwise"; String json = "{\"rotaryCount\":" + String(rotaryCount) + ",\"activatedRockerCount\":" + String(activatedRockerCount) + ",\"direction\":\"" + direction + "\"}"; server.send(200, "application/json", json); }); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); int actualStateCLK = digitalRead(pinCLK); int actualStateDT = digitalRead(pinDT); int actualStateSW = digitalRead(pinSW); int currentRockerState1 = digitalRead(pinRocker1); int currentRockerState2 = digitalRead(pinRocker2); int currentRockerState3 = digitalRead(pinRocker3); int currentRockerState4 = digitalRead(pinRocker4); // Update direction based on the rotary encoder movement if (actualStateCLK != previousStateCLK) { delayMicroseconds(200); // Debounce delay if (digitalRead(pinCLK) == actualStateCLK) { previousStateCLK = actualStateCLK; if (actualStateCLK == LOW) { isClockwise = (actualStateCLK != actualStateDT); rotaryCount += isClockwise ? 1 : -1; rotaryCount = constrain(rotaryCount, 1, 5); Serial.println(rotaryCount); int pwmValue = map(rotaryCount, 1, 5, 0, 128); analogWrite(pinLED, pwmValue); } } } // Update activated rocker switch count if (currentRockerState1 != previousRockerState1 || currentRockerState2 != previousRockerState2 || currentRockerState3 != previousRockerState3 || currentRockerState4 != previousRockerState4) { delay(50); activatedRockerCount = (currentRockerState1 == LOW) + (currentRockerState2 == LOW) + (currentRockerState3 == LOW) + (currentRockerState4 == LOW); Serial.print("Activated rocker switches: "); Serial.println(activatedRockerCount); previousRockerState1 = currentRockerState1; previousRockerState2 = currentRockerState2; previousRockerState3 = currentRockerState3; previousRockerState4 = currentRockerState4; } // Push button verification if (actualStateSW != previousStateSW) { delay(50); // Debounce delay previousStateSW = actualStateSW; Serial.println(actualStateSW == LOW ? "SW Button pushed" : "SW Button released"); } }