I have a sketch that need to detect button pressed time but need keep sketch running.
have any idea or provide sketch sample to help me?
for explain:
When I press button less than 1s. the sketch turn on the W_LED.
When I press button more than 2s the sketch turn on the Y_LED, but if press button more 2s and not release button, the sketch also needs turn on the Y_LED.
sketch 1:
I use "while" command, but sketch pending here , until button released.
while (digitalRead(buttonPush) == LOW);
Sketch 2:
If I use "if", the startTime will always be update
if (digitalRead(buttonPush) == LOW)
startTime = millis();
if (digitalRead(buttonPush) == HIGH)
duration_time = millis() - startTime ;
Sketch 3: PluseIN have 1 second waiting time when no button is pressed
duration_time = pulseIn(buttonPush/1000, LOW);
below is a part of my current sketch:
#define whiteLED 3
#define yellowLED 11
#define buttonPush 2
#define boostLED_ON_OFF 8
pinMode(whiteLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(buttonPush, INPUT);
pinMode(boostLED_ON_OFF, INPUT);
digitalWrite (boostLED_ON_OFF, LOW); // default disable boost IC
digitalWrite (buttonPush, HIGH); // internal pull high
analogWrite(whiteLED, 0); //default white LED PWM output =0
analogWrite(yellowLED, 0); //default yellow LED PWM output= 0
Serial.begin(9600);
void loop()
{
if (digitalRead(buttonPush) == LOW)// Read status of tack switch
{
startTime = millis();
while (digitalRead(buttonPush) == LOW); // sketch pending here.
duration_time = millis() - startTime ;
// duration_time = pulseIn(buttonPush/1000, LOW);
delay(10);
//===============Button detect for function select====================
if (duration_time < brightness_level_adj)
{
digitalWrite (boostLED_ON_OFF, HIGH); // ENABLE boost LED IC)
brightness_SEL++;
if (brightness_SEL > 4)
{
analogWrite(whiteLED, brightness_0);
analogWrite(yellowLED, brightness_0);
brightness_SEL = 0;
//color_SEL =1; // reset color to white color after one cycle
digitalWrite (boostLED_ON_OFF, LOW); // turn off boosr IC
}
}
if (duration_time > color_SEL_adj)
{
color_SEL++;
if (color_SEL >= 4)
{
color_SEL = 1;
}
// delay(50);
}
Serial.print(" suration = ");Serial.print(duration_time); Serial.println("ms");
Serial.print(" brightness= "); Serial.println(brightness_SEL);
Serial.print(" color select= "); Serial.println(color_SEL);
delay(50);
//=========================WHITE LED ====================================
if (brightness_SEL == 1 && color_SEL == 1)
{
analogWrite(whiteLED, brightness_25); analogWrite(yellowLED, brightness_0);
}
//==========================YELLOW LED ==============================
if (brightness_SEL == 1 && color_SEL == 2)
{
analogWrite(whiteLED, brightness_0); analogWrite(yellowLED, brightness_25);
}
}
}