goto command not working

Ok, I know that the goto command is frowned upon in many situations but In this situation it is the only command I can use to my knowledge.
What i’m trying to do is blink and LED and at the same time beep a piezzo buzzer. What I want to happen is if an analog reading from a potentiometer gets too low for the led and buzzer to shut off. I can use the digitalWrite, but afterwards they just turn back on. What i’m using is

int ledpin = 13;
int buzzerpin = 8;
label: label;
void setup() {
Serial.begin(9600);
pinMode(ledpin, OUTPUT);
for(int x=1; x <= 50; x++)
  {
  int sensorValue = analogRead (A0);
  Serial.println(sensorValue);
  delay(1);
  if(sensorValue < 400)
  {
   
   goto label;
  }
    digitalWrite(ledpin, HIGH);
    delay(100);
    digitalWrite(ledpin, LOW);
    tone(8, 29, 1000);
    delay(100);
    Serial.println(x);
  }

for(int i=1; i <= 50; i++)
{
  int sensorValue = analogRead (A0);
  if(sensorValue < 400)
  {
    goto label;
  }
  digitalWrite(ledpin, HIGH);
    delay(50);
    digitalWrite(ledpin, LOW);
    tone(8, 29, 1000);
    delay(50);
    Serial.println(i);
}
for(int i=1; i <= 50; i++)
{
  int sensorValue = analogRead (A0);
  if(sensorValue < 400)
  {
    goto label;
  }
  digitalWrite(ledpin, HIGH);
    delay(30);
    digitalWrite(ledpin, LOW);
    tone(8, 29, 1000);
    delay(30);
    Serial.println(i);
}
for(int i=1; i <= 50; i++)
{
  int sensorValue = analogRead (A0);
  if(sensorValue < 400)
  {
    goto label;
  }
  digitalWrite(ledpin, HIGH);
    delay(10);
    digitalWrite(ledpin, LOW);
    tone(8, 29, 1000);
    delay(10);
    Serial.println(i);
}+
}

void loop() {
label:
digitalWrite(ledpin, LOW);
digitalWrite(buzzerpin, LOW);
}

If there is a better way of doing this than using goto commands please, please, tell me. If not can anyone tell me what I am doing wrong because the Syntax: label in the goto command arduino page is not helpful at all.

Your Boolean logic is entered incorrectly. You need <= not <. When writing in C you have to put the operator and then an =. The IDE should have returned an error on the line when the program was verified.

What you could do is simply use a loop and an if/else statement.

for example:
if(sensor value <= 400)
shut off code here
Else
On code here

That way if the value is less than 400 it will follow the commands immediately after the if statement and skip the commands after the else statement. If it is greater than 400 it will skip the commands immediately after the if statement and start after the else statement.

if (sensorval < 400) {

Is perfectly valid code. <= is less than or equal. < is just less than.

The problem is that you can’t goto a label outside the function like that.

You need to rethink how you’re doing this. Anytime you think you need goto it 99.9999% of the time means you’re doing something very wrong.

I'm rewriting his code, it'll be a few minutes but it should work.

Ok! I got it! It’s much smaller too.
You can change the speed by changing the value of inter

int ledpin = 8;
int buzzpin =13;
int ledstate = LOW;
long premil = 0;
long inter = 100;

void setup() {
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(buzzpin, OUTPUT);
}
  void loop() {
    unsigned long curmil = millis();
    int sensorValue = analogRead(A0);
    Serial.println(sensorValue);
    if(sensorValue <= 400) {
      if(curmil -premil > inter) {
        premil = curmil;
        if(ledstate == LOW) {
          ledstate = HIGH;
        }
        else
        ledstate = LOW;
      }
      else
      ledstate = LOW;

      digitalWrite(ledpin, ledstate);
      digitalWrite(buzzpin, ledstate);
    }
  }

Thank you so much, all I need to do is set a tone() to the buzzer. And if its not a bother, can you explain to me how this code works because i'm a noob.

Since you only have a single note you can forgo the tone command and plug the buzzer right in. If you wanted to play a song with it that’d be another matter.

The code it’s self is fairly simple to understand. All the information needed to write the code is included in the blink without delay tutorial (http://arduino.cc/en/Tutorial/BlinkWithoutDelay), and the analog read tutorial (http://arduino.cc/en/Tutorial/AnalogReadSerial). Here’s the same code with the program notes added:

//This program will flash the LED connected to output pins #8 and 13 at
//1000ms intervals.

//Declare Variables
int ledpin = 8;
int buzzpin =13;
int ledstate = LOW;
//long is the same as int but is used to store large numbers.
long premil = 0;
long inter = 1000;

//Startup Routine
void setup() {
//Starts serial comunication
  Serial.begin(9600);
//Declares pins 8 an 13 as outputs
  pinMode(ledpin, OUTPUT);
  pinMode(buzzpin, OUTPUT);
}

//Begin logic loop
  void loop() {
    
//Reads clock and saves value to curmil.
    unsigned long curmil = millis();
//Records the value of input from an analoge sensor.
    int sensorValue = analogRead(A0);
// Sends the value of sensorValue to monitor via serial comunication.
    Serial.println(sensorValue);


//Boolean logic used to determine and change the pin states.
//First if statment checks to see if the value of sensorValue is less than or
//equal to 400 if so it will follow the commands immeddiately following the
//statement and skip those after the corresponding else statement.
    if(sensorValue <= 400) {
//checks to see if the set time interval has passed, if so it changes the state
//of the pins to high, if not the pin will remain off.
      if(curmil - premil > inter) {
        premil = curmil;
        if(ledstate == LOW) {
          ledstate = HIGH;
        }
        else
        ledstate = LOW;
      }
//If the sensorValue is greater than 400 it will skip the commands between the
//first if statment and the else statment.
      else
      ledstate = LOW;

// Sets the pin states.
      digitalWrite(ledpin, ledstate);
      digitalWrite(buzzpin, ledstate);
    }
  }
//Program returns to the void loop command and continues to run over and
//over indefinately.

Actually, the buzzer is not active so all it makes is a low clicking sound, but I fixed it, no worries, thanks again. XD :slight_smile:
And wow I just realized you took time to explain all of the code to me, thank you so much.

Since the power is only being applied to the buzzer for such a short amount of time it will only make a clicking noise. I didn't realize you wanted it to stay on for a bit. It will mess the timing up but you can try adding a delay command at the end, so it will keep the power on longer. The problem with that though is it will change states irregularly. I can try writing you a new program but it'll have to wait till tomorrow, I kinda over complicated it a bit myself. You can play around with it for a bit and try writing one yourself. Try combining the concepts learned in the blink and analog input tutorials and apply the if/else commands. You order the if else statements like so:

if(condition) { commands; } else commands;

an example condition would be x = 1

Bah who needs sleep… Here, try this. You can change the intervals by changing the delay values.

int ledpin = 8;
int buzzpin= 13;
int state = LOW;
int sensorvalue;

void setup() {
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);
  pinMode(buzzpin, OUTPUT);
}

void loop() {
  sensorvalue = analogRead(A0);
  Serial.println(sensorvalue);
  if(sensorvalue <= 400) {
    state = HIGH;
  }
  else
  state = LOW;
  digitalWrite(ledpin, state);
  digitalWrite(buzzpin, state);
  delay(1000);
  state = LOW;
  digitalWrite(ledpin, state);
  digitalWrite(buzzpin, state);
  delay(1000);
}

It works, thanks.