Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <DigiCDC.h>
- //USB hardware watchdog project
- //Jason Greathouse 07/11/2011
- #define level_1 60 // 1 min
- #define level_2 300 // 5 min
- #define level_3 480 // 8 min
- #define level_4 540 // 9 min
- #define level_end 600 // 10 min
- int incomingByte = 0; // for incoming serial data
- int notAlive = 0; // counter for seconds between alive
- int handshake = 0; // have we connected to watchdog daemon
- int relayPin = 2; //pin for relay
- int intled = 1; //pin for led
- int beeper = 0; //beep
- long tick = 1000; //miliseconds in a second
- long previousMillis = 0;
- int printedl1 = 0; //Just "flags" to keep it from repeating messages
- int printedl2 = 0;
- int printedl3 = 0;
- int printedl4 = 0;
- int printedend = 0;
- void setup() {
- SerialUSB.begin(); // opens serial port, sets data rate to 9600 bps
- pinMode(relayPin, OUTPUT); //Set relay pin
- pinMode(intled, OUTPUT);
- pinMode(beeper, OUTPUT);
- digitalWrite(relayPin, LOW);
- establishContact(); //Wait for inital Keep Alive before starting timers
- }
- void loop() {
- unsigned long currentMillis = millis(); //Currnet number of miliseconds since boot.
- if (SerialUSB.available() > 0) {
- // read the incoming byte:
- incomingByte = SerialUSB.read();
- }
- if (incomingByte == 'A') {
- //we got an alive message
- SerialUSB.println("Received Alive");
- notAlive = 0;
- printedl1 = 0; //Just "flags" to keep it from repeating messages
- printedl2 = 0;
- printedl3 = 0;
- printedl4 = 0;
- printedend = 0;
- digitalWrite(intled, HIGH); // set the LED on
- delay(2000); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- }
- if (incomingByte == 'R') {
- //we got a going down for reboot message, going back to establish contact.
- SerialUSB.println("Received Reboot Message");
- establishContact();
- notAlive = 0;
- printedl1 = 0; //Just "flags" to keep it from repeating messages
- printedl2 = 0;
- printedl3 = 0;
- printedl4 = 0;
- printedend = 0;
- }
- // count off seconds
- if (currentMillis - previousMillis > tick) {
- previousMillis = currentMillis;
- notAlive = notAlive + 1;
- //Serial.print("No Alive for ");
- //Serial.print(notAlive);
- //Serial.println(" seconds");
- }
- //Not alive warnings for 60/300/600 seconds
- if (notAlive == level_1) {
- if (printedl1 == 0) {
- SerialUSB.println("No Alive for level_1");
- printedl1 = 1;
- digitalWrite(intled, HIGH); // set the LED on
- delay(60); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- delay(1); // wait for a second
- analogWrite(beeper, 10);
- delay(80);
- analogWrite(beeper, 0);
- }
- }
- if (notAlive == level_2) {
- if (printedl2 == 0) {
- SerialUSB.println("No Alive for level_2");
- printedl2 = 1;
- digitalWrite(intled, HIGH); // set the LED on
- delay(120); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- delay(1); // wait for a second
- analogWrite(beeper, 10);
- delay(80);
- analogWrite(beeper, 0);
- delay(80);
- analogWrite(beeper, 10);
- delay(100);
- analogWrite(beeper, 0);
- }
- }
- if (notAlive == level_3) {
- if (printedl3 == 0) {
- SerialUSB.println("No Alive for level_3");
- printedl3 = 1;
- digitalWrite(intled, HIGH); // set the LED on
- delay(120); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- delay(1); // wait for a second
- analogWrite(beeper, 10);
- delay(80);
- analogWrite(beeper, 0);
- delay(80);
- analogWrite(beeper, 10);
- delay(100);
- analogWrite(beeper, 0);
- }
- }
- if (notAlive == level_4) {
- if (printedl4 == 0) {
- SerialUSB.println("No Alive for level_4");
- printedl4 = 1;
- digitalWrite(intled, HIGH); // set the LED on
- delay(120); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- delay(1); // wait for a second
- analogWrite(beeper, 10);
- delay(80);
- analogWrite(beeper, 0);
- delay(80);
- analogWrite(beeper, 10);
- delay(100);
- analogWrite(beeper, 0);
- }
- }
- if (notAlive >= level_end) {
- //we have not seen an "A" in 600 seconds
- SerialUSB.println("No Alive for level_end - Rebooting Now!");
- digitalWrite(intled, HIGH); // set the LED on
- delay(500); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- delay(80); // wait for a second
- analogWrite(beeper, 50);
- delay(80);
- analogWrite(beeper, 0);
- delay(80);
- analogWrite(beeper, 50);
- delay(80);
- analogWrite(beeper, 0);
- delay(80);
- analogWrite(beeper, 50);
- delay(200);
- analogWrite(beeper, 0);
- digitalWrite(relayPin, HIGH); //Hit reset button
- delay(1000);
- digitalWrite(relayPin, LOW);
- //delay(600000); //wait 10 min for the reboot
- notAlive = 0;
- printedl1 = 0; //Just "flags" to keep it from repeating messages
- printedl2 = 0;
- printedl3 = 0;
- printedl4 = 0;
- printedend = 0;
- establishContact();
- }
- incomingByte = 0;
- }
- //wait for an inital keepalive before starting timer
- void establishContact() {
- while (incomingByte != 'A') {
- if (SerialUSB.available() > 0) {
- // read the incoming byte:
- incomingByte = SerialUSB.read();
- }
- SerialUSB.println("Waiting for Initial KeepAlive...");
- delay(1000);
- digitalWrite(intled, HIGH); // set the LED on
- delay(50); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- }
- SerialUSB.println("Initialized, Timer Started.");
- incomingByte = 0;
- digitalWrite(intled, HIGH); // set the LED on
- delay(1000); // wait for a second
- digitalWrite(intled, LOW); // set the LED off
- //delay(1); // wait for a second
- analogWrite(beeper, 10);
- delay(50);
- analogWrite(beeper, 0);
- }
Add Comment
Please, Sign In to add comment