|
我想要让按钮按下一次的时候闪烁,但是找了很多法不是只闪一次,不然就是完全不会亮,请教各位大大这个要怎么写才会闪烁
#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);
}
|
|