Advertisement
mathiasbk

Klokkefade test

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