เอา Code ตัวอย่าง Button ใน Arduino sample มา แก้
เมื่อกด Pin 11 จะ เป็น toogle on / off
const int buttonPin = 11; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
bool LastButtonState =0; // Keep Previous state
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
bool readState = digitalRead(buttonPin);
if(LastButtonState!=readState &&readState==LOW) // once press active
{
buttonState = !buttonState; // switch 0 or 1
}
LastButtonState=readState ; // keep to next compare
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น