Advertisement
Guest User

Untitled

a guest
Mar 6th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. #include <ShiftPWM.h> // include ShiftPWM.h after setting the pins!
  4. #include <Wire.h>
  5. #include <EEPROM.h>
  6.  
  7. #define DS1307_I2C_ADDRESS 0x68 // This is the I2C address
  8.  
  9. const int ShiftPWM_latchPin=8;
  10. const bool ShiftPWM_invertOutputs = 0; // if invertOutputs is 1, outputs will be active low. Usefull for common anode RGB led's.
  11.  
  12. unsigned char maxBrightness = 255;
  13. unsigned char pwmFrequency = 75;
  14. int numRegisters = 3;
  15.  
  16. byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  17. int command = 0; // This is the command char, in ascii form, sent from the serial port
  18.  
  19. //My LED setup...
  20. //
  21. //0 - UV
  22. //1 - Blue
  23. //2 - Red
  24. //3 - Red
  25. //4 - Yellow
  26. //5 - Yellow
  27. //6 - White
  28. //7 - White
  29. //
  30. //8,9,10,11,12,13,14,15 - White
  31. //
  32. //16 - UV
  33. //17 - UV
  34. //18 - Blue
  35. //19 - Red
  36. //20 - Red
  37. //21 - Yellow
  38. //22 - White
  39. //23 - White
  40.  
  41. int num_led_steps = 24;
  42. int led_on_order[24] = {16, 0,17, 1,18, 2,19, 3,20, 4,21, 5, 8, 9, 6,22, 7,23,10,11,12,13,14,15};
  43. int led_off_order[24] = {-1,-1,-1,-1,-1,-1,-1,-1, 1,18,-1,-1, 2,19, 3,20,-1,-1,-1,-1,-1,-1,-1,-1};
  44.  
  45.  
  46. int alarm_hour = 0;
  47. int alarm_minute = 0;
  48. int days_to_alarm[7] = {0,0,0,0,0,0,0};
  49. int alarm_current_position = 0;
  50.  
  51. long last_action_time = 0;
  52.  
  53. int alarm_duration = 24; //number of minutes for alarm to take.
  54. int time_between_up = 1000;
  55.  
  56. long now = 0;
  57.  
  58. int i,j;
  59.  
  60. void setup()
  61. {
  62. //Set up ShiftPWM
  63. pinMode(ShiftPWM_latchPin, OUTPUT);
  64. SPI.setBitOrder(LSBFIRST);
  65. // SPI_CLOCK_DIV2 is only a tiny bit faster in sending out the last byte.
  66. // SPI transfer and calculations overlap for the other bytes.
  67. SPI.setClockDivider(SPI_CLOCK_DIV4);
  68. SPI.begin();
  69.  
  70. //Begin Wire protocol for reading from clock chip
  71. Wire.begin();
  72.  
  73. //Serial so you can read and report the time and alarm.
  74. Serial.begin(9600);
  75.  
  76. //read the stored alarm time.
  77. alarm_hour = EEPROM.read(0);
  78. alarm_minute = EEPROM.read(1);
  79. days_to_alarm[0] = EEPROM.read(2);
  80. days_to_alarm[1] = EEPROM.read(3);
  81. days_to_alarm[2] = EEPROM.read(4);
  82. days_to_alarm[3] = EEPROM.read(5);
  83. days_to_alarm[4] = EEPROM.read(6);
  84. days_to_alarm[5] = EEPROM.read(7);
  85. days_to_alarm[6] = EEPROM.read(8);
  86.  
  87. //Set up the shiftPWM constants
  88. ShiftPWM.SetAmountOfRegisters(numRegisters);
  89. ShiftPWM.Start(pwmFrequency,maxBrightness);
  90.  
  91. //Everything off.
  92. ShiftPWM.SetAll(0);
  93.  
  94. long calc = (long)(alarm_duration * 60l * 1000l);
  95. calc = calc / ((long)(num_led_steps * maxBrightness));
  96.  
  97. time_between_up = (int)calc;
  98.  
  99. Serial.print("Value Shift Amount: ");
  100. Serial.println(time_between_up);
  101.  
  102. }
  103.  
  104. void loop()
  105. {
  106. now = millis(); //get the time now.
  107.  
  108. if (now - last_action_time > 1000) //read and deal with the alarm only every second.
  109. {
  110. last_action_time = now;
  111.  
  112. getDateDs1307(); //sets the global date/time constants
  113.  
  114. if (days_to_alarm[(dayOfWeek-1)] == 1)
  115. {
  116. if ((hour*60+minute) == ((alarm_hour*60+alarm_minute)-alarm_duration)) //if we're an appropriate number of minutes before the alarm is to be finished, start the alarm.
  117. {
  118. alarm_go();
  119. }
  120. if ((hour*60+minute) == ((alarm_hour*60+alarm_minute)+120)) //turn the lights off after 2 hours.
  121. {
  122. for (int i=maxBrightness; i>=0; i--)
  123. {
  124. ShiftPWM.SetAll(i);
  125. delay(100);
  126. }
  127. }
  128. }
  129. }
  130.  
  131. /**
  132. * With this code you can set the date/time, retreive the date/time and use the extra memory of an RTC DS1307 chip.
  133. * Serial Communication method with the Arduino that utilizes a leading CHAR for each command described below.
  134. * Commands:
  135. * T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) - T Sets the date of the RTC DS1307 Chip.
  136. * Example to set the time for 02-Feb-09 @ 19:57:11 for the 3 day of the week, use this command - T1157193020209
  137. * A(00-59)(00-23)(0-1)(0-1)(0-1)(0-1)(0-1)(0-1)(0-1)
  138. * Set alarm to the alarm time. each 0/1 after the hour is the day of the week to alarm or not, sunday-saturday.
  139. */
  140. if (Serial.available()) { // Look for char in serial que and process if found
  141. command = Serial.read();
  142. if (command == 84) { //If command = "T" Set Date
  143. setDateDs1307();
  144. }
  145. if (command == 65) {
  146. setAlarm();
  147. }
  148. }
  149.  
  150. command = 0; // reset command
  151. delay(100);
  152.  
  153. }
  154.  
  155. void alarm_go()
  156. {
  157. for(i = 0; i < num_led_steps; i++)
  158. {
  159. for (j = 0; j <= maxBrightness; j++)
  160. {
  161. ShiftPWM.SetOne(led_on_order[i],j); //follow the order of turning lights on
  162. if (led_off_order[i] != -1) //and the ones you want to turn off.
  163. {
  164. ShiftPWM.SetOne(led_off_order[i],maxBrightness-j);
  165. }
  166. delay(time_between_up);
  167. }
  168. }
  169. }
  170.  
  171. // Convert normal decimal numbers to binary coded decimal
  172. byte decToBcd(byte val)
  173. {
  174. return ( (val/10*16) + (val%10) );
  175. }
  176.  
  177. // Convert binary coded decimal to normal decimal numbers
  178. byte bcdToDec(byte val)
  179. {
  180. return ( (val/16*10) + (val%16) );
  181. }
  182.  
  183.  
  184. void getDateDs1307()
  185. {
  186. // Reset the register pointer
  187. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  188. Wire.send(0x00);
  189. Wire.endTransmission();
  190.  
  191. Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  192.  
  193. // A few of these need masks because certain bits are control bits
  194. second = bcdToDec(Wire.receive() & 0x7f);
  195. minute = bcdToDec(Wire.receive());
  196. hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
  197. dayOfWeek = bcdToDec(Wire.receive());
  198. dayOfMonth = bcdToDec(Wire.receive());
  199. month = bcdToDec(Wire.receive());
  200. year = bcdToDec(Wire.receive());
  201.  
  202. Serial.print(">");
  203. Serial.print(hour, DEC);
  204. Serial.print(":");
  205. Serial.print(minute, DEC);
  206. Serial.print(":");
  207. Serial.print(second, DEC);
  208. Serial.print(" ");
  209. Serial.print(month, DEC);
  210. Serial.print("/");
  211. Serial.print(dayOfMonth, DEC);
  212. Serial.print("/");
  213. Serial.print(year, DEC);
  214. Serial.print(",");
  215. Serial.print(alarm_hour);
  216. Serial.print(":");
  217. Serial.print(alarm_minute);
  218. Serial.print(",");
  219. for (i=0;i<7;i++)
  220. {
  221. if (days_to_alarm[i] == 1)
  222. Serial.print("*");
  223. else
  224. Serial.print("-");
  225. }
  226. Serial.println();
  227. }
  228.  
  229. void setDateDs1307()
  230. {
  231.  
  232. second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.
  233. minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  234. hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  235. dayOfWeek = (byte) (Serial.read() - 48);
  236. dayOfMonth = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  237. month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  238. year= (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  239. Wire.beginTransmission(DS1307_I2C_ADDRESS);
  240. Wire.send(0x00);
  241. Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
  242. Wire.send(decToBcd(minute));
  243. Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
  244. // bit 6 (also need to change readDateDs1307)
  245. Wire.send(decToBcd(dayOfWeek));
  246. Wire.send(decToBcd(dayOfMonth));
  247. Wire.send(decToBcd(month));
  248. Wire.send(decToBcd(year));
  249. Wire.endTransmission();
  250. }
  251.  
  252. void setAlarm()
  253. {
  254. alarm_minute = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  255. alarm_hour = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48));
  256. days_to_alarm[0] = (byte) (Serial.read() - 48);
  257. days_to_alarm[1] = (byte) (Serial.read() - 48);
  258. days_to_alarm[2] = (byte) (Serial.read() - 48);
  259. days_to_alarm[3] = (byte) (Serial.read() - 48);
  260. days_to_alarm[4] = (byte) (Serial.read() - 48);
  261. days_to_alarm[5] = (byte) (Serial.read() - 48);
  262. days_to_alarm[6] = (byte) (Serial.read() - 48);
  263. EEPROM.write(0,alarm_hour);
  264. EEPROM.write(1,alarm_minute);
  265. EEPROM.write(2,days_to_alarm[0]);
  266. EEPROM.write(3,days_to_alarm[1]);
  267. EEPROM.write(4,days_to_alarm[2]);
  268. EEPROM.write(5,days_to_alarm[3]);
  269. EEPROM.write(6,days_to_alarm[4]);
  270. EEPROM.write(7,days_to_alarm[5]);
  271. EEPROM.write(8,days_to_alarm[6]);
  272.  
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement