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.
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.
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.)