Back to Parent

#define TOUCH_SENSOR_PIN D2  // Pin connected to the capacitive touch sensor

bool wasTouched = false;  // Tracks the previous state of the sensor

void setup() {
    pinMode(TOUCH_SENSOR_PIN, INPUT);  // Set the sensor pin as input
    Serial.begin(9600);               // Optional: Debugging
}

void loop() {
    // Read the current state of the touch sensor
    int sensorValue = digitalRead(TOUCH_SENSOR_PIN);

    if (sensorValue == HIGH && !wasTouched) {
        // If the sensor is touched and it wasn't previously touched
        wasTouched = true;  // Update the state
        Serial.println("Touch detected! Publishing event.");
        Particle.publish("toggle_led", "on", PRIVATE);  // Publish the event
    } 
    else if (sensorValue == LOW && wasTouched) {
        // If the sensor is not touched anymore, reset the state
        wasTouched = false;
        Serial.println("Touch released.");
    }

    delay(50);  // Small delay for stability
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0