Created
May 21, 2015 16:24
-
-
Save t-kashima/4dd61585b3958697d1e3 to your computer and use it in GitHub Desktop.
Alarm clock for Arduino
This file contains hidden or 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
// Now time | |
#define START_HOUR 1 | |
#define START_MINUTES 21 | |
// Alarm | |
#define ALARM_HOUR 1 | |
#define ALARM_MINUTES 22 | |
bool isRing = false; | |
int nowHour = START_HOUR; | |
int nowMinutes = START_MINUTES; | |
int nowSecond = 50; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(10, OUTPUT); | |
pinMode(11, OUTPUT); | |
pinMode(12, OUTPUT); | |
// Switch | |
digitalWrite(12, HIGH); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
showTime(); | |
if (isSwitchON()) { | |
checkRing(); | |
if (isRing) { | |
startRing(); | |
checkStopRing(); | |
if (isRing == false) { | |
stopRing(); | |
} | |
} | |
} | |
nextTime(); | |
} | |
bool isSwitchON() { | |
if (digitalRead(13) == HIGH) { | |
return true; | |
} | |
return false; | |
} | |
void checkRing() { | |
if (nowHour == ALARM_HOUR && | |
nowMinutes == ALARM_MINUTES && | |
nowSecond == 0) { | |
isRing = true; | |
} | |
} | |
void checkStopRing() { | |
if (0 > getDistance()) { | |
isRing = false; | |
} | |
} | |
void startRing() { | |
digitalWrite(10, HIGH); | |
tone(10, 262); | |
} | |
void stopRing() { | |
noTone(10); | |
} | |
int getDistance() { | |
digitalWrite(11, HIGH); | |
int sum = 0; | |
for (int i = 0; i < 100; i++) { | |
sum += analogRead(0); | |
} | |
int distance = sum / 100; | |
return distance; | |
} | |
void nextTime() { | |
delay(1000); | |
nowSecond += 1; | |
if (60 <= nowSecond) { | |
nowSecond = 0; | |
nowMinutes += 1; | |
} | |
if (60 <= nowMinutes) { | |
nowMinutes = 0; | |
nowHour += 1; | |
} | |
if (24 <= nowHour) { | |
nowHour = 0; | |
} | |
} | |
void showTime() { | |
Serial.print(nowHour); | |
Serial.print(":"); | |
Serial.print(nowMinutes); | |
Serial.print(":"); | |
Serial.println(nowSecond); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment