Hi all
I'm building a small timer to show the last time an object was used. The micro switch im ising to trigger the timer , when pressed in (ON), The problem is that when it has counted down , the loop just restarts the timer as the switch is still on. How can I stop the timer from restarting until the item is next used.
pic of setup added.
code added .
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 7, 6); // Software SPI
Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3); // Hardware SPI
// int hours = 0; // start hours
int minutes = 0; //start min
int seconds = 5; //start seconds
int e=0;
void setup()
{
Serial.begin(9600);
display.clearDisplay(); // clears the screen and buffer
display.begin();
// display.clearDisplay(); // clears the screen and buffer
// you can change the contrast around to adapt the display
// for the best viewing!
display.setContrast(50);
// display.clearDisplay(); // clears the screen and buffer
pinMode(A2, INPUT_PULLUP);
}
int readButtonsA2(int pin)
{
int f,g = 0;
g=analogRead(pin);
if (g<0)
{
f=0;
}
else
if (g>15 && g<25)
{
f=1;
}
return f;
}
void loop()
{
e=readButtonsA2(2);
if (e==0)
{
display.setCursor(0,0);
display.print("Press button");
display.display();
delay(1000);
}
else
if (e==1)
{
while (minutes > 0 || seconds >= 0)
{
{
display.setCursor(4, 24);
display.setTextSize(2);
(minutes < 10) ? display.print("0") : NULL;
display.print(minutes);
display.print(":");
(seconds < 10) ? display.print("0") : NULL;
display.print(seconds);
display.display();
stepDown();
delay(1000);
display.clearDisplay(); // clears the screen and buffer
}
}
}
}
void stepDown()
{
if (seconds > 0)
{
seconds -= 1;
} else
{
if (minutes > 0)
{
seconds = 59;
minutes -= 1;
} else
{
trigger();
}
}
}
void trigger()
{
display.clearDisplay();
display.setCursor(20, 24);
display.setTextSize(2);
display.print("END ");
delay(1000);
display.display();
reset();
}
void reset()
{
asm volatile (" jmp 0");
}