Overriding Latching code with a momentary switch

Hi all,

I have two illuminated momentary pushbuttons, and with them I want to control two DPDT Relays (with the LEDs in the buttons powering at the same time as the relays (both on the same pin via a 2803).

The first relay/led (Relayled1) powers when I press "button one", and unpowers when I push button1 again - latching code, all good.

The second relay/led (Relayled2) I want to power when I push and hold "button 2", and unpower when I release button2 - not latching.

So far so simple?

The way I want to use them, however, is to have button1 (the latching part) work as above, but have the momentary (button2) code also power Relayled1 as well as Relayled2 when pressed and held.

The code below does this, but causes a squeal from somewhere - presumably the teensy(3.5) I'm using for this project (it's hard to tell) - when I press and hold button2 while Relayled1 is UNpowered. If Relayled1 is already powered, there is no squeal when pressing button 2.

So how do I make this work?! I tried this code with little 5v momentary LEDs (no relays) with my UNO, and it worked fine and silently...

Cheers

//LEDS+RELAYS

int Relayled1 = 6;
int Relayled2 = 7;

//SWITCHES

int Button1 = 8;
int Button2 = 9;

//SWITCH STATES etc...

int Button1State;
int Button2State;

int Button1StateReset = 0;
int Button2StateReset = 0;

boolean Relayled1On = false;


void setup() {

  pinMode(Relayled1, OUTPUT);
  pinMode(Relayled2, OUTPUT);

  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);

}


void loop() {

  ledRelayOn();
  ledRelayOff();
  whatTheButton();


  Button1State = digitalRead(Button1);
  Button2State = digitalRead(Button2);


}

void ledRelayOn() {


  if (Relayled1On == true) digitalWrite(Relayled1, HIGH);

}

void ledRelayOff() {


  if (Relayled1On == false) digitalWrite(Relayled1, LOW);

}

void whatTheButton() {

  //BUTTON 1////////////////////////////////////////////////////////////////////////////////////////////////////////////

  if (Button1StateReset != Button1State && Button1State == 0 && Relayled1On == false) {
    Button1StateReset = Button1State;
    Relayled1On = true;
  }
  if (Button1StateReset != Button1State && Button1State == 0 && Relayled1On == true) {
    Button1StateReset = Button1State;
    Relayled1On = false;
  }
  if (Button1StateReset != Button1State && Button1State == 1) {
    Button1StateReset = Button1State;
  }

  //BUTTON 2////////////////////////////////////////////////////////////////////////////////////////////////////////////

  Button2State = digitalRead(Button2);

  if (Button2State == LOW) {

    digitalWrite(Relayled2, HIGH), digitalWrite(Relayled1, HIGH);
  }
  else {

    digitalWrite(Relayled2, LOW);

  }
  delay (1);
}

Why do you deal with the state of the switches, and then read the state of the switches?

  if (Button2State == LOW) {

    digitalWrite(Relayled2, HIGH), digitalWrite(Relayled1, HIGH);
  }

I suspect that this forcing Relayled1 to HIGH when Button2 is pressed is fighting with the code trying to keep Relayled1 LOW. The relay is being turned on and off at audio frequencies (thousands of times per second) causing a squeal.

PaulS: Why do you deal with the state of the switches, and then read the state of the switches?

No reason, it's just a copy and paste job from a while ago. I suppose I don't need to, but it wouldn't affect anything would it?

johnwasser: ```  if (Button2State == LOW) {

   digitalWrite(Relayled2, HIGH), digitalWrite(Relayled1, HIGH);  }




I suspect that this forcing Relayled1 to HIGH when Button2 is pressed is fighting with the code trying to keep Relayled1 LOW. The relay is being turned on and off at audio frequencies (thousands of times per second) causing a squeal.

That's what I've been thinking, I'm just not sure what to do about it... Something along the lines of "if button2 is pressed, ignore button1 entirely until button2 is released"?? Any clues would be useful.

Cam1: That's what I've been thinking, I'm just not sure what to do about it... Something along the lines of "if button2 is pressed, ignore button1 entirely until button2 is released"?? Any clues would be useful.

That would probably work. Check button2 first and only check button1 if button2 is not pressed.