Advertisement
mathiasbk

Untitled

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