NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » For command

March 24, 2011
by hariharan
hariharan's Avatar

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?

March 24, 2011
by bretm
bretm's Avatar

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.

March 24, 2011
by Ralphxyz
Ralphxyz's Avatar

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

March 24, 2011
by hariharan
hariharan's Avatar

i want to do this so i can show time on my lcd

March 24, 2011
by hariharan
hariharan's Avatar

will this code work for a clock?

#define F_CPU 14745600

#include <stdio.h>

#define F_CPU 14745600
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#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;
}
March 25, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

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

March 26, 2011
by hariharan
hariharan's Avatar

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.

March 26, 2011
by Ralphxyz
Ralphxyz's Avatar

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

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...