1

I am currently experimenting with a Digistump ATTiny85. Since it has no Serial, I use the basic DigiUSB library for communication. The RTC library used is this one: https://github.com/JChristensen/DS3232RTC/. The DigiUSB library is from Digispark.

I attached a DS3232 via TWI to the Digistump, because I want to use this construct as a USB-Realtime-Clock.

Initially setting the time in the setup method works as follows

  RTC.set(1532341020);

But I am unable to transfer the time via the pseudo serial connection and set it remotely. What I've tried so far:

  • Using union with char and uint32_t. Althougt the char representation is correct (tried DigiUSB.println()), the uint32_t representation of this union is not what I need.
  • Tried to adapt to some shifting examples, but seriously got lost here. All examples where about 4 byte streams.
  • atoi and some other functions with no success.

Here is my current sketch

#include <DS3232RTC.h>
#include <DigiUSB.h>

//union ArrayToInteger {
//  char array[10];
//  uint32_t integer;
//};

unsigned char foo[10];
uint32_t num;

void setup() {                
  RTC.begin();
  //RTC.set(1532341020);
  DigiUSB.begin();
}

void loop() {
  if (DigiUSB.available() >= 10) {
    for (int i=0; i<=10; i++) {
    foo[i] = DigiUSB.read();
  }
  RTC.set((uint32_t)foo);
  DigiUSB.delay(100);
  DigiUSB.refresh();
  //DigiUSB.println(_foo.array);
  DigiUSB.println(RTC.get());
} else {
  DigiUSB.refresh();
  DigiUSB.println(RTC.get());
  DigiUSB.refresh();
  DigiUSB.delay(1000);
  }
}

In the end I need the a char _foo 0123456789 represented as/converted to uint32_t num 0123456789

Kinda stuck here. Can anyone point me in the right direction ore provide an example?

  • add links to libraries. a number representation of time with RTC is 'Epoch' - seconds from 1.1.1970. what do you mean by "char representation"? – Juraj Jul 23 '18 at 10:34
  • 1
    You may try atol(), but note that from Jan 19 2038 you will need to use strtoul() instead. – Edgar Bonet Jul 23 '18 at 10:54
  • how do you send the epoch value? – Juraj Jul 23 '18 at 10:56
  • @EdgarBonet atol errors invalid conversion from 'unsigned char*' to 'const char*'. Same with strtoul. – ansi_lumen Jul 23 '18 at 11:02
  • if (DigiUSB.available()) { time_t epoch = DigiUSB.parseInt(); RTC.set(epoch);}? – Juraj Jul 23 '18 at 11:02
2

There are several possible issues here:

  • can you send the number, as a string, from the PC to the ATtiny?
  • can you convert the string to an actual number?
  • can you give this number to the RTC?

The phrasing of your question sounds like your issue is with the conversion. However that is easily handled with atol(). Here is a test program I tried on my Uno:

void setup() {
    char time_string[] = "1532341020";
    uint32_t time_number = atol(time_string);

    Serial.begin(9600);
    Serial.print("string: \"");
    Serial.print(time_string);
    Serial.print("\", number = ");
    Serial.println(time_number);
}

The output is:

string: "1532341020", number = 1532341020
  • Indeed atol does the trick. For completeness, I answered by posting the working code. Thanks for input. – ansi_lumen Jul 23 '18 at 11:27
0

Answering this question based on the help from above. It works with this code. Thank you all for the help. The clock does not reply the newly set time instantly, but after a few seconds.

#include <DS3232RTC.h>
#include <DigiUSB.h>
char foo[10];

void setup() {                
  RTC.begin();  
  DigiUSB.begin();
}

 void loop() {
   if (DigiUSB.available() >= 10) {
     for (int i=0; i<=10; i++) {
       foo[i] = DigiUSB.read();
   }
   RTC.set(atol(foo));
   DigiUSB.delay(100);
   DigiUSB.refresh();
   DigiUSB.println(RTC.get());
} else {
   DigiUSB.refresh();
   DigiUSB.println(RTC.get());
   DigiUSB.refresh();
   DigiUSB.delay(1000);
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.