Go Down

Topic: Help for a ring? (Read 244 times) previous topic - next topic

mat4ever

Hi, sorry for my bad english but i'm italian and i can't speak english very well.

My problem is a stupid thing but i'm new in the Arduino world

So, how can i say to the Arduino:"every milliseconds add 1 to 'integer1' "?

Krupski


Hi, sorry for my bad english but i'm italian and i can't speak english very well.

My problem is a stupid thing but i'm new in the Arduino world

So, how can i say to the Arduino:"every milliseconds add 1 to 'integer1' "?


Code: [Select]


int integer1 = 0;

void setup (void)
{
}

void loop (void) // this runs forever
{
    integer1++; // add 1 to "integer1"
    delay(1); // delay 1 millisecond
}
Gentlemen may prefer Blondes, but Real Men prefer Redheads!


PeterH


Code: [Select]

int integer1 = 0;

void setup (void)
{
}

void loop (void) // this runs forever
{
    integer1++; // add 1 to "integer1"
    delay(1); // delay 1 millisecond
}



This works (ish) if you don't need the Arduino to do anything else at all apart from increment the number. If you're going to modify the code to do anything else then you will need to use a more sophisticated approach which is demonstrated in the 'blink without delay' sketch. That approach will enable you to increment the number at the right speed regardless of what else the sketch is doing.

As an aside, all you're doing here is counting the passage of time. The Arduino already provides a millis() function which you can call to find how many milliseconds have elapsed since startup. Quite likely what you're trying to achieve could be done by calling millis() when you want to know the value instead of incrementing your own counter, in which case you don't need any code at all.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up