Need Help Getting an active buzzer and stepper motor to turn on at the same time

I am attempting to turn on a stepper motor and an active buzzer at the same time by pressing a button. I am also attempting to get them to turn off at the same time. Currently I can get them to work seperately but not in tandem. I know I need to integrate my statements but I am unsure how to do that. Any help would be appreciated!

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(buttonPin, INPUT_PULLUP); // set arduino pin to input pull-up mode
  pinMode(buzzer, OUTPUT);       // set arduino pin to output mode
  pinMode(4, INPUT);

void loop() {
  int buttonState = digitalRead(buttonPin); // read new state
  if (buttonState == HIGH) {
    for (int stepper = 0; stepper < 4; stepper++) {
      if (stepper >= 1 && stepper < 4) {
        digitalWrite(buzzer, HIGH);
  else {
    digitalWrite(buzzer, LOW);

  // iterate over the notes of the melody.
  // Remember, the array is twice the number of notes (notes + durations)
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    // calculates the duration of each note
    divider = pgm_read_word_near(melody + thisNote + 1);
    if (divider > 0) {
      // regular note, just proceed
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      // dotted notes are represented with negative durations!!
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes

    // we only play the note for 90% of the duration, leaving 10% as a pause
    tone(buzzer, pgm_read_word_near(melody + thisNote), noteDuration * 0.9);

    // Wait for the specific duration before playing the next note.

    // stop the waveform generation before the next note.


An active buzzer only has one input to turn it on and off, you cannot make it play notes. Are you sure that you have an active buzzer and not just a piezo speaker?

In any case, you cannot use delays() if you want multiple things to happen. You need to note the time when something has started and then come back to it when the time has expired by continuously checking the time and not holding up the rest of the processing. This is how the BlinkWithoutDelay example works, so you can look at that for a template.

Thank you! I figured it out!

If you are trying to play different notes you probably want to take a look at Passive Buzzer. Active buzzer will only give you a single tone.

Thanks for sharing your solution for those who may be searching the forum for a similar issue in the future. You solved their problem in six words.

I am, of course, being sarcastic if you couldn't guess.

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