Making Annoying PCB, Beeping sound wont change tone

Making an Annoying beeping PCB. I can get the loop to work correctly at random intervals but the passive buzzer is making a low-volume tap rather then a high pitch beep. I tried using tone(buzzer, 1000) which makes the correct sound but its constant instead of being at random intervals. I have attached code.

//Make sure to plug the buzzer into this pin # on the arduino!
const int buzzer = 9;

//You buzzer will turn on at random intervals between the shortest and longest wait times in seconds entered here.
const int shortest_wait = 300;
const int longestest_wait = 600;
long randomWait; //we'll change this each loop.

//How many miliseconds the buzzer will stay on?
const int buzzer_length = 500;

//This loop runs once when the Arduino is turned on, to set up the buzzer.
void setup(){
  pinMode(buzzer, OUTPUT);
}

//This loop runs over and over, as long as the Arduino has power!
void loop(){

  //pick a new random wait time.
  randomWait = random(shortest_wait, longestest_wait);

  //turn the buzzer on with our selected sound.
  digitalWrite(buzzer,LOW);
  delay(buzzer_length);
  
  //leave the buzzer off for our random amount of time.
  digitalWrite(buzzer,HIGH);
  delay(randomWait*1000);
}

Try:
int toneDuration = 500; // 1/2 second
. . . . .
tone(buzzer, 1000, toneDuration)

no luck yet, will keep trying

Not sure what you really want.

Here is a start:

const int buzzer = 9;

const int shortest_wait    = 300;
const int longestest_wait  = 600;

int randomWait;                 

const int buzzer_length     = 500;


//********************************************^************************************************
void setup()
{
  pinMode(buzzer, OUTPUT);
}


//********************************************^************************************************
void loop()
{
  randomWait = random(shortest_wait, longestest_wait);

  tone(buzzer, 3500, randomWait);

  delay(buzzer_length);

}

//********************************************^************************************************

Updated version

const int buzzer = 9;

const int shortest_wait    = 300;
const int longestest_wait  = 600;

int duration;
int frequency;

const int buzzer_length     = 500;


//********************************************^************************************************
void setup()
{
  pinMode(buzzer, OUTPUT);
}


//********************************************^************************************************
void loop()
{
  duration = random(shortest_wait, longestest_wait);
  frequency = random(300,500)*10; 
  
  tone(buzzer, frequency, duration);

  delay(buzzer_length);

}

//********************************************^************************************************

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.