led blink 问题-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1662|回复: 1

[未解决] led blink 问题

[复制链接]
发表于 2021-3-7 22:48 | 显示全部楼层 |阅读模式
我想要让按钮按下一次的时候闪烁,但是找了很多法不是只闪一次,不然就是完全不会亮,请教各位大大这个要怎么写才会闪烁

#define sw 5
#define red 7
#define green 8
#define blue 9

int lastButtonState = HIGH;   
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;   
bool buttonState = HIGH;
byte tapCounter;
int timediff;
bool flag1, flag2;
long double presstime, releasetime;
void setup() {
  Serial.begin(9600);
  pinMode(sw, INPUT_PULLUP);
  pinMode(red, OUTPUT); digitalWrite(red, LOW);
  pinMode(green, OUTPUT); digitalWrite(green, LOW);
  pinMode(blue, OUTPUT); digitalWrite(blue, LOW);


}

void loop() {

  int reading = digitalRead(sw);

  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;
    }
  }

  if (buttonState == 0 && flag2 == 0)
  {
    presstime = millis();
    flag1 = 0;
    flag2 = 1;
    tapCounter++;
    //delay(10);
  }

  if (buttonState == 1 && flag1 == 0)
  {
    releasetime = millis();
    flag1 = 1;
    flag2 = 0;

    timediff = releasetime - presstime;
  }

  if ((millis() - presstime) > 400 && buttonState == 1)
  {
    if (tapCounter == 1)
    {
      if (timediff >= 400)
      {
        Serial.println("Hold");
        hold();
      }
      else
      {
        Serial.println("single tap");
        singleTap();
      }
    }
    else if (tapCounter == 2 )
    {
      if (timediff >= 400)
      {
        Serial.println("single tap and hold");
        tapAndHold();
      }
      else
      {
        Serial.println("double tap");
        doubleTap();
      }
    }
    else if (tapCounter == 3)
    {
      Serial.println("triple tap");
      tripleTap();
    else if (tapCounter == 4)
    {
      Serial.println("four tap");
      fourTap();
    }
    tapCounter = 0;
  }
  lastButtonState = reading;
}
void nolight()
{
  digitalWrite(red, LOW);
  digitalWrite(green, LOW);
  digitalWrite(blue, LOW);
}
case singleTap()
{
  nolight();
  digitalWrite(red,HIGH);
  delay(50);
  digitalWrite(red,LOW);
  delay(1000);
}
void doubleTap()
{
  nolight();
  digitalWrite(green, HIGH);
}
void tripleTap()
{
  nolight();
  digitalWrite(blue, HIGH);
}
void fourTap()
{
  nolight();
  digitalWrite(red, HIGH);
  digitalWrite(blue, HIGH);
}
void hold()
{
  nolight();
  digitalWrite(red, HIGH);
  digitalWrite(green, HIGH);
}
void tapAndHold()
{
  nolight();
  digitalWrite(green, HIGH);
  digitalWrite(blue, HIGH);
}

发表于 2021-3-10 11:15 | 显示全部楼层
如果你只是想要按一下然后就一直闪烁的话,那就在判断按钮按下后加个while(1)让灯一直闪就好。类似:
  1. while (1)
  2.     {
  3.       ledState = !ledState;
  4.       digitalWrite (ledPin, ledState);
  5.       delay(500);
  6.     }
复制代码

要有限次数的闪那就用个for循环自己计数,要退出的话就加个条件break就行。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-29 11:46 , Processed in 0.071762 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表