REPEAT UNTILL

i just need to know how to make a repeat untill loop :confused:

#define tot 50
int wt = 1000;
void setup()
{
	pinMode(11, OUTPUT);
	Serial.begin(9600); 
}

while(wt > 50){
	tone(11, 4000, tot);
	delay(wt);
	wt = wt - 10;
	Serial.println(wt);
}
// i want the above to do a repeat untill wt = 50 and then do tone(11, 4000, tot);
//while cant do another string of code after that parameter is false

(and why does this code not work)?

Invert the condition then use...

#define tot 50
int wt = 1000;
void setup()
{
pinMode(11, OUTPUT);
Serial.begin(9600);
}
do
{
tone(11, 4000, tot);
delay(wt);
wt = wt - 10;
Serial.println(wt);
} while (wt > 50);

BOMB_BEEP.ino:8:1: error: expected unqualified-id
do
^
BOMB_BEEP.ino:14:3: error: expected unqualified-id
} while (wt > 50);
^

(getting an error, on codebender)

why does this code not work

One reason would be because the while loop is not in a function.
Do you get any error messages when you upload the code to the Arduino ?

i want the above to do a repeat untill wt = 50 and then do tone(11, 4000, tot);/quote]So why have you got the tone() function inside the while loop rather than after it ? Should it produce the tone once or several times ?

while cant do another string of code after that parameter is false

It would if you had more code after the while loop.

BOMB_BEEP.ino:8:1: error: expected unqualified-id
do
^
 BOMB_BEEP.ino:14:3: error: expected unqualified-id
} while (wt > 50);
  ^

You have code not in a function again.

sorry there was a error in the comments from the original code i want a final tone of 1000ms, not the defined value

#define tot 50
int wt = 1000;
void setup()
{
pinMode(11, OUTPUT);
Serial.begin(9600); 
}
void loop(){
do
{
tone(11, 4000, tot);
delay(wt);
wt = wt - 10;
Serial.println(wt);
} while (wt > 50);
tone(11, 4000, 1000);
}

this works, but without the final 1 second tone

beall619:

#define tot 50

int wt = 1000;
void setup()
{
pinMode(11, OUTPUT);
Serial.begin(9600);
}
void loop(){
do
{
tone(11, 4000, tot);
delay(wt);
wt = wt - 10;
Serial.println(wt);
} while (wt > 50);
tone(11, 4000, 1000);
}




this works, but without the final 1 second tone

What happens when the execution point reaches the end of the loop function?

...
tone(11, 4000, 1000);
//return from loop
//enter loop,  wt ==50, tot == 50
do {
  tone(11, 4000, 50);
  delay(50);
...

this works, but without the final 1 second tone

Don't forget that the loop() function will continue to run when the while loop finishes so the

 tone(11, 4000, 1000);

will run more than once. To prevent this you could move the whole program into the setup() function as that only runs once each time the Arduino is powered up or reset.

990
980
970
960
950
940
930
920
910
900
890
880
870
860
850
840
830
820
810
800
790
780
770
760
750
740
730
720
710
700
690
680
670
660
650
640
630
620
610
600
590
580
570
560
550
540
530
520
510
500
490
480
470
460
450
440
430
420
410
400
390
380
370
360
350
340
330
320
310
300
290
280
270
260
250
240
230
220
210
200
190
180
170
160
150
140
130
120
110
100
90
80
70
60
50
40
30
20
10
0
-10

this is the serial
it doesn’t do a long 1 second beep once “wt” is less than 50

not even that but it doesn’t stop at 50

You're wasting a lot of bandwidth. Please read the two posts by Nick Gammon at the top of this Forum for guidelines on posting here, especially the use of code tags when posting source code. Also, reformat your code using Ctrl-T in the IDE before posting so the code is in a standard C format.

video of

#define tot 50
int wt = 1000;
void setup()
{
	pinMode(11, OUTPUT);
	Serial.begin(9600); 
}
void loop()
{
do
{
	tone(11, 4000, tot);
	delay(wt);
	wt = wt - 10;
	Serial.println(wt);
} while (wt > 50);
tone(11, 4000, 1000);
}

(i misplaced my red led's)

this is the serial
it doesn't do a long 1 second beep once "wt" is less than 50

not even that but it doesn't stop at 50

Not sure why you would expect it to do so since nothing in your code would cause that to happen. Take a look at this and it may help you see what's going on.

#define tot 50
int wt = 1000;

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

void loop() {
  do
  {
    tone(11, 4000, tot);
    delay(wt);
    wt = wt - 10;
    Serial.println(wt);
  } while (wt > 50);

  Serial.print("wt is now: ");
  Serial.println(wt); 
  tone(11, 4000, 1000);
  delay(2000);

  wt = 1000;            // Start all over again...

}