Go Down

Topic: TIX LED CLOCK ARDUINO (Read 2252 times) previous topic - next topic

nytro81

mmmm........magari fosse così semplice!!! ;-) dunque, posto le "modifiche" che ho apportato con il tuo suggerimento (spero senza troppi errori!!)

#include <stdio.h>
#include <string.h>
#include <DS1302.h>
#include <DateTime.h>
#include <DateTimeStrings.h>
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

uint8_t CE_PIN   = 14;
uint8_t IO_PIN   = 15;
uint8_t SCLK_PIN = 16;
int colpins[] = {2,3,4,5,6,7,8,9,10};
int rowpins[] = {12, 11, 13};
char buf[50];
char day[10];
char top;
long timer;
const long muxInterval = 20;
int mux = 0;
int x;
int timesync;
int hourlist2[3];
int hourlist[9];
int minlist2[6];
int minlist[9];
int oldminute;
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
void print_time()
{
 /* Get the current time and date from the chip */
 Time t = rtc.time();
snprintf(buf, sizeof(buf), "%s %02d-%02d-%04d %02d:%02d:%02d",
          day,
          t.date, t.mon, t.yr,
          t.hr, t.min, t.sec);}
void setup() {
 Serial.begin(19200);
 for (int pin=0; pin<9; pin++) {
   pinMode( colpins[pin], OUTPUT );
 }
 for (int pin=0; pin<3; pin++) {
   pinMode( rowpins[pin], OUTPUT );
 }
 DateTime.sync(DateTime.makeTime(0, 34, 12, 10, 4, 2010));;

}

/* void hoursplit(){
 */

