Buzzer Program (beginner kit)

I don't quite understand this code for the buzzer. For example, I don't understand the while loop. While(1)?? While what is 1? delay? it can't be delay because I changed delays value to various different things and it still outputs a frequency. Also, the unsigned char "j" I don't see being used anywhere in the program. Help please! Thanks! :smiley:

int buzzer = 12;//the pin of the active buzzer
void setup()
{
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop()
{
unsigned char i,j;
while(1)
{
//output an frequency
for(i=0;i<50;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
}
//output another frequency
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
}
}

For example, I don't understand the while loop.

It's there because some idiot didn't realize that loop() looped.

While(1)?? While what is 1?

When you have a statement like while (i > 14), the comparison of i to 14 yields either true (i is greater than 14) or false (i is 13 or less). You could, therefore do

bool bigger = i > 14;
while(bigger)
{
}

So, a single value in the parentheses is not unusual. In this case, 1 is true, so the while statement will run until 1 is false. Since 1 will never be false, the loop will run forever.

it can't be delay because I changed delays value to various different things and it still outputs a frequency.

Not the same frequency, though, or you have something wired wrong.

Also, the unsigned char "j" I don't see being used anywhere in the program.

Oh no, the world will now come to an end. Yeah, it's stupid, but don't panic over it. There are other things wrong with that program that are more important.

Scotchdew:
I don't quite understand this code for the buzzer. For example, I don't understand the while loop. While(1)?? While what is 1? delay? it can't be delay because I changed delays value to various different things and it still outputs a frequency. Also, the unsigned char "j" I don't see being used anywhere in the program. Help please! Thanks! :smiley:

int buzzer = 12;//the pin of the active buzzer
void setup()
{
pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}
void loop()
{
unsigned char i,j;
while(1)
{
//output an frequency
for(i=0;i<50;i++)
{
digitalWrite(buzzer,HIGH);
delay(1);//wait for 1ms
digitalWrite(buzzer,LOW);
delay(1);//wait for 1ms
}
//output another frequency
for(i=0;i<100;i++)
{
digitalWrite(buzzer,HIGH);
delay(2);//wait for 2ms
digitalWrite(buzzer,LOW);
delay(2);//wait for 2ms
}
}
}

Use the </> Button to wrap the proper CODES around your sketch.

The "While(1);" means Loop Forever
Correct, it is redundant.

Correct, variable "j" is not used.

Did you try changing the "DELAY(1)" and "DELAY(2)" function calls
to "DELAY(10)" and "DELAY(20)" ?

Do you have a BUZZER connected to Pin 12?
A Buzzer has its own "frequency".
This code just turns that Buzzer On/Off per the DELAY(#) intervals.

lol PaulS thanks for the refreshing attitude in your reply (not being sarcastic :P). Also, thanks for the speedy reply guys. I really appreciate it. I was sitting, looking at this code (i'm new to programming obviously) and thinking it wasn't making any sense. Glad to know it's not just me.

mrsummitville I did change the frequency to a 10, 20, even in the thousands if I remember correctly (sounded like a smoke alarm).

Also, PaulS, you said that the while loop with the single value, 1 in this case, is true. Is that relative to delay(1)?

It's not related to anything. Only 0 is the same as false. Any other value is true.

the while loop with the single value, 1 in this case, is true. Is that relative to delay(1)?

No

while (1){  // loop forever
               //code here is run repeatedly 
         }
delay(1);  //do nothing for 1 millisecond then carry on to the next command

Thanks guys!!