I have a program i am working on where a relay is turned on -> waits a delay -> then turns off after either one of two things happen: 1) A set timer is reached OR 2) someone pushes a push button to trigger it.
Right now 75% of my code is working and i am stuck on how i go about "locking out" the push button from triggering the relay, for a select-able amount of delay time.
The timer portion of the code, with select-able times, is working. Depending on what analog 1 see's, it will trigger the relay appropriately after the selected time.
I tried to implement this same approach for the lock out timer, using analog 2 , to determine how long of a delay. I used a variable i called LOK to either read the button if it is unlocked (LOK = 0) or if it is locked (LOK =1) to wait the allotted delay time before unlocking and allowing the button to be read again.
I've tried all sorta of things and it still isn't functioning right. Maybe there is a better or simpler way to do this but I've racked my brain, and key word searching isn't helping.
Code below - please forgive random comments. I've been chopping up and moving things around i haven't eddited any comments , so some may be from other snippets of codes from otehr projects.
int DUMP = 2; // Pin connected to big dump relay
int BUTTON = 3; // Pin connected to manual dump push button.
int BUTTONlight = 4; // Pin connected to push button light.
int BUTstate; // Current reading from BUTTON pin
int lastBUTstate = LOW; // Previous reading from BUTTON pin
long lastDebounce = 0; // Last time output was changed
long Delay = 50; // Debounce Time
unsigned long LOT;
unsigned long DUMPTIME;
unsigned long prevTIME;
int a=0;
int d=0;
int LOK = 0;
void setup()
{
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode (BUTTON, INPUT);
pinMode (BUTTONlight, OUTPUT);
pinMode (DUMP, OUTPUT);
digitalWrite(DUMP, HIGH);
delay(10000);
digitalWrite(DUMP, LOW);
}
int readDTselector(int pin)
// returns the button number pressed, or zero for none pressed
// int pin is the analog pin number to read
{
int b,c = 1;
c=analogRead(pin); // get the analog value
if (c>1000)
{
b=0; // buttons have not been pressed
}
else
if (c<1000 && c>600)
{
b=1; // button 1 pressed
}
else
if (c<520 && c>400)
{
b=2; // button 2 pressed
}
else
if (c>100 && c<400)
{
b=3; // button 3 pressed
}
else
if (c<100)
{
b=4; // button 4 pressed
}
return b;
}
int readLOselector(int pin)
// returns the button number pressed, or zero for none pressed
// int pin is the analog pin number to read
{
int e,g = 2;
g=analogRead(pin); // get the analog value
if (g>1000)
{
e=0; // buttons have not been pressed
}
else
if (g<1000 && g>600)
{
e=1; // button 1 pressed
}
else
if (g<520 && g>400)
{
e=2; // button 2 pressed
}
else
if (g>100 && g<400)
{
e=3; // button 3 pressed
}
else
if (g<100)
{
e=4; // button 4 pressed
}
return e;
}
void loop()
{
DTselect();
LOselect();
int reading = digitalRead(BUTTON);
unsigned long currTIME = millis();
if (LOK == 0)
{
digitalWrite(BUTTONlight, HIGH);
if (reading != lastBUTstate)
{
lastDebounce = millis();
}
if ((millis() - lastDebounce) > Delay)
{
BUTstate = reading;
}
}
if ((currTIME - prevTIME) >= DUMPTIME || BUTstate == HIGH ) // Count to 2 minutes
{
LOK = 1;
digitalWrite(BUTTONlight, LOW);
digitalWrite(DUMP, HIGH);
delay(10000);
digitalWrite(DUMP, LOW);
prevTIME = currTIME;
}
if (LOK == 1)
{
delay(LOT);
LOK = 0;
}
lastBUTstate = reading;
}
int DTselect()
{
a=readDTselector(1);
if (a==0) // no buttons pressed
{
DUMPTIME = 210000;
}
else
if (a==1) // someone pressed a button!
{
DUMPTIME = 180000;
}
else
if (a==2) // someone pressed a button!
{
DUMPTIME = 150000;
}
else
if (a==3) // someone pressed a button!
{
DUMPTIME = 120000;
}
else
if (a==4) // someone pressed a button!
{
DUMPTIME = 90000;
}
return DUMPTIME;
}
int LOselect()
{
d=readLOselector(2);
if (d==0) // no buttons pressed
{
LOT = 20000;
}
else
if (d==1) // someone pressed a button!
{
LOT = 15000;
}
else
if (d==2) // someone pressed a button!
{
LOT = 10000;
}
else
if (d==3) // someone pressed a button!
{
LOT = 5000;
}
else
if (d==4) // someone pressed a button!
{
LOT = 0;
}
return LOT;
}