I am currently working on making some test equipment for µUSB cables. The code is supposed to recoqnise when an error in the cable occurs. Most of it works as expected, but I have some trouble understanding the use of millis() timers. I, therefore want to ask, in the code below, how can I make my program pick up on an missing connection in the cable and make sure it turns on the LED always? And secondly, how can I make it stay on for 2 seconds, even if the cable has "fixed" itself again (i.e. the connection has returned)?
Update It seems that I have misunderstood the assignment. The program should turn on a LED, when an error is present and keeps this on while the error is present (already fixed this in the code). When the connection has been regained a timer should start, which turns off the LED after two seconds. (Here's the trouble I'm having, I can't seem to pinpoint where and how this timer should start)
case microUSB:
if (Vcc_Ext_val > 50)
{
digitalWrite(ActivateLoad, HIGH);
LoadActivated = true;
}
else if (Vcc_Ext_val < 30)
{
digitalWrite(ActivateLoad, LOW);
LoadActivated = false;
}
if ((Vcc_Ext_val < 50) || (USB_DP_VAL < 10)|| (USB_DM_VAL < 10))
{
PulseCurrentMillis = millis();
if (FirstPulse == true)
{
digitalWrite(RedIntLED, StateLEDON);
FirstPulse = false;
}
if (PulseCurrentMillis - PulseLastMillis >= PulseInterval)
{
PulseLastMillis = PulseCurrentMillis;
if (LEDState == StateLEDON)
{
LEDState = StateLEDOFF;
}
else
{
LEDState = StateLEDON;
}
}
digitalWrite(GreenIntLED, StateLEDOFF);
digitalWrite(RedIntLED, LEDState);
}
else
{
digitalWrite(GreenIntLED, StateLEDON);
}
break;