void loop(){
 int hour = (t.hr);
 int minute = (t.min);
 int seconds = (t.sec);
 int newhour = 0;

 if (seconds == 0 || timesync == 1) {
   Serial.println(seconds);
  // reset
   for (int i = 0; i < 9; i++){
     minlist = 0;
     hourlist = 0;
   }
   for (int i = 0; i < 6; i++) {
     minlist2 = 0;
   }  
   for (int i = 0; i < 3; i++) {
     hourlist2 = 0;
   }  
 
 
  //math
  //------------- start new code ---- july 22 2010 ---
  if (hour == hour){
//    if (hour > 12){
//      newhour = hour - 12;
//    }
//    if (hour < 12) {
//      newhour = hour;
//    }
//    if (newhour == newhour){
//      if (newhour == 0) {
//        hourlist2[0] = 1;
//        hourlist[0] = 1;
//        hourlist[1] = 1;
//      }
//      if (newhour == 12){
//        hourlist2[0] = 1;
//        hourlist[0] = 1;
//        hourlist[1] = 1;
//      }
//      if (newhour == 11){
//        hourlist2[0] = 1;
//        hourlist[0] = 1;
//      }
//      if (newhour == 10){
//        hourlist2[0] = 1;
//        hourlist2[1] = 0;
//        hourlist2[2] = 0;
//        for (int i = 0; i < 9; i++){
//          hourlist = 0;
//        }
//      }
   if (hour > 19 && hour < 24) {
     hourlist2[0] = 1;
     hourlist2[1] = 1;

     int subhour = hour - 20;
     for (int i = 0; i < subhour; i++){
       hourlist = 1;
     }
   }
   if (hour > 9 && hour < 20) {
     hourlist2[0] = 1;
     int subhour = hour - 10;
     for (int i = 0; i < subhour; i++){
       hourlist = 1;
     }
   }
     
   if (hour > 0 && hour < 10) {
     for (int i = 0; i < hour; i++){
       hourlist = 1;
     }
   }
   if (hour == 0){  
   }
//--------------------end new code
   if (minute >= 0 and minute < 10) {
     minlist2[0] = 0;
     minlist2[1] = 0;
     minlist2[2] = 0;
     minlist2[3] = 0;
     minlist2[4] = 0;
     minlist2[5] = 0;
     minute -= 0;
     for (int i = 0; i < minute; i++){
       minlist = 1;
     }
   }
   if (minute >= 10 and minute < 20) {
     minlist2[0] = 1;
     minlist2[1] = 0;
     minlist2[2] = 0;
     minlist2[3] = 0;
     minlist2[4] = 0;
     minlist2[5] = 0;
     minute -= 10;
     for (int i = 0; i < minute; i++){
       minlist = 1;
     }
   }
   if (minute >= 20 and minute < 30) {
     minlist2[0] = 1;
     minlist2[1] = 1;
     minlist2[2] = 0;
     minlist2[3] = 0;
     minlist2[4] = 0;
     minlist2[5] = 0;
     minute -= 20;
     for (int i = 0; i < minute; i++){
       minlist = 1;
     }
   }
   if (minute >= 30 and minute < 40) {
     minlist2[0] = 1;
     minlist2[1] = 1;
     minlist2[2] = 1;
     minlist2[3] = 0;
     minlist2[4] = 0;
     minlist2[5] = 0;
     minute -= 30;
     for (int i = 0; i < minute; i++){
       minlist = 1;
     }
   }
   if (minute >= 40 and minute < 50) {
     minlist2[0] = 1;
     minlist2[1] = 1;
     minlist2[2] = 1;
     minlist2[3] = 1;
     minlist2[4] = 0;
     minlist2[5] = 0;
     minute -= 40;
     for (int i = 0; i < minute; i++){
       minlist = 1;
     }
   }
   if (minute >= 50 and minute < 60) {
     minlist2[0] = 1;
     minlist2[1] = 1;
     minlist2[2] = 1;
     minlist2[3] = 1;
     minlist2[4] = 1;
     minlist2[5] = 0;
     minute -= 50;
     for (int i = 0; i < minute; i++){
       minlist = 1;
     }
   }
 }
timesync = 0;
}

// Random function
if (seconds == 0 || seconds ==  5 || seconds ==  10 || seconds == 15 || seconds ==  20 || seconds ==  25 || seconds == 30 || seconds ==  35 || seconds ==  40 || seconds ==  45 || seconds ==  50 || seconds == 55){
 if (x == 1){
   x = 0;
   for (int a=0; a<9; a++){
     int r = random(a,9);
     int temp = minlist[a];
     minlist[a] = minlist[r];
     minlist[r] = temp;

   }
   for (int a=0; a<9; a++){
     int r = random(a,9);
     int temp = hourlist[a];
     hourlist[a] = hourlist[r];
     hourlist[r] = temp;
     }
   for (int a=0; a<6; a++){
     int r = random(a,6);
     int temp = minlist2[a];
     minlist2[a] = minlist2[r];
     minlist2[r] = temp;
   }
   for (int a=0; a<3; a++){
     int r = random(a,3);
     int temp = hourlist2[a];
     hourlist2[a] = hourlist2[r];
     hourlist2[r] = temp;
   }
 }
}
// random off for 10 seconds
if (seconds == 1 || seconds == 6 || seconds == 11 || seconds ==  16 || seconds == 21 || seconds == 26 || seconds == 31 || seconds == 36 || seconds == 41 || seconds == 46 || seconds == 51 || seconds == 56){
 x = 1;
}

// draw



int rowone[] = {hourlist2[0], hourlist[0], hourlist[1], hourlist[2], minlist2[0], minlist2[1], minlist[0], minlist[1], minlist[2]};
int rowtwo[] = {hourlist2[1], hourlist[3], hourlist[4], hourlist[5], minlist2[2], minlist2[3], minlist[3], minlist[4], minlist[5]};
int rowthree[] = {hourlist2[2], hourlist[6], hourlist[7], hourlist[8], minlist2[4], minlist2[5], minlist[6], minlist[7], minlist[8]};

if (millis() - timer < muxInterval)
 {
   timer = millis();
   mux++;
   if (mux > 2){
     mux = 0;
   }
   for (int i = 0; i <=8; i++){
     digitalWrite(colpins, LOW);

   }
   digitalWrite(rowpins[0], LOW);
   digitalWrite(rowpins[1], LOW);
   digitalWrite(rowpins[2], LOW);
   
     
  if (mux == 0) {
     for (int i = 8; i >= 0; i--) {
       if (rowone == 1){
         digitalWrite(colpins, HIGH);
         digitalWrite(rowpins[0], HIGH);
       }
     }
   }
 

 if (mux == 1) {
   for (int i = 8; i >= 0; i--) {
     if (rowthree == 1){
       digitalWrite(colpins, HIGH);
       digitalWrite(rowpins[2], HIGH);
     }
   }
 }

 if (mux == 2) {
   for (int i = 8; i >= 0; i--) {
     if (rowtwo == 1){
       digitalWrite(colpins, HIGH);
       digitalWrite(rowpins[1], HIGH);
     }
   }
 }
}
 

// Time
unsigned long  prevtime;
 if( getPCtime()) {  // try to get time sync from pc
   Serial.print("Clock synced at: ");
   Serial.println(DateTime.now(),DEC);
 }
 if(DateTime.available()) { // update clocks if time has been synced
   digitalWrite(13,LOW);  // first flash the LED
   prevtime = DateTime.now();

   DateTime.available(); //refresh the Date and time properties


 }
}


boolean getPCtime() {
 // 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
   if( Serial.read() == TIME_HEADER ) {        
     time_t pctime = 0;
     for(int i=0; i < TIME_MSG_LEN -1; i++){  
       char c= Serial.read();
       top = c;        
       if( c >= '0' && c <= '9'){  
         pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
       }
     }
     timesync = 1;  
     DateTime.sync(pctime);   // Sync Arduino clock to the time received on the serial port
     return true;   // return true if time message received on the serial port
   }  
 }
 return false;  //if no message return false
}


