Switch States With A Push button

Good afternoon Guys,

I am trying to design something to switch audio sources, i am an electronics engineer by trade and so understand all the hardware aspects of my project but have little to no understanding of programing, long and short of it is i am not going to beat about the bush and am looking for someone to complete my code for me as my timescale is short in the hope i can learn from it for the future. what i am trying to achieve is that every time i press my push button it changes the state of my outputs giving me a 4 different combinations in total of my 2 outputs being high or low then returns to the beginning of the sequence if i press the button once more. as below:

by default - output 1 low & output 2 low
first press - output 1 high & output 2 low
second press - output 1 low & output 2 high
third press - output 1 high & output 2 high
fourth press - return to the start

i have included the (Very) basic bit of code of what i want each of the outputs to but other than that i am stuck so any help would be appreciated.

void setup() {
  pinMode(2, INPUT); //INPUT 1 PUSH BUTTON
  pinMode(5, OUTPUT); //OUTPUT 1
  pinMode(6, OUTPUT); //OUTPUT 2
  digitalWrite(2, HIGH); //SET PULL-UP
 
}
void loop() {


{
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}


{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}


{
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
}


{
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
}
}

Thanks,

Phil.

pcoe149:
long and short of it is i am not going to beat about the bush and am looking for someone to complete my code for me as my timescale is short in the hope i can learn from it for the future.

In that case, you may be better off posting in the Gigs and Collaboration section.

Although your last statement

i have included the (Very) basic bit of code of what i want each of the outputs to but other than that i am stuck so any help would be appreciated.

Seems more fitting for this section, so I'll assume you're in the right place for now and you can let us know otherwise.

The first step is to get the push button to change states working. The StageChangeDetection example outlines how to do that.

Here’s one way:

byte currentState = 0;

void loop()
{
  if (digitalRead(2) == LOW)      // Handle button press
  {
    delay(50);                    // debounce
    if (currentState == 0)
    {
      digitalWrite(5, HIGH);
      digitalWrite(6, LOW);
      currentState = 1;
    }
    else if (currentState == 1)
    {
      digitalWrite(5, LOW);
      digitalWrite(6, HIGH);
      currentState = 2;
    }
//... Fill in the other two states
    while (digitalRead(2) == LOW);  // Do nothing until the button is released
    delay(50);                      // debounce
  }
}

pcoe149:
every time i press my push button it changes the state of my outputs giving me a 4 different combinations in total of my 2 outputs being high or low then returns to the beginning of the sequence if i press the button once more.

The code you posted doesn't seem anywhere near close, but what you're trying to achieve is relatively simple. I would implement like this:

Decide whether you want the change to occur on the button press or the button release. Either is possible, you just need to decide.

Use debouncing and edge detection to detect when the button was pressed (or released). There are various examples showing how to achieve that.

For each press (or release), increment a counter. Use the modulo (%) operator to make the counter wrap back to zero:

counter = (counter + 1) % 4;

Write some code to output the right combination of output states corresponding to the current counter value. One way to achieve that would be using a switch/case statement:

// example only - I haven't checked whether these output states are in the sequence you wanted

switch(counter)
{
  case 0:
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    break;
  case 1:
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    break;
  case 2:
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    break;
  case 3:
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    break;
}

(There are more compact and more elegant ways to code that logic but this is the simplest way imo.)

byte ButtonState;
byte LastState = LOW;
int Pressed = 0;

void setup() {
  pinMode(2, INPUT); //INPUT 1 PUSH BUTTON
  pinMode(5, OUTPUT); //OUTPUT 1
  pinMode(6, OUTPUT); //OUTPUT 2
  digitalWrite(2, HIGH); //SET PULL-UP
}

void loop() {
ButtonState = digitalRead(2);

if(ButtonState == LOW && ButtonState != LastState) { 
//By adding either (ButtonState == HIGH && ButtonState != LastState) or (ButtonState == LOW && ButtonState != LastState) 
// will determine if it looks at ON PRESS or ON RELEASE
  Pressed++;
  if(Pressed > 3) Pressed = 0; // reset pressed count
}
switch(Pressed) {
case 0: 
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  break;

case 1: 
  digitalWrite(5, HIGH);
  digitalWrite(6, LOW);
  break;

case 2:
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  break;

case 3:
  digitalWrite(5, HIGH);
  digitalWrite(6, HIGH);
  break;
 }
LastState = ButtonState; // update state.
}

Not tested