// ----- VoltGuard project ------
// A 5v DTDP relay protects dual probe inputs.
// display DC/AC voltage in default mode (via a R divider & bridge rectifier).
// on button press if voltage across the two probes exceeds a set voltage the device tries to discharge it (assuming it is a charged C.)
// if/when voltage falls below a set voltage it activates relay and the signature capture starts, then hands back to Voltguard, de activates relay.
// waits for button (re)press.
// thus ensuring the input pins used by sigAnalyser never gets zapped by a high voltage.
// buzzer/speaker er pin 3 used as audible progress/status indicater.
//...........................................................................................
// Define the pin numbers for easier reference
// if sharing with sigAnalyser, free pins are: d2,d3,d4,d5,d6,d7,A2,A3, keep A4 & A5 free for I2C
const int buttonPin = 2; // 'sample' Button connected to digital pin D7 / GND
const int buzzer = 3; // buzzer or speaker (active or passive?) (D3 is a PWM pin)
const int discharge = 4; // Digital pin D8, extra discharge R
const int handoff = 5; // Digital pin D9, indicates handoff/ Relay control/coil
const int analogPin = A2; // Analog input pin just for voltGuard
int buttonLastState = LOW; // button state for button logic
//--- SETUP -------------------------------------------------------------------------------------
void setup() {
// Start serial communication at 115200 bits per second
Serial.begin(115200);
// Configure the digital pins
pinMode(buttonPin, INPUT_PULLUP); // Set D7 as input with internal pull-up resistor
pinMode(discharge, OUTPUT); // Set D8 as input, to go low for extra discharge
pinMode(handoff, OUTPUT); // Set D9 as output indicates handoff
pinMode(buzzer, OUTPUT);
// Initialize D9 to LOW
//digitalWrite(discharge, HIGH);// diagnostic flash only
//digitalWrite(handoff, HIGH);
//delay(1000);
digitalWrite(discharge, LOW);
digitalWrite(handoff, LOW);
delay(1000);
}
//==== LOOP ======================================================================================
void loop() {
// Main loop monitors the voltage on A0 and uses button logic to call signature capture function
if (digitalRead(buttonPin) == HIGH) {
buttonLastState = HIGH;// set button state for button logic
digitalWrite(handoff, LOW);// handoff indicate off
digitalWrite(discharge, LOW);// extra discharge R off
}
float voltage = analogRead(analogPin) * (5.0 / 1023.0); // Convert analog reading to voltage
// Print the voltage to the Serial Monitor
Serial.print("Voltage on A0: ");
Serial.println(voltage);
delay(200);
// Check if the button is pressed (buttonPin will read LOW when pressed due to pull-up resistor)
if ((digitalRead(buttonPin) == LOW) && (buttonLastState == HIGH)) {// new button press event?
Serial.print("new button press event. ");
if (voltage > 2.0) {
digitalWrite(discharge, LOW);// make sure extra discharge R is off
}
if (voltage <= 2.0) {
digitalWrite(discharge, HIGH);// enable extra discharge R
Serial.print("extra discharge R added ");
}
if (voltage < 1.0) {
digitalWrite(handoff, HIGH);// handoff indicate
Serial.print(" handoff... ");
delay(3000);// proxy for function call to activate relay and do signature capture
Serial.println(" handback ");
buttonLastState = LOW;// set button state for button logic (need to wait to take finger off button)
digitalWrite(handoff, LOW);// clean up ; handoff indicate off
digitalWrite(discharge, LOW);// clean up ; extra discharge R off
}
}
}
//=== LOOP END ========================================================================================
// Function Definitions