Momentary Pushbuttons to Toggle Dual Coil Latching Relays On/Off

@szukalski Thanks for sharing your code and thoughts!

I just realized I do something similar to what you are working through. I have a dual non-latching relay pcb & code (mostly) working where I read 2 footswitches with one analog pin (the reset pin actually!). Below I posted a snippet of the code for how I do the analog read in case it is useful to someone.

C++:
void poll_switch(){
    //uint8_t current_switch_state = !(PINB & 0b100);
    int adc_l=ADCL;                // value of INput Voltage in lower register
    int adc_val=(ADCH<<8)|adc_l;   // Reading ADCH and combining the data

    if(adc_val <= 600){//current_switch1_state){
        // If switch is pressed
        if(switch1_state == OFF){
            // If the switch was previously not pressed
            // (the switch changed from not pressed to pressed)
            relay1_toggle();
            led1_toggle();
            switch1_state = ON;
            // To debounce, we wait DEBOUNCE_MS milliseconds
            // before polling the switch again
            _delay_ms(DEBOUNCE_MS);
        }
    }
    else if(adc_val >=800){
        if(switch2_state == OFF){
          relay2_toggle();
          led2_toggle();
          switch2_state = ON;
          _delay_ms(DEBOUNCE_MS);
        }
    }

Here is what the relevant part of the schematic looks like too:

SCR-20230620-kfdp.png
 
Back
Top