Ho inserito le librerie x la comunicazione col ds1302 e modificato i campi delle variabili secondi, minuti ed ore, xò mi da un errore, " In function 'void loop()':
error: 't' was not declared in this scope", come se non riconoscesse la variabile t.hr sulla riga

   int hour = (t.hr);


che ne pensi?! ps grazie veramente x il tuo aiuto, mi sei molto prezioso in questa fase!!!  ;D

nytro81

Orologio completato e perfettamente funzionante, con funzione di mantenimento dell'ora anche in caso di spegnimento e riaccensione!!! Posto qua il codice modificato alla perfezione dal mitico uwefed per l'integrazione del ds1302

#include <stdio.h>
#include <string.h>
#include <DS1302.h>
//#include <DateTime.h>
//#include <DateTimeStrings.h>
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  255   // Header tag for serial time sync message

uint8_t CE_PIN   = 14;
uint8_t IO_PIN   = 15;
uint8_t SCLK_PIN = 16;
int colpins[] = {2,3,4,5,6,7,8,9,10};
int rowpins[] = {12, 11, 13};
char buf[50];
char day[10];
char top;
long timer;
const long muxInterval = 20;
int mux = 0;
int x;
int timesync;
int hourlist2[3];
int hourlist[9];
int minlist2[6];
int minlist[9];
int oldminute;

DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);   //definisce pin per collegamento RTC
/*
void print_time()
{
// Get the current time and date from the chip
Time t = rtc.time();
snprintf(buf, sizeof(buf), "%s %02d-%02d-%04d %02d:%02d:%02d",   // estrae i diversi valori dalla stringa del che il RTC da.
         day,
         t.date, t.mon, t.yr,
         t.hr, t.min, t.sec);
}
*/          
void setup() {
Serial.begin(19200);
 
for (int pin=0; pin<9; pin++) {
  pinMode( colpins[pin], OUTPUT );
}
for (int pin=0; pin<3; pin++) {
  pinMode( rowpins[pin], OUTPUT );
}
/* setta l orologio RTC
 rtc.write_protect(false);
 rtc.halt(false);
 Time t(3, 5, 19, 2010, 21, 16, 37);
 
 rtc.time(t);
 */
}

