Go Down

### Topic: Looping 15 times (Read 1 time)previous topic - next topic

#### htetmyetaung1993

##### Dec 04, 2012, 04:31 am
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.

#### Cybernetician

#1
##### Dec 04, 2012, 04:34 am
Code: [Select]
`void loop(){i++;if(15==i){    i=0;}}`

#### Osgeld

#2
##### Dec 04, 2012, 04:36 amLast Edit: Dec 04, 2012, 04:38 am by Osgeld Reason: 1
or
Code: [Select]
`void loop(){    i = 0    while(i < 15)    {        stuff();        ++i;    }}`

or

Code: [Select]
`void loop(){    for(byte i = 0; i < 15; ++i)    {         stuff();    }}`

#### htetmyetaung1993

#3
##### Dec 04, 2012, 04:37 amLast Edit: Dec 04, 2012, 04:39 am by Jaay Reason: 1
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: [Select]
` 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); `

#### Cybernetician

#4
##### Dec 04, 2012, 04:38 am
Code: [Select]
`i = 0    while(i < 15)    {        stuff();        i++;    }`

#### Osgeld

#5
##### Dec 04, 2012, 04:39 am
heres your problem, its going to loop 15 times in a matter of milliseconds, if not microseconds... more info is needed

#### htetmyetaung1993

#6
##### Dec 04, 2012, 04:40 am
It's going to loop in a microseconds and it will continue looping..but every 15 times, the data is reset to 0

#### Osgeld

#7
##### Dec 04, 2012, 04:41 amLast Edit: Dec 04, 2012, 04:45 am by Osgeld Reason: 1
Code: [Select]
`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

#### Cybernetician

#8
##### Dec 04, 2012, 04:44 am
Code: [Select]
`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

#### htetmyetaung1993

#9
##### Dec 04, 2012, 04:48 am
thank you brothers....It works

Go Up