/* Junsi Delay with HVC and LVC (High Voltage Cut, Low ...) Delay minutes is read from Pin A1 (0..5 V) using a 10 kOhm potentiometer. For a 3 minute delay, this is 1.50 V. Junsi alarm signal, active LOW, on A2 starts process: If the battery pack voltage (Vcc) > 13.0 V = v_cut then HVC is required. If its < 13.0 V then LVC is required. We assume Vcc is input to ATtiny using a 10 kOhm pot set to v_cut/3 V = 4.33 V. For HVC case, Pin 1 is set high and Delay minutes commences and then Pin 1 is set low. Pin 1 drives a grounded open-collector transistor to switch the grounded power relay(s). For LVC case, Pin 0 is set high for 1 second and then is set low. A delay of Delay minutes then commences. Pin 0 drives an open collector transistor that can switch an 80 A latching relay OFF. Peter Manins. Revised 13 Sep 2014 */ bool HVC = true, LVC = false; int timed; //time delay duration (0..10 min) const float scaleV = 1.0/3.0; //voltage divider on Vcc const float v_cut = 13.0; //Cut point. HVC above, LVC below const byte timedPin = A1; //Input pin to set time delay (min) const byte alarmPin = A2; //Input pin for alarm from Junsi const byte railPin = A3; //Input pin for rail voltage const byte HVC_Pin = 1; //Output pin for HVC relay controller const byte LVC_Pin = 0; //Output pin for LVC relay controller //LED on Programmer Board void setup() { pinMode(HVC_Pin, OUTPUT); pinMode(LVC_Pin, OUTPUT); } void wait(int len) { //'len' is wait time in seconds for (int ii=0; ii < len; ii++) delay(1000); } bool Query_alarm (byte pin) { // Has Junsi alarm gone off? const int count = 5; bool a = true; for (int ii=0; ii < count; ii++) {// Check 'count' times delay(50); a = a && (digitalRead(pin) == LOW); } return a; //'True' only if true 'count' times } bool Query_HvLv (byte pin) { // Is the rail voltage high or low? const int count = 5; float voltage = 0; bool a; for (int ii = 0; ii < count; ii++) { // Check 'count' times delay(50); voltage = voltage + analogRead(pin); } voltage /= count; voltage = voltage * 5.0 / 1024.0; //scaled between 0..5 V if (voltage > v_cut * scaleV) a = HVC; else a = LVC; return a; } void loop() { //the time delay for HVC is set here rather than in setup so the user can //change it without having to cycle power to the circuit timed = 600.0 * analogRead(timedPin)/1024.0; if (Query_alarm(alarmPin)) { if (Query_HvLv(railPin)) { //If HVC alarm ... digitalWrite(HVC_Pin, HIGH); wait(timed); //to allow loads to bleed energy digitalWrite(HVC_Pin, LOW); } else { //Its LVC alarm ... digitalWrite(LVC_Pin, HIGH); wait(1); digitalWrite(LVC_Pin, LOW); wait(timed); //so if LVC is held, relay is not hammered } } else { digitalWrite(HVC_Pin, LOW); digitalWrite(LVC_Pin, LOW); } } //end program