I want to press the push button once to turn on the dispositives in the board and press it again to turn off but I dont have any idea how to make that, and how to connect it in the board.
I dont want to let him down to turn on and off.
I want to press the push button once to turn on the dispositives in the board and press it again to turn off but I dont have any idea how to make that, and how to connect it in the board.
I dont want to let him down to turn on and off.
See:
And for further reading
Simple toggle code.
//zoomkat LED button toggle test 11-08-2012
int button = 5; //button pin, touch to ground as button
int press = 0;
boolean toggle = true;
void setup()
{
pinMode(13, OUTPUT); //LED on pin 13
pinMode(button, INPUT); //arduino monitor pin state
digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}
void loop()
{
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
digitalWrite(13, HIGH); // set the LED on
toggle = !toggle;
}
else
{
digitalWrite(13, LOW); // set the LED off
toggle = !toggle;
}
}
delay(500); //delay for debounce
}
zoomkat:
Simple toggle code.delay(500); //delay for debounce
That delay is not for debounce, is it? Your code will keep toggling the output each time loop() runs as long as the button is held down because you have not implemented any state change detection for the input, and the delay just stops it from doing that too fast. A better approach IMO would be to take the code from the state change detection example sketch. Every time that code detects a button press, invert the state of the toggle. This will toggle the output once per button press regardless of how long or short the button press is.
toggle = !toggle;
digitalWrite(pin, toggle);
int button = 5; //button pin, touch to ground as button
...
digitalWrite(5, HIGH); //enable pullups to make pin 5 high
Defining a variable to hold the pin number is good. Making it a const would be better. Actually using it would be better still.
I want to press the push button once to turn on the dispositives in the board
To turn on what?