How to control the switch to light a small bulb in the specific press order?
can led toggle on , off each press of switch? is, first time press switch, led turns on, second time press switch turns off, , on?
ps. read debounce example, confused it. should use debounce function deal this, think not time interval.
ps. read debounce example, confused it. should use debounce function deal this, think not time interval.
code: [select]
void setup() { pinmode(led_builtin, output);
pinmode(2, input); }
void loop() {
int switchvalue = digitalread(2);
if (switchvalue == high) {
digitalwrite(led_builtin, high); }
else {
digitalwrite(led_builtin, low); }
delay(10); }
the state change detection example is, think, looking for. in ide go file, examples, digital. using can toggle led each time button becomes pressed (as opposed pressed).
i modified state change detection example show how toggle on-board led push buttonn switch. changed switch wiring wired ground , internal pullup. see comments in code.
i modified state change detection example show how toggle on-board led push buttonn switch. changed switch wiring wired ground , internal pullup. see comments in code.
code: [select]
/*
state change detection (edge detection)
often, don't need know state of digital input time,
need know when input changes 1 state another.
example, want know when button goes off on. called
state change detection, or edge detection.
example shows how detect when button or button changes off on
, on off. no debounce required case.
circuit:
pushbutton attached pin 2 ground
internal pullup resistor on pin 2 enabled
led attached pin 13 ground (or use built-in led on
most arduino boards)
created 27 sep 2005
modified 30 aug 2011
tom igoe
example code in public domain.
http://www.arduino.cc/en/tutorial/buttonstatechange
*/
// constant won't change:
const int buttonpin = 2; // pin pushbutton attached to
const int ledpin = 13; // pin led attached to
// variables change:
int buttonstate = 0; // current state of button
int lastbuttonstate = 0; // previous state of button
void setup()
{
// initialize button pin input internal pullup enabled
pinmode(buttonpin, input_pullup);
// initialize led output:
pinmode(ledpin, output);
// initialize serial communication:
serial.begin(9600);
}
void loop()
{
// read pushbutton input pin:
buttonstate = digitalread(buttonpin);
// compare buttonstate previous state
if (buttonstate != lastbuttonstate)
{
// if state has changed, increment counter
if (buttonstate == low)
{
// if current state low button
// went off on:
serial.println("on");
digitalwrite(ledpin, !digitalread(ledpin)); // toggle led
}
else
{
// if current state low button
// wend on off:
serial.println("off");
}
// delay little bit avoid bouncing
delay(50);
}
// save current state last state,
//for next time through loop
lastbuttonstate = buttonstate;
}
Arduino Forum > Using Arduino > Programming Questions > How to control the switch to light a small bulb in the specific press order?
arduino
Comments
Post a Comment