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);
stepper.setSpeed(200);
}
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);
Serial.println(stepper);
}
delay(950);
}
}
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.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
stepper.step(2048);
}
}