Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Time.h>
- #include <LiquidCrystal.h> //include code for LCD
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //initialiserer LCD Display
- #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
- #define TIME_HEADER 'T' // Header tag for serial time sync message
- #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
- int redPin = 10; // Red LED, connected to digital pin 9 //blå
- int grnPin = 9; // Green LED, connected to digital pin 10 //Gul
- int bluPin = 6; // Blue LED, connected to digital pin 11 //HVIT
- // Color arrays
- int black[3] = { 0, 0, 0 };
- int white[3] = { 100, 100, 100 };
- int red[3] = { 100, 0, 0 };
- int green[3] = { 0, 100, 0 };
- int blue[3] = { 0, 0, 100 };
- int yellow[3] = { 40, 95, 0 };
- int dimWhite[3] = { 30, 30, 30 };
- // etc.
- // Set initial color
- int redVal = black[0];
- int grnVal = black[1];
- int bluVal = black[2];
- //int wait = 50; // 10ms internal crossFade delay; increase for slower fades
- int bluewait = 20;
- int yellowwait = 1;
- int whitewait = 1; //wait for the wait color to fade, in MS
- int currentcolor = 0; //current color. 1 = red, 2 = green, 3 = blue
- int hold = 1; // Optional hold when a color is complete, before the next crossFade
- int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
- int j = 0; // Loop counter for repeat
- // Initialize color variables
- int prevR = redVal;
- int prevG = grnVal;
- int prevB = bluVal;
- // Set up the LED outputs
- void setup()
- {
- pinMode(redPin, OUTPUT); // sets the pins as output
- pinMode(grnPin, OUTPUT);
- pinMode(bluPin, OUTPUT);
- Serial.begin(9600);
- setSyncProvider( requestSync); //set function to call when sync required
- Serial.println("Waiting for sync message");
- lcd.begin(16, 2);
- }
- // Main program: list the order of crossfades
- void loop()
- {
- //START: serial sync
- LCD();
- if(Serial.available() )
- {
- processSyncMessage();
- }
- if(timeStatus()!= timeNotSet)
- {
- digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
- digitalClockDisplay();
- }
- //STOP: serial sync
- // crossFade(red);
- // crossFade(green);
- // crossFade(blue);
- // crossFade(yellow);
- //START: endrer lyset basert på klokken
- 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))
- {
- currentcolor = 1;
- crossFade(red);
- }
- 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))
- {
- currentcolor = 2;
- crossFade(green);
- }
- if((hour() == 10) || (hour() == 11) || (hour() == 12) || (hour() == 13) || (hour() == 14) || (hour() == 15) || (hour() == 16) || (hour() == 17) || (hour() == 18))
- {
- currentcolor = 3;
- crossFade(blue);
- }
- //SLUTT: endrer lyset basert på klokken
- delay (500);
- if (repeat) { // Do we loop a finite number of times?
- j += 1;
- if (j >= repeat) { // Are we there yet?
- exit(j); // If so, stop.
- }
- }
- }
- int calculateStep(int prevValue, int endValue) {
- int step = endValue - prevValue; // What's the overall gap?
- if (step) { // If its non-zero,
- step = 1020/step; // divide by 1020hvo
- }
- return step;
- }
- int calculateVal(int step, int val, int i) {
- LCD();
- if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
- if (step > 0) { // increment the value if step is positive...
- val += 1;
- }
- else if (step < 0) { // ...or decrement it if step is negative
- val -= 1;
- }
- }
- // Defensive driving: make sure val stays in the range 0-255
- if (val > 255) {
- val = 255;
- }
- else if (val < 0) {
- val = 0;
- }
- return val;
- }
- void crossFade(int color[3]) {
- // Convert to 0-255
- int R = (color[0] * 255) / 100;
- int G = (color[1] * 255) / 100;
- int B = (color[2] * 255) / 100;
- int stepR = calculateStep(prevR, R);
- int stepG = calculateStep(prevG, G);
- int stepB = calculateStep(prevB, B);
- for (int i = 0; i <= 1020; i++) {
- redVal = calculateVal(stepR, redVal, i);
- grnVal = calculateVal(stepG, grnVal, i);
- bluVal = calculateVal(stepB, bluVal, i);
- analogWrite(redPin, redVal); // Write current values to LED pins
- analogWrite(grnPin, grnVal);
- analogWrite(bluPin, bluVal);
- /*
- if(currentcolor = 1)
- {
- delay(bluewait);
- }
- if(currentcolor = 2)
- {
- delay(yellowwait);
- }
- if(currentcolor = 3)
- {
- delay(whitewait);
- }
- */
- delay(bluewait); // Pause for 'wait' milliseconds before resuming the loop
- }
- // Update current values for next loop
- prevR = redVal;
- prevG = grnVal;
- prevB = bluVal;
- delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
- }
- void digitalClockDisplay(){
- // digital clock display of the time
- Serial.print(hour());
- printDigits(minute());
- printDigits(second());
- Serial.print(" ");
- Serial.print(day());
- Serial.print(" ");
- Serial.print(month());
- Serial.print(" ");
- Serial.print(year());
- Serial.println();
- }
- void printDigits(int digits){
- // utility function for digital clock display: prints preceding colon and leading 0
- Serial.print(":");
- if(digits < 10)
- Serial.print('0');
- Serial.print(digits);
- }
- void processSyncMessage() {
- // if time sync available from serial port, update time and return true
- while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
- char c = Serial.read() ;
- Serial.print(c);
- if( c == TIME_HEADER ) {
- time_t pctime = 0;
- for(int i=0; i < TIME_MSG_LEN -1; i++){
- c = Serial.read();
- if( c >= '0' && c <= '9'){
- pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
- }
- }
- setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- }
- }
- time_t requestSync()
- {
- Serial.print(TIME_REQUEST,BYTE);
- return 0; // the time will be sent later in response to serial mesg
- }
- void LCD()
- {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Temp: 20*C");
- lcd.setCursor(3, 3);
- lcd.print("ambystima mexicanum");
- lcd.setCursor(5,4);
- lcd.print("Ohm'r & Voltrata");
- lcd.setCursor(13, 0);
- lcd.print(hour());
- lcd.print(":");
- lcd.print(minute());
- //}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement