Skip to content

Instantly share code, notes, and snippets.

@markusbaden
Created January 15, 2012 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusbaden/1614838 to your computer and use it in GitHub Desktop.
Save markusbaden/1614838 to your computer and use it in GitHub Desktop.
Hearbet interlock for Arduino
// Constant Variables
// Pin receiving the signal that experiment is still running
const byte heartPin = 2;
// Pin controlling the analog interlock
const byte cmdPin = 3;
// Time interval the interlock will stay
// active after having received the heartbeat
// 10 min = 600000 ms
const unsigned long activeInterval = 1200000;
//const unsigned long activeInterval = 1000;
// Variables that will changed
unsigned long lastPressed;
unsigned long currentMillis;
boolean overflowDetected = LOW;
// Start in sleep mode, when the first heartbeat
// is detected this will be pulled low. Starting
// HIGH ensures that the interlock is inactive
// when first plugged in. (lastPressed is initally
// zero, so for the first time period
// currentTime - lastPress < activeInterval)
boolean sleep = HIGH;
void setup() {
// Setup pins
pinMode(heartPin, INPUT);
pinMode(cmdPin, OUTPUT);
}
void loop() {
// Check whether we have received a heartbeat
// that is a HIGH on that pin
currentMillis = millis();
// The current millis are smaller then last pressed
// millis overflowed. When the overflown millis reaches
// the last Pressed value again, then the condition will
// become false again, however as long as the condition
// was met once, overflowDetected will stay high until
// another hearbeat is received.
if (currentMillis < lastPressed) {
overflowDetected = HIGH;
}
// If we had an overflow and the millis reached last pressed again
// then a long time has passed and we go to sleep mode until
// another heartbeat was received
if (overflowDetected && (currentMillis > lastPressed -1)) {
sleep = HIGH;
}
if (digitalRead(heartPin)) {
lastPressed = currentMillis;
overflowDetected = LOW;
sleep = LOW;
digitalWrite(cmdPin, HIGH);
}
else {
// if we are not sleeping and the last interval was
// not to far away then we are active
if (!sleep && (currentMillis - lastPressed < activeInterval)) {
digitalWrite(cmdPin, HIGH);
}
else {
digitalWrite(cmdPin, LOW);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment