Sincronizar o relógio do PC com o Arduino

Boas pessoal!

Eu preciso de criar uma espécie de calendário no arduino. Para tal, acho que é necessário sincronizar o arduino com o relógio do PC mas, após instalar a livraria DateTime e correr o exemplo obtive erros de funções não declaradas.

No fundo preciso que o arduino me retorne numa variável o dia da semana e a hora mas ainda não consegui encontrar um bom método para o fazer..Alguém me consegue ajudar com isto?

Obrigado

Tens um relógio de tempo real ligado ao Arduino?

Que erros é que apareceram?

Que código estavas a correr?

Como esperas que o Arduino mantenha a hora?

Já viu isto? http://playground.arduino.cc/Code/time

Vai precisar de um desse:

Estou a usar este código:

// DateTime.pde
// example sketch for the DateTime library

#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

void setup(){
Serial.begin(19200);
pinMode(13,OUTPUT); // we flash the LED each second
}

void loop(){
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();
while( prevtime == DateTime.now() ) // wait for the second to rollover
;
DateTime.available(); //refresh the Date and time properties
digitalClockDisplay( ); // update digital clock

// send our time to any app at the other end of the serial port
Serial.print( TIME_HEADER,BYTE); // this is the header for the current time
Serial.println(DateTime.now());
digitalWrite(13,HIGH);
}
delay(100);
}

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();
if( c >= ‘0’ && c <= ‘9’){
pctime = (10 * pctime) + (c - ‘0’) ; // convert digits to a number
}
}
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
}

void digitalClockDisplay(){
// digital clock display of current date and time
Serial.print(DateTime.Hour,DEC);
printDigits(DateTime.Minute);
printDigits(DateTime.Second);
Serial.print(" “);
Serial.print(DateTimeStrings.dayStr(DateTime.DayofWeek));
Serial.print(” “);
Serial.print(DateTimeStrings.monthStr(DateTime.Month));
Serial.print(” ");
Serial.println(DateTime.Day,DEC);
}

void printDigits(byte digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print(‘0’);
Serial.print(digits,DEC);
}

Os erros que aparecem são os seguintes:

DateTime.pde: In function ‘void loop()’:
DateTime:19: error: ‘DateTime’ was not declared in this scope
DateTime:21: error: ‘DateTime’ was not declared in this scope
DateTime:30: error: ‘BYTE’ was not declared in this scope

As of Arduino 1.0, the ‘BYTE’ keyword is no longer supported.
Please use Serial.write() instead.

DateTime.pde: In function ‘boolean getPCtime()’:
DateTime:41: error: ‘time_t’ was not declared in this scope
DateTime:41: error: expected `;’ before ‘pctime’
DateTime:45: error: ‘pctime’ was not declared in this scope
DateTime:48: error: ‘DateTime’ was not declared in this scope
DateTime:48: error: ‘pctime’ was not declared in this scope
DateTime.pde: In function ‘void digitalClockDisplay()’:
DateTime:57: error: ‘DateTime’ was not declared in this scope
DateTime:61: error: ‘DateTimeStrings’ was not declared in this scope

Eu não tenho nenhum relógio real ligado ao arduino, apenas tenho o arduino em comunicação com o PC.
Penso eu que o arduino, com o código em cima, seja capaz de tentar sincronizar-se com o relógio do PC e a partir daí tornar-se autónomo, podendo-se em seguida encontrar a data actual, ou seja, o dia, o dia da semana e o ano. Estarei enganado?

Obrigado

De onde você tirou este código tem uma mensagem em letras garrafais:

Software Date Time library This library has been superseded by a newer version that is available here

Apontando para: http://playground.arduino.cc/Code/Time

Baixou e instalou as libs?

Sim já fiz download e instalei as libs..

Abri o ficheiro SyncArduinoClock da lib Time -> examples -> processing -> SyncArduinoClock e não consegui fazer com que o Arduino sincronizasse com o clock do PC.

Os erros reportados foram:

SyncArduinoClock:14: error: 'import' does not name a type SyncArduinoClock:16: error: expected unqualified-id before 'public' SyncArduinoClock:17: error: expected unqualified-id before 'public' SyncArduinoClock:18: error: expected unqualified-id before 'public' SyncArduinoClock:19: error: expected unqualified-id before 'public' SyncArduinoClock:20: error: expected unqualified-id before 'public' SyncArduinoClock:21: error: 'Serial' does not name a type SyncArduinoClock.pde: In function 'void setup()': SyncArduinoClock:24: error: 'size' was not declared in this scope SyncArduinoClock:25: error: 'class HardwareSerial' has no member named 'list' SyncArduinoClock:25: error: 'println' was not declared in this scope SyncArduinoClock:26: error: 'class HardwareSerial' has no member named 'list' SyncArduinoClock:26: error: 'portIndex' was not declared in this scope SyncArduinoClock:27: error: 'myPort' was not declared in this scope SyncArduinoClock:27: error: expected type-specifier before 'Serial' SyncArduinoClock:27: error: expected ;' before 'Serial' SyncArduinoClock.pde: In function 'void draw()': SyncArduinoClock:32: error: 'myPort' was not declared in this scope SyncArduinoClock:34: error: 'TIME_REQUEST' was not declared in this scope SyncArduinoClock:36: error: 'TIME_HEADER' was not declared in this scope SyncArduinoClock:40: error: 'LF' was not declared in this scope SyncArduinoClock:42: error: 'CR' was not declared in this scope SyncArduinoClock:43: error: 'println' was not declared in this scope SyncArduinoClock:45: error: 'print' was not declared in this scope SyncArduinoClock.pde: In function 'void mousePressed()': SyncArduinoClock:51: error: 'TIME_HEADER' was not declared in this scope SyncArduinoClock.pde: In function 'void sendTimeMessage(char, long int)': SyncArduinoClock:56: error: expected primary-expression before '.' token SyncArduinoClock:57: error: 'myPort' was not declared in this scope SyncArduinoClock.pde: In function 'long int getTimeNow()': SyncArduinoClock:63: error: 'GregorianCalendar' was not declared in this scope SyncArduinoClock:63: error: expected;' before 'cal' SyncArduinoClock:64: error: 'cal' was not declared in this scope SyncArduinoClock:64: error: expected type-specifier before 'Date' SyncArduinoClock:64: error: expected `)' before 'Date' SyncArduinoClock:65: error: 'Calendar' was not declared in this scope

Consegue ajudar?

Obrigado

Dá esses erros porque você esta tanando compilar uma linguagem escrita em "processing" na IDE do Arduino.

http://processing.org/

Boas pessoal.

Já tenho um Tiny RTC I2C ligado à placa UNO mas, ao testar o exemplo "TimeRTC" o compilador retorna-me o erro:

TimeRTC.pde: In function 'void setup()': TimeRTC:13: error: 'RTC' was not declared in this scope

Supostamente, o programa não está a reconhecer a variável RTC e, quando esta é chamada para obter a data e hora do relógio externo, o programa não executa a função que deveria executar.

Alguém me sabe dizer se estou a cometer algum erro?

Obrigado

Posta o código, mas presumo que você não copiou a livraria para a pasta, copiou?

Sim, julgo que copiei a livraria.

/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 * 
 */

#include <Time.h>  
#include <Wire.h>  
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop()
{
   digitalClockDisplay();  
   delay(1000);
}

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);
}