I try my code to change the state of a relay on pushing a push button buy checking the state of a digital pin.
My code :
const int led1 = 6;
const int enable1 = 7;
boolean led1_OnOff;
long now;
int minSecsBetweenUpdates = 1; // 1 seconds
long lastSend = -minSecsBetweenUpdates * 1000l;
void setup() {
led1_OnOff = false;
pinMode(enable1, INPUT);
pinMode(led1, OUTPUT);
}
void loop() {
now = millis();
if (now > (lastSend + minSecsBetweenUpdates * 1000l))
{
if(digitalRead(enable1) == HIGH)
{
lastSend = now;
if(led1_OnOff == true)
{
led1_OnOff = false;
//+digitalWrite(led1, LOW);
}
else
{
led1_OnOff = true;
//digitalWrite(led1, HIGH);
}
}
}
if(led1_OnOff == true)
digitalWrite(led1, HIGH);
else
digitalWrite(led1, LOW);
}
But the code keeps on looping and the relay keep changing it's state as if pin 7 is always high, I'm sure of my schematic but I don't know what's wrong with my code.
for those who want my schematic:
