31-12-2017, 20:30
(Ten post był ostatnio modyfikowany: 04-01-2018, 14:25 przez CEO.
Powód edycji: Używamy znaczników [code]!
)
coś znalazlem w sumie to prawie całość tylko jesli zmieniam czas delay na dłuższy to nie włącza sie dioda
Kod:
const byte btnPin = 4,
ledPin = 13,
dbTime = 25; // debounce time
unsigned long timeStart, // debounce timer
cntStart; //button press timer
const int timeOut = 5000; // 5 seconds
bool btn,
oldBtn = false, // previous button state
btnState = true,
timing = false;
byte cnt = 0; // button press counter
void setup()
{
Serial.begin(9600);
pinMode(btnPin,INPUT_PULLUP);
pinMode(ledPin,OUTPUT);
}
void loop()
{
// first, debounce the button +++++++++++++++
if(digitalRead(btnPin) != btnState)
{
timeStart = millis();
btnState ^= 1;
}
if(millis() - timeStart > dbTime)
btn = !btnState;
// end debounce +++++++++++++++++++++++++++++
if(btn != oldBtn && btn == true) // if state changed and button is pressed
{
if(!timing) // if first press, start timer
{
cntStart = millis();
timing = true;
}
if(++cnt > 2)
digitalWrite(ledPin,HIGH); // press reset button to clear
delay(20000);
digitalWrite(ledPin, LOW);
Serial.println(cnt);
}
oldBtn = btn;
if(millis() - cntStart > timeOut)
{
cnt = 0;
timing = false;
cntStart = millis(); // reset counter and timer
}
}