Nested loops

Hi all

I am a complete novice at C++ so please go easy on the flames :cold_sweat:

I want to test if a button has been pressed and if so turn on an LED, just keep looping from that point checking if the button has been pushed.
Within that loop I need another loop to check a sensor and take action on that sensor value.

This whole thing boils down to a laser trip wire that fires a flashgun.
Press the button to "arm" the system and turn on the laser
Leave the laser on and enter another loop monitoring the laser sensor
If the sensor trips, fire the flash and turn the laser off.

My current non-working code looks like this:

int buttonPin = 2;    
int ledPin =  12;     
int sensorPin = 0;
int sensorValue = 0;
int cameraTrigger =  13; 
int buttonState = 0;  

void setup() {
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
  pinMode (cameraTrigger, OUTPUT);
}

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH); 
//start
void loop() {
   sensorValue = analogRead(sensorPin);
   if (sensorValue > 700) {
     digitalWrite (cameraTrigger, LOW);
   }
   else
   {
     digitalWrite (cameraTrigger, HIGH);
     delay(10);
     digitalWrite (cameraTrigger, LOW);
     digitalWrite(ledPin, LOW);
}
//end
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

Where did I go wrong ?

Thanks

I'm not 100% sure, but loop() probably doesn't return. Try using while() and for() to create a loop instead of repeating loop()

Definitely don't do this:

void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH); 
//start
void loop() {

"void loop()" is not a language construct (as you are using it), but a function which is called by the system. Define it once, and the system calls it continuously.

You could probably just remove the following two lines of code, and it may work just fine. And then for bonus points, clean it up with using the Tools > Auto Format command.

//start
void loop() {

Thanks, it does compile fine by removing the two lines as you suggest but as I read the logic I now have to keep the button pressed for the rest of the code to run.
What I want is to use a momentary press to arm and run from there until I press the button again.

Your planned action is a sequence sort of mapped out by way points one after another. There is no need for any nested loops. You can fill in the meat to this skeleton :

void loop()
{
while (key not pressed)
{}

//key must have been pressed so while loop escaped.

turn on laser

while (laser not tripped)
{}

// laser must have been tripped so this second while loop also escaped

do flashy stuff
}

Thanks, that looks good.
I will post the full code once I have it working for reference.

Much appreciate the help.