Go Down

Topic: Looping 15 times (Read 574 times) previous topic - next topic

Jaay

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

Code: [Select]

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

    i=0;
}
}
From Idea To Invention

Osgeld

#2
Dec 04, 2012, 04:36 am Last 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();
   }
}

http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Jaay

#3
Dec 04, 2012, 04:37 am Last 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

Code: [Select]

i = 0
   while(i < 15)
   {
       stuff();
       i++;
   }
From Idea To Invention

Osgeld

heres your problem, its going to loop 15 times in a matter of milliseconds, if not microseconds... more info is needed
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Jaay

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 am Last 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
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Cybernetician

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
From Idea To Invention

Jaay

thank you brothers....It works

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy