I am a complete novice at C++ so please go easy on the flames
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);
 }
}
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.
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