Pages: [1]   Go Down
Author Topic: Looping 15 times  (Read 395 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody,
        Newbie here. How can I make to do something when the program loops 15 times.My program is I am calculating the data, when the data is 15 times runned, i want to reset to 0.
Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
i++;
if(15==i)
{

    i=0;
}
}
Logged

From Idea To Invention

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

or
Code:
void loop()
{
    i = 0
    while(i < 15)
    {
        stuff();
        ++i;
    }
}

or

Code:
void loop()
{
    for(byte i = 0; i < 15; ++i)
    {
         stuff();
    }
}
« Last Edit: December 03, 2012, 10:38:51 pm by Osgeld » Logged


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

ah....sry brother...I cannot understand...let's say if this is my program...How can i Stop at 15th loop(making datastore 0)?I mean I want to continue the loop but I want to reset the datastore to 0.
Code:
float KWH;
      KWH=(float) pow/1000/60;
      Serial.print("Total energy usage for 1 min=");
      Serial.println(KWH,2);
      
    
      
      datastore= datastore + KWH;
      //float finaldata=datastore+KWH;
      Serial.print("DATA=");
      Serial.println(datastore,2);
« Last Edit: December 03, 2012, 10:39:37 pm by Jaay » Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
i = 0
    while(i < 15)
    {
        stuff();
        i++;
    }
Logged

From Idea To Invention

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heres your problem, its going to loop 15 times in a matter of milliseconds, if not microseconds... more info is needed
Logged


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

It's going to loop in a microseconds and it will continue looping..but every 15 times, the data is reset to 0
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop()
{
    for(byte i = 0; i < 15; ++i)
    {
        float KWH;
        KWH=(float) pow/1000/60;
        Serial.print("Total energy usage for 1 min=");
        Serial.println(KWH,2);    
      
        datastore= datastore + KWH;
        //float finaldata=datastore+KWH;
        Serial.print("DATA=");
        Serial.println(datastore,2);
    }
    datastore = 0;
}

its a loop in a loop, above using a for loop (which is what I would do) will go from 0-14 (15 times) exit and start the main loop over again, which starts the for loop again going from 0-14
« Last Edit: December 03, 2012, 10:45:34 pm by Osgeld » Logged


Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
float KWH;
      KWH=(float) pow/1000/60;
      Serial.print("Total energy usage for 1 min=");
      Serial.println(KWH,2);
      
    
      
      datastore= datastore + KWH;
      //float finaldata=datastore+KWH;
      Serial.print("DATA=");
      Serial.println(datastore,2);
      
      if(15==i)
      {
       datastore=0;
       i=0;
      }
      i++;

declare i global
Logged

From Idea To Invention

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

thank you brothers....It works
Logged

Pages: [1]   Go Up
Jump to: