Maintenance: Project Hub will be unavailable on Monday 24 (9AM to 6PM CET) while we deploy critical improvements
Components and supplies
LED (generic)
Tactile Switch, SPST-NO
Breadboard (generic)
Buzzer, Piezo
Resistor 1k ohm
Arduino UNO
Male/Male Jumper Wires
Project description
Code
alarmTimer.ino
arduino
1#include "LowPower.h" 2 3// set the corresponding snooze/off button pin number 4const int buttonPin = 2; 5 6// set the four corresponding LED pin numbers 7const int ledPin1 = 4; 8const int ledPin2 = 3; 9const int ledPin3 = 5; 10const int ledPin4 = 6; 11 12// set the corresponding piezo buzzer pin 13const int alarmPin = 9; 14 15// initialize timer variables 16int nappingTime = 0; 17String strNappingTime = ""; 18 19// initialize the snooze/off button state 20int buttonState = 0; 21 22void setup() { 23 // initialize the LED pins as outputs: 24 Serial.begin(9600); 25 pinMode(ledPin1, OUTPUT); 26 pinMode(ledPin2, OUTPUT); 27 pinMode(ledPin3, OUTPUT); 28 pinMode(ledPin4, OUTPUT); 29 30 // initialize the snooze button pin as an input: 31 pinMode(buttonPin, INPUT); 32 33 // user inputs napping time 34 Serial.println("How long do you want to set the timer for?"); 35 while (Serial.available() == 0) { } 36 strNappingTime = Serial.readString(); 37 nappingTime = strNappingTime.toInt(); 38 delay (nappingTime * 60000); 39} 40 41void loop() { 42 // after time is up, reads the state of the snooze button: 43 buttonState = digitalRead(buttonPin); 44 45 // checks to see if the snooze/off button is pressed: 46 if (buttonState == LOW) { 47 // alarm rings 48 // LEDs flash 49 tone(alarmPin, 3000); 50 digitalWrite(ledPin1, HIGH); 51 digitalWrite(ledPin2, HIGH); 52 digitalWrite(ledPin3, HIGH); 53 digitalWrite(ledPin4, HIGH); 54 delay (500); 55 noTone(alarmPin); 56 digitalWrite(ledPin1, LOW); 57 digitalWrite(ledPin2, LOW); 58 digitalWrite(ledPin3, LOW); 59 digitalWrite(ledPin4, LOW); 60 delay(500); 61 } 62 63 // if the snooze button is pressed 64 else { 65 // alarm stops 66 // turns LEDs off 67 noTone(alarmPin); 68 digitalWrite(ledPin1, LOW); 69 digitalWrite(ledPin2, LOW); 70 digitalWrite(ledPin3, LOW); 71 digitalWrite(ledPin4, LOW); 72 delay (1000); 73 // shuts down until reset button is pressed 74 LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 75 } 76}
alarmTimer.ino
arduino
1#include "LowPower.h" 2 3// set the corresponding snooze/off button pin number 4const int buttonPin = 2; 5 6// set the four corresponding LED pin numbers 7const int ledPin1 = 4; 8const int ledPin2 = 3; 9const int ledPin3 = 5; 10const int ledPin4 = 6; 11 12// set the corresponding piezo buzzer pin 13const int alarmPin = 9; 14 15// initialize timer variables 16int nappingTime = 0; 17String strNappingTime = ""; 18 19// initialize the snooze/off button state 20int buttonState = 0; 21 22void setup() { 23 // initialize the LED pins as outputs: 24 Serial.begin(9600); 25 pinMode(ledPin1, OUTPUT); 26 pinMode(ledPin2, OUTPUT); 27 pinMode(ledPin3, OUTPUT); 28 pinMode(ledPin4, OUTPUT); 29 30 // initialize the snooze button pin as an input: 31 pinMode(buttonPin, INPUT); 32 33 // user inputs napping time 34 Serial.println("How long do you want to set the timer for?"); 35 while (Serial.available() == 0) { } 36 strNappingTime = Serial.readString(); 37 nappingTime = strNappingTime.toInt(); 38 delay (nappingTime * 60000); 39} 40 41void loop() { 42 // after time is up, reads the state of the snooze button: 43 buttonState = digitalRead(buttonPin); 44 45 // checks to see if the snooze/off button is pressed: 46 if (buttonState == LOW) { 47 // alarm rings 48 // LEDs flash 49 tone(alarmPin, 3000); 50 digitalWrite(ledPin1, HIGH); 51 digitalWrite(ledPin2, HIGH); 52 digitalWrite(ledPin3, HIGH); 53 digitalWrite(ledPin4, HIGH); 54 delay (500); 55 noTone(alarmPin); 56 digitalWrite(ledPin1, LOW); 57 digitalWrite(ledPin2, LOW); 58 digitalWrite(ledPin3, LOW); 59 digitalWrite(ledPin4, LOW); 60 delay(500); 61 } 62 63 // if the snooze button is pressed 64 else { 65 // alarm stops 66 // turns LEDs off 67 noTone(alarmPin); 68 digitalWrite(ledPin1, LOW); 69 digitalWrite(ledPin2, LOW); 70 digitalWrite(ledPin3, LOW); 71 digitalWrite(ledPin4, LOW); 72 delay (1000); 73 // shuts down until reset button is pressed 74 LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 75 } 76}
Downloadable files
Circuit Schematic
Circuit Schematic
Comments
Only logged in users can leave comments
manjariiiiiiiiitalasila
0 Followers
•0 Projects
2
0