Skip to content

Instantly share code, notes, and snippets.

@suadanwar

suadanwar/simple_alarm.ino

Last active Nov 30, 2020
Embed
What would you like to do?
This is sample code for Simple Alarm System Using Maker Nano's tutorial.
#define SENSOR 3
#define PIEZO 8
#define NOTE_G5 784
#define NOTE_C6 1047
int Sound[] = {NOTE_G5, NOTE_C6};
int SoundNoteDurations[] = {12, 8};
#define playSound() playMelody(Sound, SoundNoteDurations, 2)
char inChar;
String inString;
// variables will change:
int sensorState = 0; // variable for reading the sensor status
void setup() {
pinMode(PIEZO, OUTPUT);
pinMode(SENSOR, INPUT_PULLUP);
}
void loop() {
// read the state of the Magnetic Sensor value:
sensorState = digitalRead(SENSOR);
// check if the pushbutton is pressed. If it is, the Magnetic Sensor is HIGH:
if (sensorState == HIGH) {
playSound();
} else {
noTone(PIEZO);
}
}
void playMelody(int *melody, int *noteDurations, int notesLength)
{
pinMode(PIEZO, OUTPUT);
for (int thisNote = 0; thisNote < notesLength; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(PIEZO, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment