From b9d278a324b4e120603563e28405b02d37f70d06 Mon Sep 17 00:00:00 2001 From: "Alexander(AJ) Federici" Date: Wed, 20 Feb 2019 18:51:52 -0600 Subject: [PATCH] Update Watchdog.ino --- Watchdog/Watchdog.ino | 55 +++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/Watchdog/Watchdog.ino b/Watchdog/Watchdog.ino index 1ece08f8..c81636a0 100644 --- a/Watchdog/Watchdog.ino +++ b/Watchdog/Watchdog.ino @@ -24,50 +24,54 @@ void setup() { digitalWrite(outPin, 0); } +void setupState1(){ + changeCounter = 0; + waitPeriod = 52; + startTime = millis(); + goal = 1; + state = 1; +} + +void reset(){ + startTime = millis(); + changeCounter = 0; +} + +void checkHeartBeat(){ + heartBeat = digitalRead(inPin); + currTime = millis(); + //check for change in hearbeat + + if (heartBeat != lastBeat){ changeCounter++; } + + lastBeat = heartBeat; +} + + void loop() { //START STATE if (state == 0){ - currTime = millis(); - //check for change in hearbeat - heartBeat = digitalRead(inPin); - if (heartBeat != lastBeat){ changeCounter++; } - - lastBeat = heartBeat; - + checkHeartBeat(); //if we get 5 changes in a span of 260ms continue to healthy state if (changeCounter > goal){ - //sets everything up for the next state - changeCounter = 0; - waitPeriod = 52; - startTime = millis(); - goal = 1; - state = 1; + setupState1(); } //reset counters to continue waiting if (currTime - startTime > waitPeriod){ - startTime = millis(); - changeCounter = 0; + reset(); } } //HEALTHY STATE if (state == 1){ digitalWrite(outPin, 1); - heartBeat = digitalRead(inPin); - currTime = millis(); - //check for change in hearbeat - - if (heartBeat != lastBeat){ changeCounter++; } - - lastBeat = heartBeat; - + checkHeartBeat(); if (currTime - startTime >= waitPeriod){ if (changeCounter < goal){ state = 2; } - startTime = millis(); - changeCounter = 0; + reset(); } } @@ -78,6 +82,5 @@ void loop() { //DO NOTHING } } - }