This is sample code for Simple Alarm System Using Maker Nano's tutorial.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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