Changing to fast to produce sound?

Hi all. First of all if there could be a better title please let me know not sure how i could formulate my problem better.

I have borrowed some volt measurment code and adjusted it a bit so i would have a buzzer buzz when voltage would drop below a certain voltage.
In the first case, having the voltage drop below 12 volt with a delay of 1000ms at end of coude , would give me a solid beep until voltage would rise above 12v again.

For my second try i changed my code. beep when voltage drops below 12.05v and measuring every 500ms.

Here is where i had troubles, there was no beep. i had my serial monitor running and data being saved. there were two measurments below 12.05v.

below i add the code i borrowed and adjusted, any of you can figure out why the faster readings and different voltage would give me troubles?

/*
  0 - ~17volt voltmeter
  works with 3.3volt and 5volt Arduinos
  uses the stable internal 1.1volt reference
  10k resistor from A0 to ground, and 150k resistor from A0 to +batt
  (1k8:27k or 2k2:33k are also valid 1:15 ratios)
  100n capacitor from A0 to ground for stable readings
*/

int buzzer = 11; 

unsigned int total; // holds readings
float voltage; // converted to volt

void setup() {
  pinMode(11, OUTPUT);
  analogReference(INTERNAL); // use the internal ~1.1volt reference | change (INTERNAL) to (INTERNAL1V1) for a Mega
  Serial.begin(9600); // ---set serial monitor to this value---
}

void loop() {
  total = 0; // reset
  analogRead(A0); // one unused reading to clear any ghost charge
  for (int x = 0; x < 64; x++) { // 64 analogue readings for averaging
    total = total + analogRead(A0); // add each value
  }
  voltage = total * 0.0002505; // convert readings to volt | ---calibrate by changing the last three digits---

  Serial.print("The battery is ");
  Serial.print(voltage, 2); // change to (voltage, 3) for three decimal places
  Serial.println(" volt");
  if(voltage<=12.05  )
  {
    digitalWrite (buzzer, HIGH);
  }

  else 
  {
    digitalWrite (buzzer, LOW);
  }
  
  delay(500); // readout delay
}

Have you considered the tone() function?

...R

I have not. Never heard of it I will search for it right now.

A quick look at it tells me you can produce difrent frequencies? The frequency it uses produces a sound I can hear, only not with the code I posted. How could the tone() function help me there?

Yes, [u]tone(frequency)[/u].

...It looks like your problem is, you forgot to delay() after writing high.

DVDdoug, could you explain to me why and how "tone(frequency)" would help me? Why should i set any frequency? Output Pin high gives me sound.

And why is there a delay needed after high?

I have a relay switching a fridge on and off using this code:

  if(steinhart<=4 )//if temperature is  degrees
{
  digitalWrite(relay,HIGH);
}

  else if(steinhart>=8 )//if temperature is   degrees  
{
  digitalWrite(relay,LOW);
}

}

No delay there, can't understan why there should be a delay.

What sort of buzzer do you have? Post a link to the product page.

Some buzzers beep continuously when powered, others require a stream of HIGH/LOW pulses.

I know. It is an active buzzer. 5v dc and we have sound.

It works on <12v and 1000ms eelay.

It does not work on <12.05 and 500ms delay.

just does not produce a sound after the code was changed.

Bringamosa:
I know. It is an active buzzer. 5v dc and we have sound.

Your code led me to think it needed a series of HIGH and LOW pulse at the frequency you wanted.

If it just needs a continuous HIGH then you need to make sure it stays on long enough to hear it. Either include a short delay() after making it HIGH or check that the condition that makes it HIGH persists for long enough to hear it.

...R

Problem solved. A loose connection. Works fine now. Thanks all for thinking along.