Hi all,
Im trying to write some code that will count button pressing time for below code,
First state will start after 3 second pressing to button, and other states immediately.
How can I count pressing time 3 second or more?
Thanks in advance.
const int MainLed = 3;
const int button = 2;
boolean previousBtnState = LOW;
boolean nowBtnState = LOW;
int ledMode = 0;
void setup()
{
pinMode(MainLed, OUTPUT);
pinMode(button, INPUT);
}
boolean buttonControl(boolean state)
{
boolean nowBtn = digitalRead(button);
if (state != nowBtn)
{
delay(5);
nowBtn = digitalRead(button);
}
return nowBtn;
}
void ledSet(int state)
{
if (state == 1)
{
digitalWrite(MainLed, HIGH);
}
else if (state == 2)
{
analogWrite(MainLed, 127);
}
else if (state == 3)
{
digitalWrite(MainLed, HIGH);
delay (500);
digitalWrite(MainLed, LOW);
delay (500);
}
}
void loop()
{
nowBtnState = buttonControl(previousBtnState);
if (previousBtnState == LOW && nowBtnState == HIGH)
{
ledMode++;
}
previousBtnState = nowBtnState;
if (ledMode == 4)
ledMode = 0;
ledSet(ledMode);
}