My Dad and I are just learning how to use my new UNO.
We have learned how to make leds flash at the touch of a button. We have also learned how to make a passive buzzer sound at the touch of a button and change tone to sound like a police car.
We have just learned that we cant run more than one loop so we have combined the buzzer code and the LED code twice (for blue and red LEDs) and it works... but the delay on the LEDs is ruining the buzzer sound. Is there anything we can do?
The code is below.
Thank you!
Code:
int buzzerPin = 9; // the number of the buzzer pin
int BlueLEDpin = 5; // the number of the blue LED pin
int RedLEDpin = 7; // the number of the Red LED pin
float sinVal; // Define a variable to save sine value for buzzer
int toneVal; // Define a variable to save sound frequency for buzzer
void setup() {
pinMode(buzzerPin, OUTPUT); // Set Buzzer pin to output mode
pinMode(BlueLEDpin, OUTPUT); // Set Blue LED pin to output mode
pinMode(RedLEDpin, OUTPUT); // Set Red LED pin to output mode
}
void loop() {
Buzzer();
BlueLED();
RedLED();
}
void Buzzer() {
for (int x = 0; x < 360; x++) { // X from 0 degree->360 degree
sinVal = sin(x * (PI / 180)); // Calculate the sine of x
toneVal = 2000 + sinVal * 500; // Calculate sound frequency according to the sine of x
tone(buzzerPin, toneVal); // Output sound frequency to buzzerPin
delay(1);
}
}
void BlueLED() {
digitalWrite(5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1); // wait for a second
digitalWrite(5, LOW); // turn the LED off by making the voltage LOW
delay(1); // wait for a second
}
void RedLED() {
digitalWrite(7, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1); // wait for a second
digitalWrite(7, LOW); // turn the LED off by making the voltage LOW
delay(1); // wait for a second
}