Paso 3: codificación
int touchPin = 2; //The pin of the touch sensorint relayPin = 12; //The pin of the relay int relayState = HIGH; // relayState stores the state of relay int touchState; // touchState stores the state of touch sensor int lastTouchState = LOW; // lastTouchState stores the last state of touch sensor long lastDebounceTime = 0; long debounceDelay = 50; // debounce time void setup() { pinMode(touchPin, INPUT); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, relayState); } void loop() { //read the touch sensor state into the reading variable. int reading = digitalRead(touchPin); // if the state of touch sensor changes, stamp the time. if (reading != lastTouchState) { lastDebounceTime = millis(); } // Wait 50ms to confirm the change really happens // If the new state of the touch sensor is HIGH, then change the state of the relay. if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != touchState) { touchState = reading; if (touchState == HIGH) { relayState = !relayState; } } } digitalWrite(relayPin, relayState); // record the last state of the touch sensor lastTouchState = reading; }
Después de subir el boceto y se puede tocar el sensor para controlar el LED.