Step 7The display code
The display() function works by checking if the hours variable can be divided by 16, if it can, it will turn on the LED representing 16, else turn it off. Then try dividing the remainder of the previous division with 8, turn the 8-led on if it could be divided, else off, divide the remainder of that division with 4 and so on.The same process is then repeated for minutes but starting at 32 instead of 16.
display() uses the hours_p and minutes_p pointers, that points to either the variables hours and minutes, or to the variables alarm_hours and alarm_minutes. What they are pointing to is decided in the settings switch code (in the buttons() function). So when the switch is turned to A (alarm), the display will automatically show the alarm time, and when it's turned back to T (time) the current time will be shown.
____________________________________________________________________
The display() function:
// "INTERNAL" VARIABLES FOR DISPLAY FUNCTION:
int remainder;
int led_value;
void display()
{
// display() will display the ordinary time or the alarm time,
// depending on what hours_p points to, this is decided
// by the settings_switch_state, in the buttons() function
// Display hours:
remainder = *hours_p;
for(int i = 0; i < 5; i++) // repeat four all five hour-LEDs
{
led_value = 16/round(pow(2,i)); // first LED = 16, second = 8, third = 4 etc.
if(remainder/led_value == 1)
digitalWrite(hour_pins[i], HIGH);
else
digitalWrite(hour_pins[i], LOW);
// the remainder of the hours is saved for
// the next LED that is displaying a lower value
remainder=remainder%led_value;
}
// Display minutes:
remainder = *minutes_p;
for(int i = 0; i < 6; i++) // repeat for all six minute-LEDs
{
led_value = 32/round(pow(2,i)); // first 32, then 16, then 8 etc.
if(remainder/led_value == 1)
digitalWrite(minute_pins[i], HIGH);
else
digitalWrite(minute_pins[i], LOW);
// the remainder of the minutes is saved for
// the next LED that is displaying a lower value
remainder=remainder%led_value;
}
}
____________________________________________________________________
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|













































