Siren Spark
Difficulty: ● ○ ○ ○ ○
-
Make Sure You have:
-
Did you know ?
Parts Required
No products found in parts_required metafield
How It Works ?
GENESIS
Connection Guide
Snap your modules as shown above, copy the code into the Arduino IDE and Click upload
#include <Adafruit_NeoPixel.h>
#define SWITCH_PIN 14 // Toggle switch
#define BUZZER_PIN 6 // Buzzer pin
#define MATRIX_PIN 10 // NeoMatrix IN pin (GPIO10)
#define MATRIX_SIZE 25 // 5x5 matrix has 25 LEDs
Adafruit_NeoPixel matrix = Adafruit_NeoPixel(MATRIX_SIZE, MATRIX_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
pinMode(SWITCH_PIN, INPUT_PULLUP); // Toggle switch input
pinMode(BUZZER_PIN, OUTPUT); // Buzzer output
matrix.begin(); // Start NeoMatrix
matrix.show(); // All off
}
void policeLightsInterruptible() {
for (int i = 0; i < 15; i++) {
if (digitalRead(SWITCH_PIN) == LOW) return; // Abort if switch turned off
int randomIndex = random(MATRIX_SIZE);
uint32_t color = random(2) == 0 ? matrix.Color(255, 0, 0) : matrix.Color(0, 0, 255);
matrix.setPixelColor(randomIndex, color);
}
matrix.show();
delay(30);
matrix.clear();
matrix.show();
}
void sirenInterruptible() {
for (int i = 0; i < 3; i++) {
for (int freq = 1000; freq <= 2500; freq += 50) {
if (digitalRead(SWITCH_PIN) == LOW) {
noTone(BUZZER_PIN);
matrix.clear(); matrix.show();
return;
}
tone(BUZZER_PIN, freq, 30);
policeLightsInterruptible();
}
for (int freq = 2500; freq >= 1000; freq -= 50) {
if (digitalRead(SWITCH_PIN) == LOW) {
noTone(BUZZER_PIN);
matrix.clear(); matrix.show();
return;
}
tone(BUZZER_PIN, freq, 30);
policeLightsInterruptible();
}
}
noTone(BUZZER_PIN);
delay(300);
}
void loop() {
if (digitalRead(SWITCH_PIN) == HIGH) {
sirenInterruptible(); // Run siren with check for early stop
} else {
noTone(BUZZER_PIN); // Ensure buzzer is off
matrix.clear(); matrix.show(); // Turn off LEDs
}
}