Pages: [1]   Go Down
Author Topic: Help for a ring?  (Read 228 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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' "?
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

int integer1 = 0;

void setup (void)
{
}

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

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot! smiley
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12535
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: