Not sure why im getting such a huge delay.

Hi I’m trying to set a buzzer to buzz… I loaded this program

 void setup() {
  pinMode(5, OUTPUT); // set a pin for buzzer output
}

void loop() {
  buzz(5, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
  delay(1000); // wait a bit between buzzes
}


void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to 
  //// get the total number of cycles to produce
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}

and it works fine, but when I transplanted it to another program, the delay between buzzes get prolonged… why?

here is the transplanted code.

#define fill 155 //the position in degrees to fill the hopper
#define empty 20 //the position in degrees to empty the hopper
#define potPin A0 //this is the pin (must be analog) that the middle of the potentiometer is connected to

#include <Servo.h> //this is a library used to control a servo (for more information see www.arduino.cc for more info)
 
//variables
Servo mainServo; //declare the main servo
int trigger = 0; //this is used to switch between fill and empty
int potIn = 0; //this is the data read from pin A0 (the potPin)
int count = 1; //used as a time muliplier


void setup()
{
//basic setup
mainServo.attach(3); //tell arduino which pin the servo is on (the white wire from the servo)
//set the pin modes
pinMode(4, OUTPUT); //used to output 5V or High to the potentiometer
pinMode(5, OUTPUT); // set a pin for buzzer output
pinMode(10, OUTPUT); //used for the led
pinMode(11, OUTPUT); //used for the led

digitalWrite(4, HIGH); //set pin 4 high
digitalWrite(10, LOW); //set pin 10 low
}


void loop() {
{
buzz(5, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
  delay(1000); // wait a bit between buzzes  
}  
potIn = analogRead(potPin); //read the position the potentiometer is at
//if the trigger value is 0 fill the hopper
if(!trigger)
{
mainServo.write(fill);//move servo to fill position
//this is used to setup the delay
//count = 171 //uncomment this to set the max delay to 3 hours
//the delay below is calculated using potin (which can be 0-1024) as delayinseconds ~= 0 - 64 seconds
for(;count>=0;count--)
{
for(;potIn>0;potIn=potIn-20)
{
//this is to make the LED flash every 100+potIn miliseconds
digitalWrite(11,HIGH); //set led to on
delay(100+potIn);
digitalWrite(11,LOW); //set led to off
delay(100+potIn);
}
}
count = 1;
trigger = 1; //change trigger to 1 to setup empty
digitalWrite(11,LOW); //set led off
}
else if(trigger)
{
mainServo.write(empty); //set the servo to empty position
delay(1000); //delay while servo sets position
trigger = 0; //change trigger to 0 to setup fill
}
}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to 
  //// get the total number of cycles to produce
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}

Thanks guys

Does that second one even compile?

yes

OK, the eccentric use of braces threw me.

lol yea, I was afraid if I compartmentalize everything I would get lost.

The delays in this section would seem to account for what you see:

for(;potIn>0;potIn=potIn-20)
{
//this is to make the LED flash every 100+potIn miliseconds
digitalWrite(11,HIGH); //set led to on
delay(100+potIn);
digitalWrite(11,LOW); //set led to off
delay(100+potIn);
}
  {
    buzz(5, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
    delay(1000); // wait a bit between buzzes  
  }

Useless braces.

    for(;count>=0;count--)
    {
      for(;potIn>0;potIn=potIn-20)
      {

Where is the initialization step? That really isn’t optional.

Consistent placement of the { is a good thing. So is using Tools + Auto Format before posting code.

thanks for the help guys. I blanked out

//potIn = analogRead(potPin); //read the position the potentiometer is at //if the trigger value is 0 fill the hopper //if(!trigger)

and it worked...I dont know why. Now i have to find a way to intergrate that to make the servo work lol.

----------ok so i figured out what was wrong. Thanks again guys.

My next question is how would I set the buzzer to react to a trigger. Whenever the servo rotates I'd like the buzzer to activate, and be able to turn off the buzzer based on another trigger.

If i type if digitalwrite(11,HIGH); buzz(5, 2500, 500);

Or something along that logic, will it work?

Something along those lines, yes.
But why not just try it?

haha yea. sorry about that. I'm still kind of new to the arduino scene and I have these fears that if i make a mistake something will catch fire... If I run into any more problems I'll come back with a specific problem.

Why not try it and see ? Can digitalwrite(11,HIGH); ever be true ? Where are the brackets for the if ?

There is a known bug in delayMicros() when called with 0. as your param is calculated it can hit that bug. I posted to the Arduino team including a fix but it is not included yet.

ok im back. I set up a command...

if (11,HIGH){ buzz(5, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds delay(1000); // wait a bit between buzzes }

but regardless of whether the pin is HIGH or LOW the buzzer will beep.

Is my if function wrong? Thanks.

Did you intend to read pin 11?

1 sec let me truncate the specific code…

int ledPin = 11;

void setup()
{
//basic setup
{
pinMode(ledPin, OUTPUT);      // sets the digital pin as output
//set the pin modes
pinMode(5, OUTPUT); // set a pin for buzzer output
pinMode(11, OUTPUT); //used for the led

}
}

void loop() {
  
if (11,HIGH){
buzz(5, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
delay(1000); // wait a bit between buzzes
}
digitalWrite(ledPin, HIGH);   // sets the LED on
delay(1000);                  // waits for a second
digitalWrite(ledPin, LOW);    // sets the LED off
delay(1000);                  // waits for a second


}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to 
  //// get the total number of cycles to produce
 for (long i=0; i < numCycles; i++){ // for the calculated length of time...
    digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait againf or the calculated delay value
  }
}

if (12,HIGH){Let me rephrase. Did you intend to read pin 12?

sorry i realized i posted the version of me seeing if changing the pin would do anything. Yea, id like it to read the pin, and if its on (HIGH) it will turn on the buzzing.

digitalRead

Is this the right logic?

First step is to tell the arduino to read the status of pin 11..under the void loop.

digitalRead (11);

Then if its set to high I want it to activate the buzzer.

if (11,HIGH){ buzz(5, 2500, 500); delay(1000);

The logic seems fine, I think I just dont know how to write it properly. Am I making a coding mistake?