Advertisement
mathiasbk

Untitled

Mar 4th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.84 KB | None | 0 0
  1. #include <Time.h>  
  2.  
  3. #define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
  4. #define TIME_HEADER  'T'   // Header tag for serial time sync message
  5. #define TIME_REQUEST  7    // ASCII bell character requests a time sync message
  6.  
  7.  
  8. int redPin = 11;   // Red LED,   connected to digital pin 9
  9. int grnPin = 10;  // Green LED, connected to digital pin 10
  10. int bluPin = 9;  // Blue LED,  connected to digital pin 11
  11.  
  12. // Color arrays
  13. int black[3]  = { 0, 0, 0 };
  14. int white[3]  = { 100, 100, 100 };
  15. int red[3]    = { 100, 0, 0 };
  16. int green[3]  = { 0, 100, 0 };
  17. int blue[3]   = { 0, 0, 100 };
  18. int yellow[3] = { 40, 95, 0 };
  19. int dimWhite[3] = { 30, 30, 30 };
  20. // etc.
  21.  
  22. // Set initial color
  23. int redVal = black[0];
  24. int grnVal = black[1];
  25. int bluVal = black[2];
  26.  
  27. int wait = 50;      // 10ms internal crossFade delay; increase for slower fades
  28. int hold = 1;       // Optional hold when a color is complete, before the next crossFade
  29. int repeat = 0;     // How many times should we loop before stopping? (0 for no stop)
  30. int j = 0;          // Loop counter for repeat
  31.  
  32. // Initialize color variables
  33. int prevR = redVal;
  34. int prevG = grnVal;
  35. int prevB = bluVal;
  36.  
  37. // Set up the LED outputs
  38. void setup()
  39. {
  40.   pinMode(redPin, OUTPUT);   // sets the pins as output
  41.   pinMode(grnPin, OUTPUT);  
  42.   pinMode(bluPin, OUTPUT);
  43.  
  44.   Serial.begin(9600);
  45.   setSyncProvider( requestSync);  //set function to call when sync required
  46.   Serial.println("Waiting for sync message");
  47. }
  48.  
  49. // Main program: list the order of crossfades
  50. void loop()
  51. {
  52.  
  53. //START: serial sync
  54.  
  55. if(Serial.available() )
  56.   {
  57.     processSyncMessage();
  58.   }
  59.   if(timeStatus()!= timeNotSet)  
  60.   {
  61.     digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh  
  62.     digitalClockDisplay();  
  63.   }
  64.  
  65. //STOP: serial sync
  66. //  crossFade(red);
  67. //  crossFade(green);
  68. //  crossFade(blue);
  69. //  crossFade(yellow);
  70. //START: endrer lyset basert på klokken
  71. if((hour() == 20) || (hour() == 21) || (hour() == 22) || (hour() == 23) || (hour() == 24) || (hour() == 00) || (hour() == 01) || (hour() == 02) || (hour() == 03) || (hour() == 04)  || (hour() == 05) || (hour() == 06))
  72. {
  73.   crossFade(red);
  74. }
  75.  
  76. if((hour() == 05) || (hour() == 06) || (hour() == 07) || (hour() == 8) || (hour() == 9) || (hour() == 10) || (hour() == 11) || (hour() == 12) || (hour() == 13) || (hour() == 17) || (hour() == 18) || (hour() == 19) || (hour() == 20) || (hour() == 21))
  77. {
  78.   crossFade(green);
  79. }
  80.  
  81. if((hour() == 10) || (hour() == 11) || (hour() == 12) || (hour() == 13) || (hour() == 14) || (hour() == 15) || (hour() == 16) || (hour() == 17) || (hour() == 18))
  82. {
  83.   crossFade(blue);
  84. }
  85.  
  86. //SLUTT: endrer lyset basert på klokken
  87. delay (500);
  88.  
  89.   if (repeat) { // Do we loop a finite number of times?
  90.     j += 1;
  91.     if (j >= repeat) { // Are we there yet?
  92.       exit(j);         // If so, stop.
  93.     }
  94.   }
  95. }
  96.  
  97.  
  98. int calculateStep(int prevValue, int endValue) {
  99.   int step = endValue - prevValue; // What's the overall gap?
  100.   if (step) {                      // If its non-zero,
  101.     step = 1020/step;              //   divide by 1020hvo
  102.   }
  103.   return step;
  104. }
  105.  
  106.  
  107.  
  108. int calculateVal(int step, int val, int i) {
  109.  
  110.   if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
  111.     if (step > 0) {              //   increment the value if step is positive...
  112.       val += 1;          
  113.     }
  114.     else if (step < 0) {         //   ...or decrement it if step is negative
  115.       val -= 1;
  116.     }
  117.   }
  118.   // Defensive driving: make sure val stays in the range 0-255
  119.   if (val > 255) {
  120.     val = 255;
  121.   }
  122.   else if (val < 0) {
  123.     val = 0;
  124.   }
  125.   return val;
  126. }
  127.  
  128.  
  129.  
  130. void crossFade(int color[3]) {
  131.   // Convert to 0-255
  132.   int R = (color[0] * 255) / 100;
  133.   int G = (color[1] * 255) / 100;
  134.   int B = (color[2] * 255) / 100;
  135.  
  136.   int stepR = calculateStep(prevR, R);
  137.   int stepG = calculateStep(prevG, G);
  138.   int stepB = calculateStep(prevB, B);
  139.  
  140.   for (int i = 0; i <= 1020; i++) {
  141.     redVal = calculateVal(stepR, redVal, i);
  142.     grnVal = calculateVal(stepG, grnVal, i);
  143.     bluVal = calculateVal(stepB, bluVal, i);
  144.  
  145.     analogWrite(redPin, redVal);   // Write current values to LED pins
  146.     analogWrite(grnPin, grnVal);      
  147.     analogWrite(bluPin, bluVal);
  148.  
  149.     delay(wait); // Pause for 'wait' milliseconds before resuming the loop
  150.   }
  151.   // Update current values for next loop
  152.   prevR = redVal;
  153.   prevG = grnVal;
  154.   prevB = bluVal;
  155.   delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
  156. }
  157.  
  158. void digitalClockDisplay(){
  159.   // digital clock display of the time
  160.   Serial.print(hour());
  161.   printDigits(minute());
  162.   printDigits(second());
  163.   Serial.print(" ");
  164.   Serial.print(day());
  165.   Serial.print(" ");
  166.   Serial.print(month());
  167.   Serial.print(" ");
  168.   Serial.print(year());
  169.   Serial.println();
  170.  
  171.  
  172. }
  173.  
  174. void printDigits(int digits){
  175.   // utility function for digital clock display: prints preceding colon and leading 0
  176.   Serial.print(":");
  177.   if(digits < 10)
  178.     Serial.print('0');
  179.   Serial.print(digits);
  180. }
  181.  
  182. void processSyncMessage() {
  183.   // if time sync available from serial port, update time and return true
  184.   while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
  185.     char c = Serial.read() ;
  186.     Serial.print(c);  
  187.    
  188.     if( c == TIME_HEADER ) {      
  189.       time_t pctime = 0;
  190.       for(int i=0; i < TIME_MSG_LEN -1; i++){  
  191.         c = Serial.read();          
  192.         if( c >= '0' && c <= '9'){  
  193.           pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
  194.         }
  195.       }  
  196.       setTime(pctime);   // Sync Arduino clock to the time received on the serial port
  197.     }  
  198.   }
  199. }
  200.  
  201. time_t requestSync()
  202. {
  203.   Serial.print(TIME_REQUEST,BYTE);  
  204.   return 0; // the time will be sent later in response to serial mesg
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement