|
单独按一次按钮 启动一组动作 长时间按 启动另一组动作
- const int buttonPin = 11; // the number of the pushbutton pin
- const int ledPin = 13; // the number of the LED pin
- int ledState = HIGH; // the current state of the output pin
- int buttonState; // the current reading from the input pin
- int lastButtonState = HIGH; // the previous reading from the input pin
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 50; // the debounce time; increase if the output flickers
- long debounceDelay2 = 1050; // the debounce time; increase if the output flickers
- int ttt;
- void setup() {
- pinMode(buttonPin, INPUT_PULLUP);
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, ledState);
- Serial.begin(9600);
- }
- void loop() {
- int reading = digitalRead(buttonPin);//读取按键电平高低
- if (reading != lastButtonState) { //如果电瓶如之前翻转了.说明按键状态改变.
- lastDebounceTime = millis(); //记录翻转时候的时间
- // Serial.println("1111");
- }
-
- if ((millis() - lastDebounceTime) > debounceDelay)
- {
-
- if (reading != buttonState) {
- buttonState = reading;
- if (buttonState == LOW) {
- ledState = !ledState;
- ttt++;
- Serial.println(ttt);
- }
- }
- }
- if ((millis() - lastDebounceTime) > debounceDelay2)
- {
- if (reading == LOW) {
- Serial.println(ttt);
- }
- }
-
- digitalWrite(ledPin, ledState);
- lastButtonState = reading;
- }
复制代码 |
|