#ifndef NonLatchingRelay_h #define NonLatchingRelay_h #ifndef Arduino_H #include #endif class NonLatchingRelay { private: String name = "Non-Latching Relay"; unsigned int pin; unsigned int pin_readback; bool pin_desired_state; // The desired state of the pin bool pin_actual_state; // The actual state of the pin per the readback bool pin_working; unsigned int voltage_read_pin; bool desired_state = false; // The desired state of the relay, set by the control program bool expected_state; // The expected state of the relay based off of digital spin readback bool actual_state; // Whether or not there actually is a voltage across the relay or not bool relay_working; bool startup = true; // This is used so that the object knows whether or not to use the normal or startup function to control const unsigned long relay_delay = 500; // The max time in ms that we would expect the Arduino to see a voltage change across the relay after turning on the pin unsigned long relay_turned_on_time; // The millis() that the relay was turned on. Useful for knowing if it is operating correctly unsigned long relay_turned_off_time ; // The millis() that the relay was turned off. Useful for knowing if it is operating correctly unsigned long relay_first_voltage_time; // The millis() that we first saw a voltage across the relay after trying to turn it on unsigned long relay_first_no_voltage_time; // The millis() that we first saw no voltage across the relay after trying to turn it off const unsigned int num_voltage_reads = 3; // How many times we should read the relay voltage and average float read_relay_voltage(); // Get a single voltag reading across the relay void update_actual_state(); // Average a few voltage readings and determine if the relay has ~12V across it void update_if_pin_working(); void update_if_relay_working(); void turn_on_relay(); void turn_off_relay(); void turn_on_relay_startup(); // Turns on relay as well as setting values that may have been uninitialized void turn_off_relay_startup(); // Turns off relay as well as setting values that may have been uninitialized public: NonLatchingRelay(String name, unsigned int pin, unsigned int pin_readback, unsigned int voltage_read_pin); NonLatchingRelay(unsigned int pin, unsigned int pin_readback, unsigned int voltage_read_pin); bool get_pin_working() const {return pin_working;} // Gets the state of the pin_working bool get_desired_state() const {return desired_state;} // Gets the desired state of the relay bool get_expected_state() const {return expected_state;} // Gets the expected state of the relay bool get_actual_state() const {return actual_state;} // Gets the actual state of the relay bool get_relay_working() const {return relay_working;} // Gets whether or not the relay is working correctly unsigned long get_relay_first_voltage_time() const {return relay_first_voltage_time;} // Gets the time that the first voltage was seen after trying to turn on unsigned long get_relay_first_no_voltage_time() const {return relay_first_no_voltage_time;} // Gets the time that the first no voltage was seen after trying to turn off void set_desired_state(unsigned char new_desired_state); // Set the desired state of the relay void control_relay(); // Controls the relay including pins to get relay to desired state, checking if the pin needs to be turned off, and error detection }; #endif