/************************************************************ LEAN STATION - Cycle Time Monitoring System Board: Seeed Studio XIAO ESP32-C3 Sensor: HC-SR04 Ultrasonic Sensor Platform: Blynk IoT Description: This program detects when an object passes through the sensing area of a conveyor belt, measures the cycle time, counts the number of cycles, calculates the average time, and sends the information to a Blynk dashboard. Author: Rodrigo Guamán ************************************************************/ /************************************************************ 1. Blynk and WiFi Configuration ************************************************************/ #define BLYNK_TEMPLATE_ID "TMPL2_cwfk2-a" #define BLYNK_TEMPLATE_NAME "LEAN STATION" #define BLYNK_AUTH_TOKEN "PASTE_YOUR_BLYNK_AUTH_TOKEN_HERE" #include #include #include // Replace these values with your own WiFi credentials char ssid[] = "YOUR_WIFI_SSID"; char pass[] = "YOUR_WIFI_PASSWORD"; /************************************************************ 2. Sensor Pins and Process Variables ************************************************************/ // HC-SR04 sensor pins #define TRIG_PIN D5 #define ECHO_PIN D6 // Detection threshold in centimeters float detectionThreshold = 15.0; // Distance and detection states float distanceCM = 0; bool objectDetected = false; bool previousState = false; // Cycle time variables unsigned long startTime = 0; float cycleTime = 0; float totalTime = 0; float averageTime = 0; int cycleCount = 0; // Optional variable for dashboard float cyclesPerMinute = 0; // Blynk timer BlynkTimer timer; /************************************************************ 3. Distance Reading Function ************************************************************/ float readDistance() { // Send ultrasonic pulse digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // Read echo time long duration = pulseIn(ECHO_PIN, HIGH, 30000); // If no echo is received, return a large value if (duration == 0) { return 999; } // Convert time into distance in centimeters float distance = duration * 0.0343 / 2.0; return distance; } /************************************************************ 4. Object Detection and Cycle Measurement ************************************************************/ void monitorProcess() { // Read distance from ultrasonic sensor distanceCM = readDistance(); // Send distance value to Blynk Blynk.virtualWrite(V5, distanceCM); // Object is detected when the distance is below the threshold objectDetected = distanceCM < detectionThreshold; // Detect transition: free area to object present if (objectDetected == true && previousState == false) { startTime = millis(); Blynk.virtualWrite(V3, "Object detected"); Serial.println("Object detected"); } // Detect transition: object present to free area if (objectDetected == false && previousState == true) { cycleTime = (millis() - startTime) / 1000.0; cycleCount++; totalTime += cycleTime; averageTime = totalTime / cycleCount; // Calculate cycles per minute based on the last cycle if (cycleTime > 0) { cyclesPerMinute = 60.0 / cycleTime; } // Send values to Blynk dashboard Blynk.virtualWrite(V0, cycleCount); Blynk.virtualWrite(V1, cycleTime); Blynk.virtualWrite(V2, averageTime); Blynk.virtualWrite(V3, "Area free"); Blynk.virtualWrite(V4, cyclesPerMinute); // Print values in Serial Monitor Serial.println("-------------------------"); Serial.print("Cycle Count: "); Serial.println(cycleCount); Serial.print("Cycle Time: "); Serial.print(cycleTime); Serial.println(" s"); Serial.print("Average Time: "); Serial.print(averageTime); Serial.println(" s"); Serial.print("Cycles per Minute: "); Serial.println(cyclesPerMinute); Serial.println("-------------------------"); } // Update previous state previousState = objectDetected; } /************************************************************ 5. Reset Button from Blynk ************************************************************/ BLYNK_WRITE(V6) { if (param.asInt() == 1) { cycleCount = 0; cyclesPerMinute = 0; totalTime = 0; averageTime = 0; cycleTime = 0; startTime = 0; // Update dashboard values Blynk.virtualWrite(V0, cycleCount); Blynk.virtualWrite(V1, cycleTime); Blynk.virtualWrite(V2, averageTime); Blynk.virtualWrite(V3, "Data reset"); Blynk.virtualWrite(V4, cyclesPerMinute); Blynk.virtualWrite(V5, distanceCM); Serial.println("Data reset from Blynk button"); } } /************************************************************ 6. Setup Function ************************************************************/ void setup() { Serial.begin(115200); // Configure ultrasonic sensor pins pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); // Start Blynk connection Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Run monitoring function every 200 ms timer.setInterval(200L, monitorProcess); Serial.println("LEAN STATION started"); } /************************************************************ 7. Main Loop ************************************************************/ void loop() { Blynk.run(); timer.run(); }