|
楼主 |
发表于 2021-9-15 14:52
|
显示全部楼层
谢谢,我拿millis()试了一下,确实状态可以切换了,但是灯不能灭了,请问您能帮我看看吗
- int mode;
- int led;
- unsigned long prevCloes = 0;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(2,OUTPUT);
-
- pinMode(3,OUTPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- led = digitalRead(2);
- if(Serial.available()>0){
- mode = Serial.read();
- mode = mode - 48;
- Serial.flush();
- Serial.println(mode);
-
- if(mode == 0){
- digitalWrite(2,LOW);
- digitalWrite(3,LOW);
- Serial.println("led off");
- }
- if(mode == 1){
- digitalWrite(2,HIGH);
- digitalWrite(3,LOW);
- Serial.println("led on");
- }
- if(mode ==2 && led ==HIGH){
- prevCloes = millis();
- Serial.println(prevCloes);
- digitalWrite(3,HIGH);
- Serial.println("waiting");
- if(millis() - prevCloes > 1000){
- Serial.println("led off");
- digitalWrite(2,LOW);
- digitalWrite(3,LOW);
- }
-
- }
-
-
- }
- }
复制代码 |
|