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
withchar
anduint32_t
. Althougt the char representation is correct (tried DigiUSB.println()), theuint32_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?
2 Answers 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
-
1Indeed
atol
does the trick. For completeness, I answered by posting the working code. Thanks for input.moestly– moestly2018年07月23日 11:27:13 +00:00Commented Jul 23, 2018 at 11:27
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);
}
}
atol()
, but note that from Jan 19 2038 you will need to usestrtoul()
instead.atol
errorsinvalid conversion from 'unsigned char*' to 'const char*'
. Same withstrtoul
.if (DigiUSB.available()) { time_t epoch = DigiUSB.parseInt(); RTC.set(epoch);}
?