void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.print(
"Roman Black Timing experiments per\n"
"https://www.romanblack.com/one_sec.htm\n"
);
}
void loop() {
// vars to remember time and 120hz bresenham sum
static uint32_t lastMs;
static uint16_t bres120;
uint32_t nowMs = millis();
if (false && nowMs - lastMs > 0) { // every change in ms
// 1000ms/120 = 8.333ms/cycle
Serial.print(nowMs - lastMs); Serial.print(" ");
bres120 += (nowMs - lastMs) * 120; // step forewward deltaMs*120
if (bres120 >= 1000 ) { // process fraction
bres120 -= 1000;
if ( black120HzTo100Hz()) {
static uint32_t lastXX = 0;
Serial.print(nowMs - lastXX); Serial.print(" "); Serial.print(bres120); Serial.print("\ndms:");
lastXX = nowMs;
}
}
lastMs = nowMs;
}
if (black1SecFromMicros()) {
Serial.print("\nnowMS:");
Serial.print(nowMs);
Serial.print(" black1SecFromMicros\n");
}
}
uint8_t black1SecFromMicros(void) {
const unsigned long increment = 200;
unsigned char retval = false;
static unsigned long last = 0;
static unsigned long bres1Sec = 0;
unsigned long nowUs = micros();
if (nowUs - last >= increment) { // assume 4us 250000Hz
last+=increment;
bres1Sec += increment;
if (bres1Sec >= 1000000) {
bres1Sec -= 1000000;
retval = true;
}
}
return retval;
}
uint8_t black120HzTo100Hz(void) {
static unsigned char bres;
uint8_t retval = false;
// uses 1 variable; unsigned char bres
// gets here every 120Hz, synced to US mains freq
bres += 100; // add 100 to bresenham total
if (bres >= 120) // if reached 1/100th of a second!
{
bres -= 120; // subtract 1/100th of a second, retain error
//do_100th_event(); // update clock, etc
retval = true;
}
return retval;
}