I am trying to figure out why my led light is not blinking. I have another function called Wakeablewait that should count about 1 second with the led either on or off. If the button is clicked before the timer erupts it return false (although at a further point it will switch to another function). The caution led controls the color outputs. Not sure what I am missing. In the log statements it shows that I am entering both functions over and over but the led light just stays yellow (I use two leds to make the yellow color by the way). Anyone have a clue what I am missing?
Button pressed equates to 0 on the microcontroller, and 1 when not.
//This time spec is in nano seconds but equates to 10ms
const struct timespec sleepTime = { 0, 10000000 };
GPIO_Value_Type previous_button_value = BUTTON_UNPRESSED;
bool WakeableWait(int btnA, int milliseconds) {
Log_Debug("In Wake\n");
while (milliseconds > 0) {
// Return true if full time elapsed, false if button was pressed
GPIO_Value_Type value;
GPIO_GetValue(btnA, &value);
bool button_got_pressed = (previous_button_value == BUTTON_UNPRESSED) && (value == BUTTON_PRESSED);
previous_button_value = value;
if (button_got_pressed) {
return false;
}
nanosleep(&sleepTime, NULL);
milliseconds -= 10;
return true;
}
}
// This function should blink Yellow until button A is pressed to switch to typical working traffic light
void caution_light(int y1, int y2, int btnA) {
//// This should blink on and off for one second
while (true) {
// Turn on Yellow
GPIO_SetValue(y1, GPIO_Value_Low);
GPIO_SetValue(y2, GPIO_Value_Low);
// Switch to other function to wait time
Log_Debug("Turning On\n");
WakeableWait(btnA,1000);
//Turn Led off
GPIO_SetValue(y1, GPIO_Value_High);
GPIO_SetValue(y2, GPIO_Value_High);
// Leave to other function
Log_Debug("Turning Off\n");
WakeableWait(btnA, 1000);
}
}
Image of output in Visual Studio:
Thank you.
WakeableWait
does not loop. No matter what, it only does one iteration, and returns immediately: the return true;
statement is a part of the loop body. Your LEDs are likely blinking at 100 Hz (every 10 milliseconds), and your eyes cannot detect it (what you should see is that the LEDs are dimmer than normal). The solution is to pull return true;
out of the loop.
WakeableWait(....)
{
while (milliseconds > 0) {
....
}
return true;
}