hi,
im also new at this but it sounds like you want something like this
This is untested code :
const int sensor = 2; // the number of the sensor pin
const int relay = 3; // the number of the relay pin
int sensorState = 0; // variable for reading the sensor status
void setup() {
pinMode(sensor, INPUT); // initialize the sensor pin as an input:
pinMode(relay, OUTPUT); // initialize the relay pin as an output:
}
void loop(){
sensorState = digitalRead(sensor); // read the state of the sensor value:
if (sensorState == HIGH) { // if it is, the sensorState is HIGH:
digitalWrite(relay, HIGH); // turn relay on:
delay(5000); // wait 5 seconds
digitalWrite(relay, LOW); // turn relay off:
}
else {
digitalWrite(relay, LOW); // turn relay off:
}
}
thinking about it, you could even remove the first digitalWrite(relay, LOW); // turn relay off: as this could make the relay turn off for a split second while the sensor is still HIGH, and then you should still get about 5 second from the time the sensor go's LOW
hi, im just testing the code myself, and it works for me, but i am using a push button, but the time delay can be a bit out, i have changed the line that may turn the relay off and back on even when the input is high.
const int sensor = 2; // the number of the sensor pin
const int relay = 13; // the number of the relay pin
int sensorState = 0; // variable for reading the sensor status
void setup() {
pinMode(sensor, INPUT); // initialize the sensor pin as an input:
pinMode(relay, OUTPUT); // initialize the relay pin as an output:
}
void loop(){
sensorState = digitalRead(sensor); // read the state of the sensor value:
if (sensorState == HIGH) { // if it is, the sensorState is HIGH:
digitalWrite(relay, HIGH); // turn relay on:
delay(5000); // wait 5 seconds
}
else {
digitalWrite(relay, LOW); // turn relay off:
}
}