March 24, 2011 by hariharan i wanted to use the for() command to increment the variable int i; ``````for(i=0; i<=59; i++) { } `````` I want the variable "i" to increment with a particular time interval. how do i do it? The best answer really depends on why you want to do that. I could tell you to do ``````for (i = 0; i < 60; i++) { delay_ms(1000); } `````` but that might not be the best way, depending on your application. Well here is a unqualified (non programmer) answer. I am sure others will do better but here is a literal answer: ``````for(i=0; i<=59; i++) { delay_ms(500); LCD write i; } `````` I believe that will work, of course the LCD write is sudo. Others will jump all over me if I am incorrect. Ralph i want to do this so i can show time on my lcd will this code work for a clock? ``````#define F_CPU 14745600 #include #define F_CPU 14745600 #include #include #include #include #include "../libnerdkits/delay.h" #include "../libnerdkits/lcd.h" int main(){ // variable seconds int8_t s; // variable Minutes int8_t m; // variable hrs int8_t h; while (1){ // added 1 since it will count from 0,1,2.... // I want it to count from 1,2,3.....60 for 1+(s = 0; s < 59; s++) { // delaying for 1000 milli sec delay_ms(1000); } //same here for 1+(m = 0; m < 59; m++) { delay_ms (60000) ; } for 1+(h = 0; h < 24; h++) { delay_ms (3600000); } //send it to lcd printf_p(PSTR("time is %d : %d : %d ", h, m, s)); } return 0; } `````` Hi Hari, Ralph gave you some great pointers up above, I suggest you take a close look at what he posted for you. The place where you are adding 1 does not really make sense. If you want a for loop to start from 1, simply make it start there by using the initialization parameter. The loop ``````int i; for(i=1;i<100;i++){ // do something } `````` would start at 1 and keep looping while i is less than 100. Your code would also not do what you really expect it to because of the way you laid out the for loops. Your first for loop will actually delay you one second, but then the next one will wait for full minute before the loop exists, and the next one would wait a full hour, the last one would not even really work because you would overflow the capacity of the delay function. It is a very good attempt at coding, and you should definitely take a second to understand why the way you laid out the flow does not work. I highly recommend you take a look at our crystal real time clock tutorial for an idea of how we kept track of "real time" Humberto Thanks Humberto! But i do not understand the code written forn the crystal time clock I do not understand how u set up the interrupt. hariharan, start a new thread "How do you setup a interrupt". We have not had a detailed discussion on interrupts for a while (there are lots of discussions in the forum). It might be good to get a consolidated starting from scratch discussion going. Start with what you know, do some searching and make a attempt then post what you have. Ralph