Buyrun anlamadığınız yeri sorabilirsiniz. Ben test ettim çalışıyor.İsterseniz devre şemasını atabilirim.
const int ledPin = 13;
const int buttonPin = 2;
int buttonState = LOW;
int ledState = 0;
long lastDebounceTime = 0;
long debounceDelay = 100;
int firsttime = 1;
unsigned long startTime;
unsigned long pressTime;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonPin, INPUT);
attachInterrupt(0,DebounceInterrupt,RISING);
}
void loop() {
switch(ledState)
{
case 0:
digitalWrite(ledPin, LOW);
break;
case 1:
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
break;
case 2:
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(2000);
break;
default:
break;
}
}
void DebounceInterrupt() {
if((millis() - lastDebounceTime) > debounceDelay)
{
Pressed();
lastDebounceTime = millis();
}
}
void Pressed(){
buttonState = digitalRead(buttonPin);
if(digitalRead(buttonPin) == HIGH){
if(firsttime == 1){
startTime = millis();
firsttime=0;
}
else if(firsttime == 0)
{
firsttime = 1;
}
pressTime = millis()- startTime;
if(pressTime>=2000){
ledState=2;
}
else if(pressTime<2000){
ledState=1;
}
}
}