#include #include #include // Define pins for rotary encoder and button #define pinCLK 32 #define pinDT 35 #define pinSW 34 #define pinLED 23 #define pinRocker1 25 // Rocker switch 1 connected to GPIO 25 #define pinRocker2 26 // Rocker switch 2 connected to GPIO 26 #define pinRocker3 27 // Rocker switch 3 connected to GPIO 27 #define pinRocker4 14 // Rocker switch 4 connected to GPIO 14 // Network credentials const char* ssid = "your_ssid"; const char* password = "you_password"; // Create a web server on port 80 WebServer server(80); // Variables to track the count and last action int rotaryCount = 1; // Variable to keep track of the rotary encoder count int previousStateSW; // Variable to store the previous state of the SW pin int previousStateCLK; // Variable to store the previous state of the CLK pin int previousRockerState1; // Variable to store the previous state of Rocker switch 1 int previousRockerState2; // Variable to store the previous state of Rocker switch 2 int previousRockerState3; // Variable to store the previous state of Rocker switch 3 int previousRockerState4; // Variable to store the previous state of Rocker switch 4 int activatedRockerCount = 1; // Counter for the total number of activated rocker switches int actualStateSW_global; // Global variable to store the state of SW pin // HTML content const char index_html[] PROGMEM = R"rawliteral( Mindset Selector
Rotary Count:
Activated Rocker Switches:

Mood

Image

Interaction acceptance

Image

Mindset

)rawliteral"; // Function to initialize the SPIFFS void initSPIFFS() { if (!SPIFFS.begin(true)) { Serial.println("An error occurred while mounting SPIFFS"); return; } Serial.println("SPIFFS mounted successfully"); } void setup() { Serial.begin(115200); // Initialize serial communication at 115200 baud rate pinMode(pinSW, INPUT_PULLUP); // Set SW pin as input with internal pull-up resistor pinMode(pinDT, INPUT); // Set DT pin as input pinMode(pinCLK, INPUT); // Set CLK pin as input pinMode(pinLED, OUTPUT); // Set LED pin as output pinMode(pinRocker1, INPUT_PULLUP); // Set Rocker switch 1 as input with internal pull-up resistor pinMode(pinRocker2, INPUT_PULLUP); // Set Rocker switch 2 as input with internal pull-up resistor pinMode(pinRocker3, INPUT_PULLUP); // Set Rocker switch 3 as input with internal pull-up resistor pinMode(pinRocker4, INPUT_PULLUP); // Set Rocker switch 4 as input with internal pull-up resistor previousStateSW = digitalRead(pinSW); // Read initial state of the SW pin previousStateCLK = digitalRead(pinCLK); // Read initial state of the CLK pin previousRockerState1 = digitalRead(pinRocker1); // Read initial state of Rocker switch 1 previousRockerState2 = digitalRead(pinRocker2); // Read initial state of Rocker switch 2 previousRockerState3 = digitalRead(pinRocker3); // Read initial state of Rocker switch 3 previousRockerState4 = digitalRead(pinRocker4); // Read initial state of Rocker switch 4 analogWrite(pinLED, 0); // Initialize the LED with 0 brightness (off) // Initialize SPIFFS initSPIFFS(); // 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"); // Print the IP address Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Define server routes server.on("/", []() { server.send_P(200, "text/html", index_html); }); server.on("/data", []() { actualStateSW_global = digitalRead(pinSW); // Read the current state of SW pin bool triggerAddValues = (actualStateSW_global == LOW); // Set triggerAddValues based on actualStateSW_global String json = "{\"rotaryCount\":" + String(rotaryCount) + ",\"activatedRockerCount\":" + String(activatedRockerCount) + ",\"triggerAddValues\":" + (actualStateSW_global == LOW ? "true" : "false") + "}"; server.send(200, "application/json", json); }); server.serveStatic("/", SPIFFS, "/"); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); int actualStateCLK = digitalRead(pinCLK); // Read current state of the CLK pin int actualStateDT = digitalRead(pinDT); // Read current state of the DT pin int actualStateSW = digitalRead(pinSW); // Read current state of the SW pin // Read current states of rocker switches again after debounce delay int currentRockerState1 = digitalRead(pinRocker1); int currentRockerState2 = digitalRead(pinRocker2); int currentRockerState3 = digitalRead(pinRocker3); int currentRockerState4 = digitalRead(pinRocker4); // Check if the state has changed and is stable if (currentRockerState1 != previousRockerState1 || currentRockerState2 != previousRockerState2 || currentRockerState3 != previousRockerState3 || currentRockerState4 != previousRockerState4) { // Debounce delay for rocker switches delay(50); // Update activatedRockerCount only if there is a change in any rocker switch state activatedRockerCount = 1; if (currentRockerState1 == LOW) { activatedRockerCount++; } if (currentRockerState2 == LOW) { activatedRockerCount++; } if (currentRockerState3 == LOW) { activatedRockerCount++; } if (currentRockerState4 == LOW) { activatedRockerCount++; } // Print the number of activated rocker switches to the Serial Monitor Serial.print(F("Activated rocker switches: ")); Serial.println(activatedRockerCount); // Update previous rocker states previousRockerState1 = currentRockerState1; previousRockerState2 = currentRockerState2; previousRockerState3 = currentRockerState3; previousRockerState4 = currentRockerState4; } // Push Button verification if (actualStateSW != previousStateSW) { // Check if SW state has changed previousStateSW = actualStateSW; // Update the previous SW state delay(50); // Debounce delay if (actualStateSW == LOW) { Serial.println(F("SW Button pushed")); // Print message when button is pressed } else { Serial.println(F("SW Button released")); // Print message when button is released } } // Rotary encoder verification if (rotaryCount < 1) { rotaryCount = 1; } else if (rotaryCount > 5) { rotaryCount = 5; } if (actualStateCLK != previousStateCLK) { delayMicroseconds(200); // Debounce delay if (digitalRead(pinCLK) == actualStateCLK) { // Check if the CLK signal has settled previousStateCLK = actualStateCLK; if (actualStateCLK == LOW) { if (actualStateCLK != actualStateDT) { // Comparing CLK and DT, if they're different, direction is counter-clockwise rotaryCount--; // Decrement counter Serial.println(F("counter-clockwise")); // Display value on Serial Monitor } else { // When CLK and DT are similar, direction is clockwise rotaryCount++; // Increment counter Serial.println(F("clockwise")); // Display value on Serial Monitor } Serial.println(rotaryCount); // Ensure count stays within bounds (0-5 for PWM) if (rotaryCount < 1) { rotaryCount = 1; } else if (rotaryCount > 5) { rotaryCount = 5; } // Map rotaryCount to PWM range (0-128) int pwmValue = map(rotaryCount, 1, 5, 0, 128); analogWrite(pinLED, pwmValue); // Set LED brightness } } } }