#define relayPin 8 // the pin relay is connected
#define potPin A0 // the pin potentiometer is connected
#define resetPin 3 // the pin where rest switch is connected
#define startPin 2 // the pin where start switch is connected
#define relayType 'L' // Relay trigger type. L for low and H for high
const int maxTime = 30000;// maximum timer time in milliseconds
const int minTime = 3000; // miniimum timer time in milliseconds
// do not change anything bellow here
long duration;
int potValue;
long rememTime;
int relayState =0;
void setup() {
pinMode(relayPin, OUTPUT);// define relayPin as output
pinMode(resetPin, INPUT_PULLUP);// define input pin with pullup (watch video)
pinMode(startPin, INPUT_PULLUP);// define input pin with pullup (watch video)
attachInterrupt(digitalPinToInterrupt(resetPin), reset, LOW);
Serial.begin(9600);// initialize serial monitor with 9600 baud
if(relayType == 'L')
{
digitalWrite(relayPin, HIGH);// turn the relay OFF and keep it OFF
Serial.println(" Relay Timer");
Serial.println("Relay Type: LOW Trigger");
}else{
digitalWrite(relayPin, LOW);// turn the relay OFF and keep it OFF
Serial.println("Relay Timer");
Serial.println("Relay Type: HIGH Trigger");
}
}
void loop() {
potValue = analogRead(potPin)/10;// reads the value of the potentiometer (value between 0 and 1023)
duration = map(potValue, 0, 102, minTime, maxTime);// convert A0 value to time set at minTime and maxTime
if(digitalRead(startPin) ==LOW)
{
rememTime = millis();
relayState = 1;
controlRelay();// send command to turn the relay ON
}
if( (millis()- rememTime) > duration )
{
relayState = 0;
controlRelay();
}
Serial.print("Time: ");
Serial.print(duration/1000);
Serial.println(" Seconds");
delay(200); // wait for 200 milliseconds
}// loop end
void controlRelay()
{
if(relayType == 'L')
{
if(relayState == 1)
{
digitalWrite(relayPin, LOW);// Turn ON relay
Serial.print("LT-Relay ON for ");
Serial.print(duration/1000);// display in seconds
Serial.println(" Seconds");
}else{
digitalWrite(relayPin, HIGH); // turn OFF
Serial.println("====Relay is OFF");
}
}else{
if(relayState == 1)
{
digitalWrite(relayPin, HIGH);
Serial.print("HT-Relay ON for ");
Serial.print(duration/1000);// display in seconds
Serial.println(" Seconds");
}else{
digitalWrite(relayPin, LOW);
Serial.println("==Relay OFF");
}
}
}//controlRelay end
void reset()
{
duration =0;
if(relayType == 'L')
{
digitalWrite(relayPin, HIGH);// turn OFF High trigger relay
}else{
digitalWrite(relayPin, LOW);// turn OFF Low trigger relay
}
Serial.println("Relay OFF");
}//reset()
تمام