Coding to use multiple push buttons - interfering

I’m trying to use three push buttons to control EL wire for a wearable. When I prototyped with LED’s the three push buttons worked fine. Now that I’ve complicated the circuit with an inverter, optoisolators, and triacs, the buttons are interfering with each other.

Here’s the code:

int LEDPin = 2;
int red = 6;
int green_one = 8;
int green_two = 10;
int yellow = 12;
int button1 = 3;
int button2 = 4;
int button3 = 5;
int push1;
int push2;
int push3;
int change = 1;
int change2 = 1;
int change3 = 1;
int state = 0;
int state2 = 0;
int state3 = 0;
int delay_value=500;

// the setup function runs once when you press reset or power the board
void setup(){
  //initialize each pin for INPUT or OUTPUT
  pinMode(LEDPin, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
  pinMode(green_one, OUTPUT);
  pinMode(green_two, OUTPUT);
  pinMode(button2, INPUT);
  pinMode(button1, INPUT);
  pinMode(button3, INPUT);
}

//the loop function runs over and over again forever
void loop(){
  push1=digitalRead(button1); // set push to left signal value
  push2=digitalRead(button2); // set push to right signal value
  push3=digitalRead(button3); // set push to stop signal value
  digitalWrite(LEDPin, LOW); // set indicator to LOW
  digitalWrite(yellow, HIGH); // set cyclist symbol to HIGH
  digitalWrite(red, LOW); // set stop cyclist to LOW
  digitalWrite(green_one, LOW); // set left signal to LOW
  digitalWrite(green_two, LOW); // set right signal to LOW
  
  if(push1==LOW && change==HIGH){ //allows left signal button to be pushed and released
    state=1-state;                //and signal stays on until pushed and released again
  }change=push1;
  if(push2==LOW && change2==HIGH){//allows stop signal button to be pushed and released
    state2=1-state2;              //and signal stays on until pushed and released again
  }change3=push3;
  if(push3==LOW && change3==HIGH){//allows right signal button to be pushed and released
    state3=1-state3;              //and signal stays on until pushed and released again
  }change3=push3;
  
  if(state==1){ 
    digitalWrite(yellow, LOW); // turn cycle symbol off
    digitalWrite(LEDPin, HIGH); // indicator LED on
    delay(delay_value); digitalWrite(LEDPin, LOW); digitalWrite(green_one, HIGH);
    delay(delay_value); // left signal blinking
  }
  if(state3==1){
    digitalWrite(yellow, LOW); // turn cycle symbol off
    digitalWrite(LEDPin, HIGH); // indicator LED on
    delay(delay_value); digitalWrite(LEDPin, LOW); digitalWrite(green_two, HIGH);
    delay(delay_value); // right signal blinking
  }
  if(state2==1){
    digitalWrite(LEDPin, HIGH); // indicator LED on
    digitalWrite(yellow, LOW); // yellow cycle symbol off
    digitalWrite(red, HIGH); // red cycle symbol on
    delay(delay_value);
  }if(state==0 and state2==0 and state3==0){ 
    digitalWrite(LEDPin, LOW); // indicator off
    digitalWrite(yellow, HIGH); // cycle symbol on -- the normal state of the lights
  }
}

An image of the circuit is attached - note* optocouplers are actually optoisolators used to keep DC and AC currents separate.

I need each button to control one behavior of just one piece of EL wire (two green lights for blinking, one red light to turn and stay on - default behavior is yellow light on). When I try to run this program, however, the buttons act as if always HIGH and the lights blink on and off in a dance. Though this adorable, not the function I’m looking for.

Any ideas how to isolate the button behavior?

Measure the voltage on D3-5.

Do they go from 5v when the switch is closed to 0v when open.

You may have a split power rail breadboard.

No comment on the problem, but one about your code. It is very difficult to read. You have a mix of 0s and 1s and HIGH and LOW. Stick with one. This:

state=1-state;

would be much easier to read and understand as

state = LOW;

Also, instead of

int change = 1;
int change2 = 1;
int change3 = 1;
int state = 0;
int state2 = 0;
int state3 = 0;

I would make it

int change1 = 1;
int change2 = 1;
int change3 = 1;
int state1 = 0;
int state2 = 0;
int state3 = 0;

for better readability.

What is this?

if(state==0 and state2==0 and state3==0)

What happened to &&?

Metallor:
What is this?

if(state==0 and state2==0 and state3==0)

What happened to &&?

https://en.cppreference.com/w/cpp/language/operator_alternative

Interesting, that is good to know. Still would like to see OP choose one or the other haha.

Hi,
Welcome to the forum.

Instead of all the IF statements you might like to look at switch.. case...

https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/

Tom... :slight_smile: