Hi all, before i get into it I want to thank anyone who contributes to this discussion.
So ive been developing a system that acts as a timer, data-logger, and sensor feedback. The data from the sensor and time are displayed on a seven segment display.
There are three buttons being implemented:
- External reset
- Timer stop/start
- Switches LCD display between timer and sensor feedback.
Currently i am having a bit of trouble with the buttons, it appears that they only react when the program is looking for a state change thus the buttons will not act as they are intended unless activated at a specific moment. This is where I believe an interrupt would be useful but I would need to couple this with a hardware debounce (capacitor and inverse schmidt trigger) from what I have seen.
My questions is, am i on the right track with this line of thinking? Is there a better way of handling such tasks?
I want to be certain this is the right direction before placing a parts order.
Please note that the code below is for the timer only but there is almost no difference between this and the integrated version.
#define LED 10
#define button_1 8
int val = 0;
float startTime;
float stopTime;
void setup() {
pinMode(button_1, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
startTimer();
stopTimer();
readTime();
}
void readTime(){
while (digitalRead(button_1) == LOW && val == 1){
Serial.println(stopTime/1000);
delay(20);
return elapsedTime();
}
}
void startTimer(){
if (digitalRead(button_1) == HIGH && val == 0){
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
val = 1;
startTime = millis();
}
}
void stopTimer(){
if (digitalRead(button_1) == HIGH && val == 1){
digitalWrite(LED, HIGH);
delay(10);
digitalWrite(LED, LOW);
val = 0;
}
}
void elapsedTime(){
stopTime -= ((millis() - startTime));
startTime = millis();
}