I am trying to run a simple program that sends data to a 595 register and a buzzer. One of my loops runs without an issue, but the other refuses to run at all. It is caused by the buzzer bool, on the tone() line of the code. The declared variable and setup is the same for both programs:
const int latchPin = 8;
const int clockPin = 12;
const int dataPin = 10;
const int buzzerpin = 4;
int loopdelay = 50;
bool buzzer = 0;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(buzzerpin, OUTPUT);
}
This loop runs without a problem with the buzzer true or false:
for (int numberToDisplay = 1; numberToDisplay < 128; numberToDisplay *= 2) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
digitalWrite(latchPin, HIGH);
delay(loopdelay);
}
tone(buzzerpin, 500 * buzzer, 40); //This runs fine when buzzer is 0
for (int numberToDisplay = 128; numberToDisplay > 1; numberToDisplay = numberToDisplay /= 2) {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
digitalWrite(latchPin, HIGH);
delay(loopdelay);
}
tone(buzzerpin, 500 * buzzer, 40); //This runs fine when buzzer is 0
But whenever I place this code in the loop, it refuses to run (when buzzer is set to false). If it's true it will run without any issues:
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, random(256));
digitalWrite(latchPin, HIGH);
tone(buzzerpin, 500 * buzzer, 40); //This does not work when buzzer is 0. Refuses to run, even the 3 lines of code before
delay(loopdelay);
