I am having issues using the micros() function with
the nano 33 BLE.
Below is the data on the version of IDE I am using:
Version: 2.1.0
Date: 2023-04-19T15:31:10.185Z
CLI Version: 0.32.2
Copyright © 2023 Arduino SA
My sketch is very involved.
Therefore, I have submitted a sketch which shows the
problem with minimal code.
What I am basically trying to implement is an off-delay timer
to be used as a screen saver to prevent damage to my OLEDs.
The falling edge of PB(pin 4) triggers the ISR.
It sets the “SCREEN_SAVER_ELAPSED_MICROS” =0.
This is the current value.
This variable then counts up and should turn off the
LED after ten seconds. (10000000)
Pulsing PB resets ELAPSED_MICROS=0.
The problem I am having is that the LED stays on for about 14.4 sec.
To make things even more confusing, if I unremark the delay(1)
on line 57 the time is pretty close.(10 sec)
Any code I add (print,display,etc) changes the timing error.
So I can’t really troubleshoot using the monitor.
I have scope pictures if interested.
There doesn’t seem to be any contact bounce on the PB.
I’m sure I’m doing something wrong but don’t have a clue.
Please don’t ask me to try millis().
Here is the code:
Thanks,
Larry
//2023-06-21
//Minimal Code test micros() problem
#define irq_pin 4
#define LEDPIN 10
#define TONEPIN 9 // Used if you want audio via a small buzzer
uint32_t SCREEN_SAVER_ELAPSED_MICROS;
const uint32_t SCREEN_SAVER_THR_MICROS = 10000000; //10 sec threshold
uint32_t SCREEN_SAVER_OLD_MICROS = 0;
uint32_t SCREEN_SAVER_DELTA_MICROS;
bool SCREEN_SAVER_ACTIVE;
void setup() {
// put your setup code here, to run once:
pinMode(4, INPUT_PULLUP); //IRQ PIN
pinMode(10,OUTPUT); //LED 4
// ========================= Global data definitions =====================
Serial.begin(57600);
attachInterrupt(irq_pin, ps2interrupt, FALLING);
} //end setup
// The ISR for the external interrupt
void ps2interrupt() //driven by PB
{
digitalWrite(10,HIGH); // Turn on L.E.D.
SCREEN_SAVER_ELAPSED_MICROS=0; //resets screen saver
}
void loop()
{
SCREEN_SAVER(); //DO screen saver calculations
} //END of loop
void SCREEN_SAVER(){
//delay(1);
if (SCREEN_SAVER_ELAPSED_MICROS >SCREEN_SAVER_THR_MICROS ){
SCREEN_SAVER_OLD_MICROS = micros(); //keep track of micros even when screen save is active
return; //don't do calculatioins if already in saver mode
}
if(SCREEN_SAVER_ACTIVE & (SCREEN_SAVER_ELAPSED_MICROS==0 ) ) { //SCREEN_SAVER_ELAPSED_MICROS SET=0 BY ISR
SCREEN_SAVER_ACTIVE = false;
return;
}
//ACCUMALATE TIME
if (micros()>SCREEN_SAVER_OLD_MICROS) {
(SCREEN_SAVER_DELTA_MICROS = micros() - SCREEN_SAVER_OLD_MICROS );
(SCREEN_SAVER_ELAPSED_MICROS = SCREEN_SAVER_ELAPSED_MICROS + SCREEN_SAVER_DELTA_MICROS);
(SCREEN_SAVER_OLD_MICROS = micros() );
}
if(SCREEN_SAVER_ELAPSED_MICROS >SCREEN_SAVER_THR_MICROS ) {
SCREEN_SAVER_ACTIVE = true;
digitalWrite(10,LOW); //
return;
}
} // end of SCREEN_SAVER()