本帖最后由 iggy 于 2022-9-6 07:38 编辑
你好,我仿照帖子中的例程写了一个LED频闪程序,目前的问题是:按下并松开按键后,要等正在运行的动作函数运行完毕才能切换到另一个动作函数,写了中断貌似也不起作用,请问如何修改?
- #include "Arduino.h"
- #define LED 0
- #define BUTTON 1
- int state = 0;
- boolean buttonLastState = HIGH;
- void checkState()
- {
- boolean buttonCurrentState = digitalRead(BUTTON);
- if (buttonLastState == LOW && buttonCurrentState == HIGH)
- {
- state++;
- if (state > 1)
- {
- state = 0;
- }
- }
- buttonLastState = buttonCurrentState;
- }
- void turnOff()
- {
- digitalWrite(LED, LOW);
- }
- void SOS()
- {
- // S
- digitalWrite(LED, HIGH);
- delayMicroseconds(200000);
- digitalWrite(LED, LOW);
- delayMicroseconds(200000);
- digitalWrite(LED, HIGH);
- delayMicroseconds(200000);
- digitalWrite(LED, LOW);
- delayMicroseconds(200000);
- digitalWrite(LED, HIGH);
- delayMicroseconds(200000);
- digitalWrite(LED, LOW);
- delayMicroseconds(500000);
- // O
- digitalWrite(LED, HIGH);
- delayMicroseconds(400000);
- digitalWrite(LED, LOW);
- delayMicroseconds(200000);
- digitalWrite(LED, HIGH);
- delayMicroseconds(400000);
- digitalWrite(LED, LOW);
- delayMicroseconds(200000);
- digitalWrite(LED, HIGH);
- delayMicroseconds(400000);
- digitalWrite(LED, LOW);
- delayMicroseconds(500000);
- // S
- digitalWrite(LED, HIGH);
- delayMicroseconds(200000);
- digitalWrite(LED, LOW);
- delayMicroseconds(200000);
- digitalWrite(LED, HIGH);
- delayMicroseconds(200000);
- digitalWrite(LED, LOW);
- delayMicroseconds(200000);
- digitalWrite(LED, HIGH);
- delayMicroseconds(200000);
- digitalWrite(LED, LOW);
- delayMicroseconds(1300000);
- }
- void Strobe()
- {
- digitalWrite(LED, HIGH);
- delayMicroseconds(20000);
- digitalWrite(LED, LOW);
- delayMicroseconds(5000000);
- }
- void setup()
- {
- pinMode(BUTTON, INPUT_PULLUP);
- pinMode(LED, OUTPUT);
- attachInterrupt(digitalPinToInterrupt(BUTTON), checkState, CHANGE);
- }
- void loop()
- {
- checkState();
- switch (state)
- {
- case 0:
- Strobe();
- break;
- case 1:
- SOS();
- break;
- default:
- break;
- }
- }
复制代码
|