I'm trying to make a timer for an LCD screen. I don't normally program because it isn't related to my degree but I thought I would do it for fun. I am using a built in library designed by a friend.
I have designed the if loop below, just some quick info on my loop. The LCD_cursor
statement moves the cursor around the screen so I can print the letter in the correct place. The LCD_display_value
function is used to print the value of the integer.
The problem that I have is, it prints everything fine until it gets to time2
, then it just skips the 10 digits and carries on as normal. It works at finishing the loop at the right point.
time1 = 1; time2 = 4; time3 = 4; finish = 3;
while (finish != 1) {
if (time1 == 0 && time2 == 0 && time3 == 0)
finish = 1;
if (time1 != 0)
time1 = time1 - 1;
else {time2 = time2 - 1;
time1 = 9;}
if (time2 == 0){
LCD_cursor(14,0);
LCD_display_value(time2);
Delay_ms(200);
if (time3 != 0){
time2 = 5;
time3 = time3 - 1;
time1 = 9;
}
}
LCD_cursor(15,0);
LCD_display_value(time1);
LCD_cursor(14,0);
LCD_display_value(time2);
LCD_cursor(13,0);
LCD_putch(':');
LCD_cursor(12,0);
LCD_display_value(time3);
delay_ms(200);
}
Change
if (time2 == 0)
to
if (time2 == 0 && time1==0)
Rearranging your code blocks you also can fix your exit condition:
#include <stdio.h>
int main(){
int time1 = 1;
int time2 = 4;
int time3 = 4;
while (time1 || time2 || time3) {
if (time1 != 0)
time1 = time1 - 1;
else {
time2 = time2 - 1;
time1 = 9;
}
printf("%d:%d%d\n", time3, time2, time1);
if (time2 == 0 && time1 == 0){
if (time3 != 0){
time2 = 5;
time3 = time3 - 1;
time1 = 9;
printf("inner %d:%d%d\n", time3, time2, time1);
}
}
}
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.