i have written a piece of code using one button as an input toggling between two outputs, it seems to work ok. here is the
/* button toggle - use one button to toggle between two outputs on different pins
momentary push button attached to pin 2
leds connected to pins 12 & 13
*/
int buttonB = 2; //button attached to pin 2
int valB; // reads the button state in loop
int val2B; // reads the button state a second time
int stateB; // reads the button state in setup and stores it could have called this button old
int pressB = 1; // press counter, i set this to 1 so when the first button is pressed it will give a value of 0 activating the first pin
int startB = 12; // led output 1
int stopB = 11; // led output 2
int button1hitB = 0; // secondary code switch stops code running before a button has been pressed
void setup() {
pinMode(buttonB, INPUT);
pinMode(startB, OUTPUT);
pinMode(stopB, OUTPUT);
Serial.begin(9600);
stateB = digitalRead(buttonB); // read button state and store it to reference against later
}
void loop(){
valB = digitalRead(buttonB);
delay(20); // wait 10 millis to counter debounce
val2B = digitalRead(buttonB); //check again to see if button is pressed
if ((valB == val2B) && (valB != stateB) && (valB == HIGH)) {
pressB++; // counts button presses
button1hitB = 1; //secondary code switch put in place
}
if (pressB > 1) {
pressB = 0; //when button press count passes 1 it resets to zero
Serial.print("press numberB");
Serial.println (pressB);
}
if (pressB == 0 && button1hitB == 1) {
digitalWrite(startB, HIGH);
delay (100); // send one pulse
digitalWrite (startB, LOW); // then stop
button1hitB = 0; // wait for button to be pressed before doing anything else
}
else {
if (pressB == 1 && button1hitB == 1) {
digitalWrite(stopB, HIGH);
delay (100);
digitalWrite (stopB, LOW);
button1hitB = 0;
}
}
stateB = valB;
}
i wanted to add another three buttons with the same function to the code. i started by just adding one button in first to see if i could get that to work. i duplicated the code for one button and cut and pasted it in, the result is this
/* button toggle - use one button to toggle between two outputs on different pins
momentary push button attached to pin 2
leds connected to pins 12 & 13
*/
int buttonA = 2; //button attached to pin 2
int startA = 12; // led output 1 starts palying mp3
int stopA = 11; // led output 2 stops playing mp3
int valA; // reads the button state in loop
int val2A; // reads the button state a second time
int stateA; // reads the button state in setup and stores it could have called this button old
int pressA = 0; // press counter, i set this to 1 so when the first button is pressed it will give a value of 0 activating the first pin
int button1hitA = 0; // secondary code switch stops code running before a button has been pressed
int buttonB = 10; //button attached to pin 2
int startB = 9; // led output 1 starts palying mp3
int stopB = 8; // led output 2 stops playing mp3
int valB; // reads the button state in loop
int val2B; // reads the button state a second time
int stateB; // reads the button state in setup and stores it
int pressB = 0; // press counter, i set this to 1 so when the first button is pressed it will give a value of 0 activating the first pin
int button1hitB = 0; // secondary code switch stops code running before a button has been pressed
void setup() {
pinMode(buttonA, INPUT);
pinMode(startA, OUTPUT);
pinMode(stopA, OUTPUT);
Serial.begin(9600);
stateA = digitalRead(buttonA); // read button state and store it to reference against later
pinMode(buttonB, INPUT);
pinMode(startB, OUTPUT);
pinMode(stopB, OUTPUT);
stateB = digitalRead(buttonB); // read button state and store it to reference against later
}
void loop(){
valA = digitalRead(buttonA);
delay(20); // wait 10 millis to counter debounce
val2A = digitalRead(buttonA); //check again to see if button is pressed
valB = digitalRead(buttonB);
delay(20); // wait 10 millis to counter debounce
val2B = digitalRead(buttonB); //check again to see if button is pressed
if ((valA == val2A) && (valA != stateA) && (valA == HIGH)) {
pressA++; // counts button presses
button1hitA = 1; //secondary code switch put in place
}
if ((valB == val2B) && (valB != stateB) && (valB == HIGH)) {
pressB++; // counts button presses
button1hitB = 1; //secondary code switch put in place
}
if (pressA > 1) {
pressA = 0; //when button press count passes 1 it resets to zero
Serial.print("press numberA");
Serial.println (pressA);
}
if (pressB > 1) {
pressB = 0; //when button press count passes 1 it resets to zero
Serial.print("press numberB");
Serial.println (pressB);
}
if (pressA == 0 && button1hitA == 1) {
digitalWrite(startA, HIGH);
delay (100); // send one pulse
digitalWrite (startA, LOW); // then stop
button1hitA = 0; // wait for button to be pressed before doing anything else
}
if (pressB == 0 && button1hitB == 1) {
digitalWrite(startB, HIGH);
delay (100); // send one pulse
digitalWrite (startB, LOW); // then stop
button1hitB = 0; // wait for button to be pressed before doing anything else
}
else {
if (pressA == 1 && button1hitA == 1) {
digitalWrite(stopA, HIGH);
delay (100);
digitalWrite (stopA, LOW);
button1hitA = 0;
}
if (pressB == 1 && button1hitB == 1) {
digitalWrite(stopB, HIGH);
delay (100);
digitalWrite (stopB, LOW);
button1hitB = 0;
}
stateA = valA;
stateB = valB;
}
}
the first button toggle its output the same as it did before; one press one output another press the other output. The second button does something different; one button press and it goes through both outputs once, another press and it does the same thing.
does anyone have a suggestion of how i can change the structure of the code to allow me to add more inputs doing the same thing without distorting the original function? im sure it's probably simple i just cant see how to do it! cheers for any help.