void loop(){
 
Time t = rtc.time();   //prende l' ora dal RTC
int hour = t.hr;
int minute = t.min;
int seconds = t.sec;
int newhour = 0;

if (seconds == 0 || timesync == 1) {
  Serial.println(seconds);
 // reset
  for (int i = 0; i < 9; i++){
    minlist = 0;
    hourlist = 0;
  }
  for (int i = 0; i < 6; i++) {
    minlist2 = 0;
  }  
  for (int i = 0; i < 3; i++) {
    hourlist2 = 0;
  }  

  if (hour > 19 && hour < 24) {
    hourlist2[0] = 1;
    hourlist2[1] = 1;

    int subhour = hour - 20;
    for (int i = 0; i < subhour; i++){
      hourlist = 1;
    }
  }
  if (hour > 9 && hour < 20) {
    hourlist2[0] = 1;
    int subhour = hour - 10;
    for (int i = 0; i < subhour; i++){
      hourlist = 1;
    }
  }
   
  if (hour > 0 && hour < 10) {
    for (int i = 0; i < hour; i++){
      hourlist = 1;
    }
  }
  if (hour == 0){  
  }
 
//--------------------end new code
  if (minute >= 0 and minute < 10) {
    minlist2[0] = 0;
    minlist2[1] = 0;
    minlist2[2] = 0;
    minlist2[3] = 0;
    minlist2[4] = 0;
    minlist2[5] = 0;
    minute -= 0;
    for (int i = 0; i < minute; i++){
      minlist = 1;
    }
  }
  if (minute >= 10 and minute < 20) {
    minlist2[0] = 1;
    minlist2[1] = 0;
    minlist2[2] = 0;
    minlist2[3] = 0;
    minlist2[4] = 0;
    minlist2[5] = 0;
    minute -= 10;
    for (int i = 0; i < minute; i++){
      minlist = 1;
    }
  }
  if (minute >= 20 and minute < 30) {
    minlist2[0] = 1;
    minlist2[1] = 1;
    minlist2[2] = 0;
    minlist2[3] = 0;
    minlist2[4] = 0;
    minlist2[5] = 0;
    minute -= 20;
    for (int i = 0; i < minute; i++){
      minlist = 1;
    }
  }
  if (minute >= 30 and minute < 40) {
    minlist2[0] = 1;
    minlist2[1] = 1;
    minlist2[2] = 1;
    minlist2[3] = 0;
    minlist2[4] = 0;
    minlist2[5] = 0;
    minute -= 30;
    for (int i = 0; i < minute; i++){
      minlist = 1;
    }
  }
  if (minute >= 40 and minute < 50) {
    minlist2[0] = 1;
    minlist2[1] = 1;
    minlist2[2] = 1;
    minlist2[3] = 1;
    minlist2[4] = 0;
    minlist2[5] = 0;
    minute -= 40;
    for (int i = 0; i < minute; i++){
      minlist = 1;
    }
  }
  if (minute >= 50 and minute < 60) {
    minlist2[0] = 1;
    minlist2[1] = 1;
    minlist2[2] = 1;
    minlist2[3] = 1;
    minlist2[4] = 1;
    minlist2[5] = 0;
    minute -= 50;
    for (int i = 0; i < minute; i++){
      minlist = 1;
    }
  }
}
timesync = 0;


// Random function
if ((seconds == 0) || (seconds ==  5) || (seconds ==  10) || (seconds == 15) || (seconds ==  20) || (seconds ==  25) || (seconds == 30) || (seconds ==  35) || (seconds ==  40) || (seconds ==  45) || (seconds ==  50) || (seconds == 55))
{
if (x == 1){
  x = 0;
  for (int a=0; a<9; a++){
    int r = random(a,9);
    int temp = minlist[a];
    minlist[a] = minlist[r];
    minlist[r] = temp;

  }
  for (int a=0; a<9; a++){
    int r = random(a,9);
    int temp = hourlist[a];
    hourlist[a] = hourlist[r];
    hourlist[r] = temp;
    }
  for (int a=0; a<6; a++){
    int r = random(a,6);
    int temp = minlist2[a];
    minlist2[a] = minlist2[r];
    minlist2[r] = temp;
  }
  for (int a=0; a<3; a++){
    int r = random(a,3);
    int temp = hourlist2[a];
    hourlist2[a] = hourlist2[r];
    hourlist2[r] = temp;
  }
}
}
// random off for 10 seconds
if (seconds == 1 || seconds == 6 || seconds == 11 || seconds ==  16 || seconds == 21 || seconds == 26 || seconds == 31 || seconds == 36 || seconds == 41 || seconds == 46 || seconds == 51 || seconds == 56){
x = 1;
}

// draw

int rowone[] = {hourlist2[0], hourlist[0], hourlist[1], hourlist[2], minlist2[0], minlist2[1], minlist[0], minlist[1], minlist[2]};
int rowtwo[] = {hourlist2[1], hourlist[3], hourlist[4], hourlist[5], minlist2[2], minlist2[3], minlist[3], minlist[4], minlist[5]};
int rowthree[] = {hourlist2[2], hourlist[6], hourlist[7], hourlist[8], minlist2[4], minlist2[5], minlist[6], minlist[7], minlist[8]};

if (millis() - timer < muxInterval)
{
  timer = millis();
  mux++;
  if (mux > 2){
    mux = 0;
  }
  for (int i = 0; i <=8; i++){
    digitalWrite(colpins, LOW);

  }
  digitalWrite(rowpins[0], LOW);
  digitalWrite(rowpins[1], LOW);
  digitalWrite(rowpins[2], LOW);
 
   
 if (mux == 0) {
    for (int i = 8; i >= 0; i--) {
      if (rowone == 1){
        digitalWrite(colpins, HIGH);
        digitalWrite(rowpins[0], HIGH);
      }
    }
  }


if (mux == 1) {
  for (int i = 8; i >= 0; i--) {
    if (rowthree == 1){
      digitalWrite(colpins, HIGH);
      digitalWrite(rowpins[2], HIGH);
    }
  }
}

if (mux == 2) {
  for (int i = 8; i >= 0; i--) {
    if (rowtwo == 1){
      digitalWrite(colpins, HIGH);
      digitalWrite(rowpins[1], HIGH);
    }
  }
}
}


// Time
/*
unsigned long  prevtime;
if( getPCtime()) {  // try to get time sync from pc
  Serial.print("Clock synced at: ");
  Serial.println(DateTime.now(),DEC);
}
if(DateTime.available()) { // update clocks if time has been synced
  digitalWrite(13,LOW);  // first flash the LED
  prevtime = DateTime.now();

  DateTime.available(); //refresh the Date and time properties
}
}
*/
/*
boolean getPCtime() {
// 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
  if( Serial.read() == TIME_HEADER ) {        
    time_t pctime = 0;
    for(int i=0; i < TIME_MSG_LEN -1; i++){  
      char c= Serial.read();
      top = c;        
      if( c >= '0' && c <= '9'){  
        pctime = (10 * pctime) + (c - '0') ; // convert digits to a number    
      }
    }
    timesync = 1;  
    DateTime.sync(pctime);   // Sync Arduino clock to the time received on the serial port
    return true;   // return true if time message received on the serial port
  }  
}
return false;  //if no message return false
*/
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy