Skip to content

Instantly share code, notes, and snippets.

@giltesa
Last active June 21, 2017 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giltesa/813d6b47a7dcade751df to your computer and use it in GitHub Desktop.
Save giltesa/813d6b47a7dcade751df to your computer and use it in GitHub Desktop.
Digispark Timers
#include <DigiKeyboard.h>
#include <EEPROM.h>
#define pLED 1
#define EEPROMSIZE 512
const unsigned int year = 31556926;
const unsigned int month = 2629743;
const unsigned int day = 86400;
const unsigned int hour = 3600;
const unsigned int minute = 60;
unsigned int tmp, i;
struct Timer{
unsigned long total; //The times are in seconds
unsigned long last;
unsigned long now;
char ready;
};
union Memory{
Timer timer;
byte b[sizeof(Timer)];
}
memory;
union Index{
unsigned int addrTimer;
byte b[sizeof(int)];
}
idx;
void setup()
{
pinMode(pLED, OUTPUT);
//Reads current memory address of the Timers
idx.b[0] = EEPROM.read(0);
idx.b[1] = EEPROM.read(1);
if( idx.addrTimer == EEPROMSIZE/sizeof(memory.b)*sizeof(memory.b) )
idx.addrTimer = EEPROMSIZE/sizeof(memory.b);
//Reads the data of the Timers
for( i=0 ; i < sizeof(memory.b) ; i++ )
memory.b[i] = EEPROM.read(i + idx.addrTimer);
//Writes next memory address of the Timers
idx.addrTimer += sizeof(memory.b);
EEPROM.write(0, idx.b[0]);
EEPROM.write(1, idx.b[1]);
//Updates the timers
if( memory.timer.ready != 'Y' )
{
memory.timer.ready = 'Y';
memory.timer.total = 0;
memory.timer.last = 0;
memory.timer.now = 0;
}
else
{
memory.timer.last = memory.timer.now;
memory.timer.total += memory.timer.last;
memory.timer.now = 0;
}
}
void loop()
{
//Prints the timers:
if( millis() % 12000 == 0 )
{
DigiKeyboard.sendKeyStroke(0);
DigiKeyboard.print("Tot ");
DigiKeyboard.println(getTime(memory.timer.total));
DigiKeyboard.print("Last ");
DigiKeyboard.println(getTime(memory.timer.last));
DigiKeyboard.print("Now ");
DigiKeyboard.println(getTime(memory.timer.now));
DigiKeyboard.println();
}
//Write the timers in EEPROM:
if( millis() % 10000 == 0 )
{
memory.timer.now += millis()/1000-memory.timer.now;
for( i=0 ; i < sizeof(memory.b) ; i++ )
EEPROM.write(i + idx.addrTimer, memory.b[i]);
digitalWrite(pLED, HIGH); delay(150);
digitalWrite(pLED, LOW ); delay(150);
digitalWrite(pLED, HIGH); delay(150);
digitalWrite(pLED, LOW ); delay(150);
digitalWrite(pLED, HIGH); delay(150);
digitalWrite(pLED, LOW ); delay(150);
}
}
String getTime(unsigned long t)
{
String s = String();
/*
tmp = t / year;
if(tmp>0){
s = s.concat(tmp);
s = s.concat("y ");
}
tmp = t % year / month;
if(tmp>0){
s = s.concat(tmp);
s = s.concat("m ");
}
tmp = t % year % month / day;
if(tmp>0){
s = s.concat(tmp);
s = s.concat("d ");
}
tmp = t % year % month % day / hour;
if(tmp>0){
s = s.concat(tmp);
s = s.concat("h ");
}
*/
tmp = t % year % month % day % hour / minute;
if(tmp>0){
s = s.concat(tmp);
s = s.concat("m ");
}
tmp = t % year % month % day % hour % minute;
if(tmp>0){
s = s.concat(tmp);
s = s.concat("s");
}
return s;
}
#include <EEPROM.h>
#define pLED 13
#define ADDTIME 10000 //300000
#define YEAR 31556926
#define MONTH 2629743
#define DAY 86400
#define HOUR 3600
#define MINUTE 60
unsigned long time, timeEEPROM=0;
unsigned int tmp;
struct Timer{
char ready;
unsigned int addr;
unsigned long total; //The times are in seconds
unsigned long last;
unsigned long now;
};
union Memory{
Timer timer;
byte b[sizeof(Timer)];
}
memory;
void setup()
{
pinMode(pLED, OUTPUT);
Serial.begin(9600);
while(!Serial);
//Reads the data of the Timers:
tmp = EEPROM.length() - sizeof(memory.b);
for( int e=0 ; e < tmp ; e++ )
{
for( int r=0 ; r < sizeof(memory.b) ; r++ )
memory.b[r] = EEPROM.read(e+r);
if( memory.timer.ready == 'Y' )
break;
}
//Updates the timers:
if( memory.timer.ready != 'Y' )
reset();
else
{
Serial.print("ADDR_1: "); Serial.println(memory.timer.addr);
memory.timer.last = memory.timer.now;
memory.timer.total += memory.timer.last;
memory.timer.now = 0;
}
//Prints the start of the program:
Serial.print(F("COUNTERS OF TIME (UPDATE TIMERS EVERY "));
Serial.print(getTime(ADDTIME/1000));
Serial.println(F(")\n"));
print();
}
void loop()
{
time = millis();
//Command for reset EEPROM:
if( Serial.available() )
{
String s = Serial.readString();
s.toUpperCase();
if( s.equals(F("RESET")) )
{
for( int e = 0 ; e < EEPROM.length() ; e++)
{
EEPROM.write(e, 0);
if( e%25 == 0 )
{
digitalWrite(pLED, !digitalRead(pLED));
delay(10);
}
}
digitalWrite(pLED, LOW);
reset();
}
while( Serial.available() && Serial.read() != -1 );
}
//Write EEPROM:
if( time - timeEEPROM > ADDTIME )
{
timeEEPROM = time;
memory.timer.now += ADDTIME/1000;
//Write new timers:
Serial.print("ADDR_2: "); Serial.println(memory.timer.addr);
memory.timer.addr = ( memory.timer.addr < EEPROM.length() - sizeof(memory.b) ? memory.timer.addr + 1 : 0 );
Serial.print("ADDR_3: "); Serial.println(memory.timer.addr);
tmp = sizeof(memory.b);
for( int r=0, e=memory.timer.addr ; r < tmp ; r++, e++ )
EEPROM.write(e, memory.b[r]);
for( int i=0 ; i < 6 ; i++ )
{
digitalWrite(pLED, !digitalRead(pLED));
delay(150);
}
print();
}
}
void print()
{
Serial.print(F(" ADDR : ")); Serial.println(memory.timer.addr);
Serial.print(F(" Total: ")); Serial.println(getTime(memory.timer.total + memory.timer.now));
Serial.print(F(" Previous: ")); Serial.println(getTime(memory.timer.last));
Serial.print(F(" Now: ")); Serial.println(getTime(memory.timer.now));
Serial.println();
}
String getTime(unsigned long t)
{
String s = String();
s.reserve(32);
tmp = t / YEAR;
if(tmp>0){
s.concat(tmp);
s.concat(F("y "));
}
tmp = t % YEAR / MONTH;
if(tmp>0){
s.concat(tmp);
s.concat(F("m "));
}
tmp = t % YEAR % MONTH / DAY;
if(tmp>0){
s.concat(tmp);
s.concat(F("d "));
}
tmp = t % YEAR % MONTH % DAY / HOUR;
if(tmp>0){
s.concat(tmp);
s.concat(F("h "));
}
tmp = t % YEAR % MONTH % DAY % HOUR / MINUTE;
if(tmp>0){
s.concat(tmp);
s.concat(F("m "));
}
tmp = t % YEAR % MONTH % DAY % HOUR % MINUTE;
if(tmp>0){
s.concat(tmp);
s.concat(F("s"));
}
s.trim();
return s;
}
void reset()
{
memory.timer.ready = 'Y';
memory.timer.addr = 0;
memory.timer.total = 0;
memory.timer.last = 0;
memory.timer.now = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment