Using millis() to change buzzer tones with a certain time delay

Hello. I got my Arduino a couple of weeks ago and am learning lots and figuring a lot out for myself. However, I’ve been playing with millis() function and cannot get it to make my buzzer delay a certain time before changing tones again.

#include <Servo.h>

int pushb = 3;         // Input from the pushbutton
int servoPin = 9;      // Servo signal wire connected to a PWM capable pin
int piezo = 6;         // Piezo buzzer

Servo theservo;  // create servo object to control a servo
// a maximum of eight servo objects can be created

long previousMillis = 0;    // Store last update
long interval = 500;        // First length of time I want the buzzer to stay on set tone
long interval2 = 600;       // Second length of time I want the buzzer to stay on now changed tone


void setup()
{
  theservo.attach(servoPin);  // Attaches the servo on pin 9 to the servo object
  pinMode(pushb, INPUT);      // Set pushbutton pin mode
  pinMode(piezo, OUTPUT);     // Set piezo buzzer pin mode
}

void loop()
{
  {
    if (digitalRead(pushb) == HIGH)   // If pushbutton held keep servo going clockwise
    {
      theservo.write(45);
      tone(piezo, 1200);
    }

    else                              // Otherwise if the pushbutton is released, change the servo to go anti-clockwise
    {
      theservo.write(125);
      if (millis() - previousMillis > interval)    // Attemping to make this keep the buzzer running for 500ms
      {
        previousMillis = millis();
        tone(piezo, 500);
      }
      if (millis()- previousMillis > interval2)    // Attempting to make this keep the buzzer running on a different tone for 600ms
      {                                            // before if goes back to the above if statement
        tone(piezo, 700);
      }
    }
  }
}

I’ve been trying to get this to work for about a week with no success and I think I’m still lost in confusion. I’d probably like to apply this to more than two functions later down the track which is why I’m determined to get it working. Delay is good here but not when I need to poll for sensor changes in another build.

Thanks in advance for any help given!

dglad4.

Are you trying to sound two different tones, one after the other? Or something else? Your explanation wasn't very clear. I need to know that so I don't go off and suggest something that doesn't do what you're trying to do.

 if (millis() - previousMillis > interval)    // Attemping to make this keep the buzzer running for 500ms
      {
        previousMillis = millis();
        tone(piezo, 500);
      }
      if (millis()- previousMillis > interval2)    // Attempting to make this keep the buzzer running on a different tone for 600ms
      {                                            // before if goes back to the above if statement
        tone(piezo, 700);
      }

This has two separate pieces of code to change the tone and I suspect they are overlapping each other. I think you should remove the second section and use a single IF (MILLIS to change between two tones.

if (millis() - previousMillis > interval)    // Attemping to make this keep the buzzer running for 500ms
      {
        previousMillis = millis();
        if (toneVal == 500) {
             toneVal = 700;
        }
        else {
            toneVal = 500;
        }
        tone(piezo, toneVal);
      }

...R

aarg: Are you trying to sound two different tones, one after the other? Or something else? Your explanation wasn't very clear. I need to know that so I don't go off and suggest something that doesn't do what you're trying to do.

Yes, sorry. I had most of the explaining next to the code.

Just a repetition between the tones, go boop for 500ms then change to beep for 600ms, then again go boop for the 500ms and then beep for 600ms and so on, but the loop is continuously checking if the button is still held down and would cut short a full tone length if released. A delay will hold the buzzer on until it's delay time finished, though the button may have been released 300ms before due to finish.

Thanks.

Robin2: if (millis() - previousMillis > interval)    // Attemping to make this keep the buzzer running for 500ms       {         previousMillis = millis();         tone(piezo, 500);       }       if (millis()- previousMillis > interval2)    // Attempting to make this keep the buzzer running on a different tone for 600ms       {                                            // before if goes back to the above if statement         tone(piezo, 700);       }

This has two separate pieces of code to change the tone and I suspect they are overlapping each other. I think you should remove the second section and use a single IF (MILLIS to change between two tones.

if (millis() - previousMillis > interval)    // Attemping to make this keep the buzzer running for 500ms
      {
        previousMillis = millis();
        if (toneVal == 500) {
             toneVal = 700;
        }
        else {
            toneVal = 500;
        }
        tone(piezo, toneVal);
      }

...R

This seems to be the way to get around having two time checks happening however, if in future I wanted two different commands to swap like the buzzer frequency, such as digitalWrite and tone would it be possible? I can't figure out how to do it.

Thanks for your reply.

dglad4: Yes, sorry. I had most of the explaining next to the code.

I read the code. It wasn't really a high level description of what behaviour you expect from it. That's what I was looking for. But it looks like other people guessed it.

dglad4: if in future I wanted two different commands to swap like the buzzer frequency, such as digitalWrite and tone would it be possible?

I don't understand.

And maybe I misunderstood your original question?

...R