Random Buzzer Timer Generator

Hi!

A project i'm trying to achieve is driving me crazy... basically I am trying to make a version of a BMX random start BUT only using buzzers.

I was hoping to have two different buzzer tones (two seperate buzzers connected) and have the first buzzer beep for a random time between 1-4 seconds (when I press a button) and then as soon as that as finished beeping, for the second (different tone) buzzer to go off.

The idea for this is to have a random start time (between 1-4 sec) and a "go" buzzer which is set off after the initial 1-4sec random start.

Anyone have any ideas of where I could start with this?

Thanks!

Anyone have any ideas of where I could start with this?

int randomDelay = random(1, 5);

Now your turn

UKHeliBob:

int randomDelay = random(1, 5);

Now your turn

Thanks - I was stuck more on how to trigger the second buzzer straight after the first one had finished its (random) start time.

Question : how will the program know that the first random time has finished ?

If the program really is doing nothing except what you describe then you could use delay() to achieve the delays but you would be better in the long term to use millis() for timing as in the BlinkwithoutDelay example.

UKHeliBob:
Question : how will the program know that the first random time has finished ?

This is what was stumping me...

Could I theoretically create 4 instances of an event? (as there are 4 potential start times)

  • Instance 1 = 1sec beep (startup) - 2sec delay - second beep (go)

  • Instance 2 = 2sec beep (startup) - 3sec delay - second beep (go)

  • Instance 3= 3sec beep (startup) - 4sec delay - second beep (go)

  • Instance 4 = 4sec beep (startup) - 5sec delay - second beep (go)

and then use

 int randomInstance = random(1, 4);

Please excuse the noobiness - I am trying LOL

Forget the random start delay for now and just use a fixed time period.

You mention the first buzzer beeping. Does that imply that it is turned on and off to make it beep or is the buzz/tone continuous ?

It seems to me that you have 3 states

state 0 - waiting for button press
state 1 - sound the "get ready" buzzer (maybe beeping on/off)
state 2 - sound the "go" buzzer and maybe turn it off after a while

Does that match what you want ?

What hardware do you have for this project ?

Thats it to a tee! - sorry if I have been confusing things.

You mention the first buzzer beeping. Does that imply that it is turned on and off to make it beep or is the buzz/tone continuous ?

at the moment, I am not overly worried as to the final result. If it is easy to achieve beeping on/off then yes! if it sounds like a hard task - I can also get away with a constant tone. All the system needs is a 2 tone change to signal a start.

As for hardware - I have a Uno R3, ESP8266/NodeMCU (don't ask.. LOL) - I will be sticking with the R3 for this, NodeMCU is a backup

Activated by a momentary switch & buzzers TBD (Waiting for the right ones to come in stock, but most likely will be 3V)

There are basically 2 different types of "buzzer". One type buzzes when you apply a voltage to it. 5V would be the most convenient for use with an Arduino. The other requires its input to be turned on and off in order to create a tone. Which type are yours ? If the second type then you will only need one of them because the frequency is controlled by the program, whereas the other type sound a fixed frequency.

I might be going about this the completely wrong way - so please feel free to correct me if you think I am.
This is where I am up to so far, I have setup (I hope) how the buttons will interface with the buzzers... but now I just need to figure out the four different start sequences and how to get them randomly chosen.

const int buttonGo = 2;
const int buttonRst = 3;
const int BuzzWarn =  13;
const int BuzzGo = 12;

void setup() {
  
  pinMode(buttonGo, INPUT);
  pinMode(buttonRst, INPUT);
  pinMode(BuzzWarn, OUTPUT); 
  pinMode(BuzzGo, OUTPUT); 

}

void loop()
{
randNumber = random(1,4);
Serial.println(randNumber);
int buttonGoState, buttonRstState;
buttonGoState = digitalRead(buttonGo);
delay(175);
buttonRstState = digitalRead(ButtonRst);
if  ((buttonRstState == LOW) && buttonGoState == LOW))
{
    digitalWrite(buzzWarn, LOW); 
    digitalWrite(buzzGo, LOW); 
}
else if (buttonGoState == LOW)
{
digitalWrite //Random 1-4 start sequence code
}

Which type are yours ?

As I mentioned - I haven't been able to order any yet. Closest hobby shop is waiting on stock.. so! if you would suggest one over the other - I would be willing to ditch my plan and rewrite it if it would suit one buzzer over the other.

Make sure that you get 5V buzzers. I would suggest using the type that controlled by the Arduino as you will only need one of them.

Meanwhile, here is something to play will.

enum states {WAIT_FOR_START, COUNTING_DOWN, STARTED};
const byte startButton = A1;
const byte resetButton = A2;
byte currentState = 0;
boolean startMessageDone = false;
unsigned long countdownStart;
unsigned long countdownPeriod;

void setup()
{
  Serial.begin(115200);
  pinMode(startButton, INPUT_PULLUP);
  pinMode(resetButton, INPUT_PULLUP);
  currentState = WAIT_FOR_START;
}

void loop()
{
  if (digitalRead(resetButton) == LOW)
  {
    Serial.println("RESET BUTTON PRESSED");
    delay(1000);  //ugly but it will do for now
    startMessageDone = false;
    currentState = WAIT_FOR_START;
  }
  switch (currentState)
  {
    case WAIT_FOR_START:
      if (!startMessageDone)
      {
        Serial.println("Press Start button to begin countdown");
        startMessageDone = true;
      }
      if (digitalRead(startButton) == LOW)
      {
        currentState = COUNTING_DOWN;
        countdownStart = millis();
        randomSeed(millis());        //make the delay more random
        countdownPeriod = random(1000, 4000);
        Serial.println("Countdown has begun");
        Serial.println(countdownPeriod);  //remove when debugged
      }
      break;

    case COUNTING_DOWN:
      if (millis() - countdownStart >= countdownPeriod)
      {
        Serial.println("GO !");
        currentState = STARTED;
      }
      break;

    case STARTED:
      break;
  }
}

If you add functions to sound the buzzer(s) you can call them when the system is in various states.

Apologies for the delay!

I really appreciate you having a crack at this for me, I will give it a go and see where I can get to.

I'll let you know how it goes.

Regards