i have had a look at a few pieces of code on 'toggling' and as i dont understand the use of boolean commands very well i opted to read the button state and use a counter to toggle between outputs, though i'm not sure if its the best way! I have written this piece of code but it doesn't quite work yet. when the button is pressed once the first led comes on and toggles off, the second led doesn't light up. when the first led is on it flickers.
Also the serial monitor only shows up a message when button press is 0 (every two button presses) which suggests that the button presses are being counted ok.
int button = 2;
int val;
int val2;
int state;
int press = 0;
int led1Pin = 12;
int led2Pin = 13;
void setup() {
pinMode(button, INPUT);
Serial.begin(9600);
state = digitalRead(button);
}
void loop(){
val = digitalRead(button);
delay(10); // wait 10 millis to counter debounce
val2 = digitalRead(button); //check again to see if button is pressed
if ((val == val2) && (val != state) && (val == HIGH)) {
press++;
}
if (press >1) {
press = 0;
Serial.print("press number");
Serial.println (press);
}
if (press == 0) {
digitalWrite(led1Pin, HIGH);
delay (100);
digitalWrite (led1Pin, LOW);
}
else {if (press == 1) {
digitalWrite(led2Pin, HIGH);
delay (100);
digitalWrite (led2Pin, LOW);
}
}
state = val;
}
this is what I am trying to do; one momentary push button input to toggle between two outputs. the first button press and release to cause pin 13 HIGH for 100 millis. the second button press to cause pin 12 HIGH for 100 millis. these outputs just need to be single brief pulses to trigger another event.
i tried removing the 0 value declared at the start, also i tried setting it to a number other than 0 or 1 but the same thing still happens. i guess it is because the way i have designed the code there are only two possible states for it to be.
i worked it out eventually... thanks for all the help. Here is the working
/* 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 button = 2; //button attached to pin 2
int val; // reads the button state in loop
int val2; // reads the button state a second time
int state; // reads the button state in setup and stores it could have called this button old
int press = 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 led1Pin = 12; // led output 1
int led2Pin = 13; // led output 2
int button1hit = 0; // secondary code switch stops code running before a button has been pressed
void setup() {
pinMode(button, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
Serial.begin(9600);
state = digitalRead(button); // read button state and store it to reference against later
}
void loop(){
val = digitalRead(button);
delay(10); // wait 10 millis to counter debounce
val2 = digitalRead(button); //check again to see if button is pressed
if ((val == val2) && (val != state) && (val == HIGH)) {
press++; // counts button presses
button1hit = 1; //secondary code switch put in place
}
if (press > 1) {
press = 0; //when button press count passes 1 it resets to zero
Serial.print("press number");
Serial.println (press);
}
if (press == 0 && button1hit == 1) {
digitalWrite(led1Pin, HIGH);
delay (100); // send one pulse
digitalWrite (led1Pin, LOW); // then stop
button1hit = 0; // wait for button to be pressed before doing anything else
}
else {
if (press == 1 && button1hit == 1) {
digitalWrite(led2Pin, HIGH);
delay (100);
digitalWrite (led2Pin, LOW);
button1hit = 0;
}
}
state = val;
}