Stop loop, help us?

hello, we have a very mean teacher that does not want to help us. how do you stop a loop in arduino? we made a alarm for a scooter but how do you stop it?
this is our code:
const int tiltPin = 5;
const int piezoPin = 3;
const int toonLaag = 1000;
const int toonHoog = 2000;
const int tijdsinterval = 20;
const int lengte = 100;
const int PIN_RED = 8;
const int PIN_GREEN = 9;
const int PIN_BLUE = 10;

void setup()
{
pinMode(tiltPin, INPUT);
pinMode(piezoPin, OUTPUT);
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
}

void loop (){
int switchstate = digitalRead(tiltPin);
if (switchstate == LOW){
for (int i = 0; i < lengte; i++){
for (int toon=toonHoog; toon >= toonLaag; toon--){
tone(piezoPin, toon);
delayMicroseconds(tijdsinterval);

digitalWrite(PIN_RED, HIGH);
digitalWrite(PIN_GREEN, LOW);
digitalWrite(PIN_BLUE, LOW);
delay(500);
digitalWrite(PIN_RED, LOW);
digitalWrite(PIN_GREEN, LOW);
digitalWrite(PIN_BLUE, HIGH);
delay(500);
}
}
}
else{
noTone(piezoPin);
}
}

help

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

try:

const int tiltPin = 5;
const int piezoPin = 3;
const int toonLaag = 1000;
const int toonHoog = 2000;
const int tijdsinterval = 20;
const int lengte = 100;
const int PIN_RED = 8;
const int PIN_GREEN = 9;
const int PIN_BLUE = 10;

void setup()
{
pinMode(tiltPin, INPUT);
pinMode(piezoPin, OUTPUT);
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
Alarm();
}

void Alarm () {
int switchstate = digitalRead(tiltPin);
if (switchstate == LOW){
for (int i = 0; i < lengte; i++){
for (int toon=toonHoog; toon >= toonLaag; toon--){
tone(piezoPin, toon);
delayMicroseconds(tijdsinterval);

digitalWrite(PIN_RED, HIGH);
digitalWrite(PIN_GREEN, LOW);
digitalWrite(PIN_BLUE, LOW);
delay(500);
digitalWrite(PIN_RED, LOW);
digitalWrite(PIN_GREEN, LOW);
digitalWrite(PIN_BLUE, HIGH);
delay(500);
}
}
}
else{
noTone(piezoPin);
}
}
void loop (){}

You don't stop a loop; the arduino cpu is ALWAYS doing something. If you want it to stop doing something, send it into a new loop that does nothing:

   if (time_to_stop) {
      while (1) {  // do forever
         // nothing.
      }
   }

Wait until all blocking function calls have been executed.

... almost... : )

const int tiltPin = 5;
const int piezoPin = 3;
const int toonLaag = 1000;
const int toonHoog = 2000;
const int tijdsinterval = 20;
const int lengte = 100;
const int PIN_RED = 8;
const int PIN_GREEN = 9;
const int PIN_BLUE = 10;

void setup() {
  pinMode(tiltPin, INPUT);
  pinMode(piezoPin, OUTPUT);
  pinMode(PIN_RED, OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE, OUTPUT);
}

void loop() {
  int switchstate = digitalRead(tiltPin);
  if (switchstate == LOW) {
    for (int i = 0; i < lengte; i++) {
      for (int toon = toonHoog; toon >= toonLaag; toon--) {
        tone(piezoPin, toon);
        delayMicroseconds(tijdsinterval);

        digitalWrite(PIN_RED, HIGH);
        digitalWrite(PIN_GREEN, LOW);
        digitalWrite(PIN_BLUE, LOW);
        delay(500);
        digitalWrite(PIN_RED, LOW);
        digitalWrite(PIN_GREEN, LOW);
        digitalWrite(PIN_BLUE, HIGH);
        delay(500);
      }
    }
  } else {
    noTone(piezoPin);
  }
}

Stop moving the tiltswitch (attached to the scooter).

It's called teaching. :slight_smile:

shut up

Due to their behavior, @robintgyohas received a temporary suspension from the forum.

@robintgyo, I hope that when you return to the forum you'll be more respectful of our community.

Thanks in advance.

Sorry if I annoyed @robintgyohas.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.