Understanding the For Loop

Hello again, everyone.

I wrote this code, much like the beginner project code that you can download from this site. I want to understand the for loop a little better though.

Here we have a for loop and it increments to 100. Then it loops over and over again. So my question is this: How does changing the value in the for loop effect the sound of the buzzer? To me it seems that the for loop sound depends just on HIGH, LOW and delay(). How does this variable “i” end up telling the buzzer to make a different sound?
Thanks guys!

int buzzer = 5;

void setup()
{
pinMode(buzzer, OUTPUT);  
}


void loop()
{

  for(unsigned char i = 0; i<100;i++)
  {
  digitalWrite(buzzer, HIGH);
  delay(20);
  digitalWrite(buzzer, LOW);
  delay(20);
  
  }
}

How does this variable "i" end up telling the buzzer to make a different sound?

It doesn't

It doesn't. The code says to turn the buzzer on and then off again. The for loop says to do that 100 times. Then the loop function repeats and it does it 100 more times.

There are different types of “buzzer”.

On sounds when it is connected between GND and 5V. You have no control of the frequency.
The other simply clicks when connected between GND and 5V. You control the frequency by controlling the frequency of the connection to GND and 5V by providing the 5V from a pin going HIGH,

Which type of “buzzer” do you have ?

UKHeliBob:
There are different types of “buzzer”.

On sounds when it is connected between GND and 5V. You have no control of the frequency.
The other simply clicks when connected between GND and 5V. You control the frequency by controlling the frequency of the connection to GND and 5V by providing the 5V from a pin going HIGH,

Which type of “buzzer” do you have ?

Sorry, UKHeliBob, I’m not sure what type of buzzer I have. Maybe I’m not understanding your question. I just connected this to a digital pin #5.

Delta_G:
It doesn’t. The code says to turn the buzzer on and then off again. The for loop says to do that 100 times. Then the loop function repeats and it does it 100 more times.

This is what I find interesting. You can change the value in the for loop from say 50 to 500 and it puts out a different sound when there are two for loops for example . I was thinking that if the for loop just says turn it off and on again x amount of times it wouldn’t matter how many for loops there are. How can this be? THanks guys.

int led = 5;

void setup()
{
pinMode(led, OUTPUT);  
}


void loop()
{

  for(unsigned char i = 0; i<1;i++)
  {
  digitalWrite(led, HIGH);
  delay(1);
  digitalWrite(led, LOW);
  delay(1);
  
  }

  for(unsigned char i = 0; i<500; i++) // changing this value to 50 puts out a different sound
  {
  digitalWrite(led, HIGH);
  delay(2);
  digitalWrite(led, LOW);
  delay(2);
  
  }
  }

Scotchdew: Here we have a for loop and it increments to 100. Then it loops over and over again. So my question is this: How does changing the value in the for loop effect the sound of the buzzer? To me it seems that the for loop sound depends just on HIGH, LOW and delay().

There are two types of buzzers: 1.) passive piezo buzzer 2.) active piezo buzzer

A passive piezo buzzer has to be driven in the audio frequency to create sound. So for a 400 Hz frequency you have to set it 400 times per second HIGH and 400 times per second LOW. This type of buzzer can create different frequencies. The easiest way to do so is to use the Ardduino tone() function.

On the other side active buzzers can just create one single frequency. When setting HIGH they start to play sound in their own frequency, when setting LOW they stop.

Which type of buzzer do you have? Passive or active buzzer?

Each of the two loops generates a different tone because the delays are different. Changing the loop limit values affects how long each tone is on before the other tone is switched to, so obviously it will sound different when changing the limit(s)

With a single for-loop inside loop() then it won't make any difference at all as the tone is never changing. That was your original example.

Thanks for the explanation guys...MarkT that makes sense. Jurs, it is an active buzzer by the way.