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);
}
}
Welcome to the forum
robintgyo:
this is our code:
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
See also FAQ - Arduino Forum for general rules on forum behaviour and etiquette.
Hello,
Welcome to the Arduino Forum.
This guide explains how to get the best out of this forum. Please read and follow the instructions below.
Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…
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 (){}
westfw
April 5, 2023, 8:12am
5
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.
xfpd
April 6, 2023, 3:40am
7
cheesecakey:
try
... 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);
}
}
xfpd
April 6, 2023, 3:41am
8
robintgyo:
how do you stop it
Stop moving the tiltswitch (attached to the scooter).
pert
April 11, 2023, 9:29am
11
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 .
system
Closed
October 8, 2023, 11:36am
13
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.