void loop starts again after reaching count limit in a 'for' argument?

Hi guys

I’ve written a program where I want the loop section to repeat a fixed number of times then exit the loop and confirm a message on the LCD once it exits. I’ve obviously messed up somewhere as the count stipulate in the ‘for’ argument increments to its limit but then just restarts the loop and begins counting from zero again, ad-infinitum.

Could someone point out where I’ve gone wrong with this please, if possible?
Thanks very much!

So what I was wanting is the “end loops” message after the count hits 5 and the loop stops, followed by the “finished” message. I get the end loop message but then the whole loop and count starts over.

#include “LiquidCrystal.h”
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );

byte ledPin = 13;

void setup()
{
Serial.begin(9600); // start serial output
Serial.println(“Starting”);

lcd.begin(16, 2); // start the lcd object - specifies the size of it. Must have this!
lcd.setCursor(0,0);
lcd.print(“Starting”);

digitalWrite(ledPin, LOW);
delay(2000);

pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}

void loop()
{
// set up how many loops:
for (int count=0; count <= 5; count++)
{
// begin loop ******************************
lcd.setCursor(0, 1); //move to second line
lcd.print(“Test”);

lcd.setCursor(0,1);
lcd.print(" "); // Blank
lcd.setCursor(0,1);
lcd.print(“Move…”); // Blank
delay(500);

Serial.println(“Wait…”);
Serial.println(500);
Serial.println(“Count…”);
Serial.println(count);

lcd.print(“Wait…”);
lcd.setCursor(8,1);
lcd.print(500);
lcd.print("Count = ");
lcd.setCursor(9,1);
lcd.print(count);
// end loop ********************
}
Serial.println(“End loops”);
lcd.print(“End loops”);
}

void endmsg() { Serial.println(“All finished”);
lcd.print(“All finished”); }

Which is the whole point of loop().

If you don't want loop() to loop, then put a while(1); where you want it to stop.

If you only want something to happen once put it in setup() rather than loop().

There is nothing wrong with leaving loop empty - but it must be there

void loop() {

}

...R

On reflection, I see the problem I think, ie it seems it's ending its own loop that's contained within the for {}, but is still within the void loop and thus begins again.

But.... I don't get how do I get the whole thing to just run once unless I can use the for loop instead of the normal void loop, which I presume would be a nonsense.

I hope that makes some sense...

Thanks guys - Robin, that’s perfect cheers, I didn’t realise I could do it like that.

Brilliant thanks :smiley:

While I'm here I have another issue but it's not arduino code as such but maths/excel help as I'm not sure how to achieve this.

I want to use a pot 0-1023 input value to convert to a camera shutter speed.
So this would have a max 30second exposure for the 1023 value say, and 0.001 for a 1/1000th of a second exposure.
But I'm not sure how to calculate (either manually or with a formula in Excel say) the range of figures between these 2 limits that would correspond to then various input values.

Ideally, a formula would be great since I can then use that in the code to convert the input to a shutter speed rather than having to do something like setting up a table (or whatever).

Thanks!

Cretster:
While I'm here I have another issue but it's not arduino code as such but maths/excel help as I'm not sure how to achieve this.

I want to use a pot 0-1023 input value to convert to a camera shutter speed.
So this would have a max 30second exposure for the 1023 value say, and 0.001 for a 1/1000th of a second exposure.
But I'm not sure how to calculate (either manually or with a formula in Excel say) the range of figures between these 2 limits that would correspond to then various input values.

Ideally, a formula would be great since I can then use that in the code to convert the input to a shutter speed rather than having to do something like setting up a table (or whatever).

Thanks!

int potPosition=analogRead(yourPotPin);
int shutterSpeed=1000/potPosition;

I would also point out that the "void endmsg()" function is never called...

Doc

Cretster:
So this would have a max 30second exposure for the 1023 value say, and 0.001 for a 1/1000th of a second exposure.

I'm assuming you send the raw pot value (which may be between 0 and 1023) to the PC where the subsequent calculations are easier and, more importantly, can be easily adjusted if you change the PC program without needing to change the Arduino program.

I'm guessing that you want to pick from among the standard exposure settings 1/1000 1/500, 1/250, 1/125 etc rather than calculate strange intermediate values such as 1/337.

I would create an array with all the values you want. Suppose there are 20 values. Then you need to convert the number from the potentiometer into a number between 0 and 19 to select the correct value. That can be done with some simple division with the answer converted to an integer. For example 1023 / 53.84 = 19.0007

...R

I want to use a pot 0-1023 input value to convert to a camera shutter speed.
So this would have a max 30second exposure for the 1023 value say, and 0.001 for a 1/1000th of a second exposure.

You can never read “0.001” in a ten bit integer

KeithRB:
Which is the whole point of loop().

There's a clue in the name..... :stuck_out_tongue: