Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
423
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // Code was designed by Joseph melton and David Carver.
  3. // Date and time functions using a DS1307 RTC connected via I2C and Wire lib
  4.  
  5. #include <Wire.h>
  6. #include "RTClib.h"
  7. #include "Button.h"
  8.  
  9.  
  10. #define DS1307_ADDRESS 0x68
  11. byte zero = 0x00; //workaround for issue #527
  12. int latchPin = 8;
  13. int clockPin = 12;
  14. int dataPin = 11;
  15. int grd[] = {9,13,7,10};
  16. int PM_Light = 2;
  17.  
  18. int aH;
  19. int aM;
  20. boolean AM_PM = false;
  21. int lastHour;
  22. int alarmSwitch = A1;
  23. int alarmOnIndicator = 6;
  24. boolean alarmAM_PM = false;
  25. int lastAlarmHour;
  26. int buzzerPin = 5;
  27. int buzzerCounter = 0;
  28. int snoozeCounter = 0;
  29. boolean snoozer = false;
  30.  
  31. int lastTime;
  32. bool soundAlarm;
  33. int snoozeTime;
  34. int h;
  35. int m;
  36.  
  37. //int minutebutton = 3;
  38. //int hourbutton = 4;
  39. Button mButton(3);
  40. Button hButton(4);
  41. Button timeButton(17);
  42. Button alarmButton(16);
  43. Button snoozeButton(14);
  44.  
  45. RTC_DS1307 rtc;
  46.  
  47. void resetTime(){
  48. Wire.beginTransmission(DS1307_ADDRESS);
  49. Wire.write(zero);
  50. Wire.endTransmission();
  51.  
  52. Wire.requestFrom(DS1307_ADDRESS, 3);
  53.  
  54. int second = 0;
  55. int minute = 12;
  56. int hour = 12;
  57.  
  58. Wire.beginTransmission(DS1307_ADDRESS);
  59. Wire.write(zero); //stop Oscillator
  60.  
  61. Wire.write(decToBcd(second));
  62. Wire.write(decToBcd(minute));
  63. Wire.write(decToBcd(hour));
  64.  
  65. Wire.write(zero); //start
  66. Wire.endTransmission();
  67. }
  68.  
  69. void setup () {
  70. Serial.begin(9600);
  71. Wire.begin();
  72. //resetTime();
  73. pinMode(dataPin, OUTPUT);
  74. pinMode(latchPin, OUTPUT);
  75. pinMode(clockPin, OUTPUT);
  76. pinMode(grd[0], OUTPUT);
  77. pinMode(grd[1], OUTPUT);
  78. pinMode(grd[2], OUTPUT);
  79. pinMode(grd[3], OUTPUT);
  80. pinMode(PM_Light, OUTPUT);
  81. pinMode(3, INPUT);
  82. pinMode(4, INPUT);
  83. pinMode(alarmOnIndicator, OUTPUT);
  84. pinMode(alarmSwitch, INPUT);
  85. pinMode(buzzerPin, OUTPUT);
  86. aH = 12;
  87. aM = 0;
  88. timeButton.setHeldReturnDelay(0);
  89. timeButton.setHeldDelay(0);
  90. alarmButton.setHeldReturnDelay(0);
  91. alarmButton.setHeldDelay(0);
  92. }
  93.  
  94.  
  95. byte decToBcd(byte val){
  96. // Convert normal decimal numbers to binary coded decimal
  97. return ( (val/10*16) + (val%10) );
  98. }
  99.  
  100. byte bcdToDec(byte val) {
  101. // Convert binary coded decimal to normal decimal numbers
  102. return ( (val/16*10) + (val%16) );
  103. }
  104.  
  105. int displays = 4;
  106. //display fuction
  107. void segment(int screen, int displayNumber){
  108. //turn off all screens
  109. for(int i=0;i < 4;i++){
  110. digitalWrite(grd[i], LOW);
  111. }
  112.  
  113. //turn on the one you want
  114. digitalWrite(screen, HIGH);
  115.  
  116. float delayTime = 2;
  117. int dataNumber;
  118. digitalWrite(latchPin, LOW);
  119.  
  120. if(displayNumber == 0){
  121. dataNumber = 253;
  122. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  123. delay(delayTime);
  124. }else if (displayNumber == 1){
  125. dataNumber = 97;
  126. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  127. delay(delayTime);
  128. }else if (displayNumber == 2){
  129. dataNumber = 219;
  130. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  131. delay(delayTime);
  132. }else if (displayNumber == 3){
  133. dataNumber = 243;
  134. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  135. delay(delayTime);
  136. }else if (displayNumber == 4){
  137. dataNumber = 103;
  138. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  139. delay(delayTime);
  140. }else if (displayNumber == 5){
  141. dataNumber = 183;
  142. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  143. delay(delayTime);
  144. }else if (displayNumber == 6){
  145. dataNumber = 191;
  146. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  147. delay(delayTime);
  148. }else if (displayNumber == 7){
  149. dataNumber = 225;
  150. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  151. delay(delayTime);
  152. }else if (displayNumber == 8){
  153. dataNumber = 255;
  154. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  155. delay(delayTime);
  156. }else if (displayNumber == 9){
  157. dataNumber = 247;
  158. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  159. delay(delayTime);
  160. }else if (displayNumber == 10){
  161. dataNumber = 1;
  162. shiftOut(dataPin, clockPin, LSBFIRST, dataNumber);
  163. delay(delayTime);
  164. }
  165. digitalWrite(latchPin, HIGH);
  166. delay(1);
  167. digitalWrite(latchPin, LOW);
  168. shiftOut(dataPin, clockPin, LSBFIRST, 1);
  169. digitalWrite(latchPin, HIGH);
  170. }
  171.  
  172. void checkAlarmIndicator(){
  173. int alarmSwitchCheck = digitalRead(alarmSwitch);
  174.  
  175. if(alarmSwitchCheck == HIGH){
  176. digitalWrite(alarmOnIndicator, HIGH);
  177. }else{
  178. digitalWrite(alarmOnIndicator, LOW);
  179. }
  180. }
  181.  
  182.  
  183. void changeAlarm(int alarmTime){
  184. if(alarmTime == 3){
  185. aM++;
  186. if(aM > 59){
  187. aM = 0;
  188. }
  189. }else if(alarmTime == 4){
  190. aH++;
  191. if(aH > 12){
  192. aH = 1;
  193. }else if(lastAlarmHour == 11 && aH == 12){
  194. alarmAM_PM = !alarmAM_PM;
  195. }
  196. }
  197. lastAlarmHour = aH;
  198. }
  199.  
  200. bool checkAlarm(){
  201. int check = digitalRead(alarmSwitch);
  202. if(check == LOW){
  203. soundAlarm = false;
  204. snoozeTime = 0;
  205. }
  206. if(soundAlarm == true){
  207. return true;
  208. }
  209. if(check == HIGH){
  210. if(aH == h && aM == m && alarmAM_PM == AM_PM){
  211. if(lastTime != m && snoozeTime == 0){
  212. soundAlarm = true;
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218.  
  219. void changeTime(int time){
  220. Wire.beginTransmission(DS1307_ADDRESS);
  221. Wire.write(zero);
  222. Wire.endTransmission();
  223.  
  224. Wire.requestFrom(DS1307_ADDRESS, 3);
  225.  
  226. int second = bcdToDec(Wire.read());
  227. int minute = bcdToDec(Wire.read());
  228. int hour = bcdToDec(Wire.read());
  229.  
  230. second = 0;
  231.  
  232. if(time == 1){
  233. minute++;
  234. if(minute > 59){
  235. minute = 0;
  236. }
  237. }else if(time == 2){
  238. hour++;
  239. if(hour > 24){
  240. hour = 1;
  241. }
  242. }
  243.  
  244. Wire.beginTransmission(DS1307_ADDRESS);
  245. Wire.write(zero); //stop Oscillator
  246.  
  247. Wire.write(decToBcd(second));
  248. Wire.write(decToBcd(minute));
  249. Wire.write(decToBcd(hour));
  250.  
  251. Wire.write(zero); //start
  252. Wire.endTransmission();
  253. }
  254.  
  255.  
  256. void convertHour(){
  257. Wire.beginTransmission(DS1307_ADDRESS);
  258. Wire.write(zero);
  259. Wire.endTransmission();
  260.  
  261. Wire.requestFrom(DS1307_ADDRESS, 3);
  262.  
  263. int second = bcdToDec(Wire.read());
  264. int minute = bcdToDec(Wire.read());
  265. int hour = bcdToDec(Wire.read());
  266.  
  267. if(hour > 11 && hour < 24){
  268. AM_PM = true;
  269. }else{
  270. AM_PM = false;
  271. }
  272.  
  273. if(hour > 24){
  274. hour = 1;
  275.  
  276. Wire.beginTransmission(DS1307_ADDRESS);
  277. Wire.write(zero); //stop Oscillator
  278.  
  279. Wire.write(decToBcd(second));
  280. Wire.write(decToBcd(minute));
  281. Wire.write(decToBcd(hour));
  282. }
  283.  
  284. Wire.write(zero); //start
  285. Wire.endTransmission();
  286. }
  287.  
  288. void checkInput(){
  289. boolean minuteButton = mButton.getInput();
  290. boolean hourButton = hButton.getInput();
  291. boolean alarmB = alarmButton.getInput();
  292. boolean timeB = timeButton.getInput();
  293.  
  294. if(timeB == true && minuteButton == true){
  295. changeTime(1);
  296. }
  297. if(timeB == true && hourButton == true){
  298. changeTime(2);
  299. }
  300. if(alarmB == true && minuteButton == true){
  301. changeAlarm(3);
  302. }
  303. if(alarmB == true && hourButton == true){
  304. changeAlarm(4);
  305. }
  306. }
  307.  
  308. void playAlarm(){
  309. if(buzzerCounter < 20){
  310. tone(buzzerPin, 100);
  311. }
  312. if(buzzerCounter > 20){
  313. noTone(buzzerPin);
  314. }
  315. if(buzzerCounter > 40){
  316. buzzerCounter = 0;
  317. }
  318. buzzerCounter++;
  319. }
  320.  
  321.  
  322.  
  323.  
  324. void loop () {
  325. checkInput();
  326.  
  327. DateTime now = rtc.now();
  328. h = now.hour();
  329. m = now.minute();
  330.  
  331. //what is this doing?
  332. convertHour();
  333.  
  334. if(h > 12){
  335. h = h - 12;
  336. }
  337. if(h == 0){
  338. h = 12;
  339. }
  340.  
  341. //display stuff
  342. if(alarmButton.getInput() == false){
  343. if(h / 10 != 0){
  344. segment(grd[0], h / 10);
  345. }
  346. segment(grd[1], h % 10);
  347. segment(grd[2], m / 10);
  348. segment(grd[3], m % 10);
  349. if(AM_PM == true){
  350. digitalWrite(PM_Light, HIGH);
  351. }else{
  352. digitalWrite(PM_Light, LOW);
  353. }
  354. }else{
  355. if(aH / 10 != 0){
  356. segment(grd[0], aH / 10);
  357. }
  358. segment(grd[1], aH % 10);
  359. segment(grd[2], aM / 10);
  360. segment(grd[3], aM % 10);
  361. if(alarmAM_PM == true){
  362. digitalWrite(PM_Light, HIGH);
  363. }else{
  364. digitalWrite(PM_Light, LOW);
  365. }
  366. }
  367. checkAlarmIndicator();
  368.  
  369. int snoozeChecker = snoozeButton.getInput();
  370.  
  371. if(snoozeChecker == true){
  372. soundAlarm = false;
  373. snoozeTime = 5;
  374. }
  375. if(lastTime != m && snoozeTime != 0){
  376. snoozeTime--;
  377. if(snoozeTime == 0){
  378. soundAlarm = true;
  379. }
  380. }
  381.  
  382. if(checkAlarm() == true){
  383. playAlarm();
  384. }else{
  385. noTone(buzzerPin);
  386. }
  387.  
  388. lastTime = m;
  389. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement