Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Last active November 30, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suadanwar/79e7f6083cce5477a5d7bb46299cb594 to your computer and use it in GitHub Desktop.
Save suadanwar/79e7f6083cce5477a5d7bb46299cb594 to your computer and use it in GitHub Desktop.
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