This simple time delay routine works fine as long as count is a global variable. If I change it to a local variable, the function breaks. What am I doing wrong?
When you declare a variable inside a set of curly braces, it's scope is limited to within those braces, not outside
Try moving the declaration (int count) right after void delaynoISR(int ms) {
It's scope will be the whole function, but not global.
The Arduino function delay() uses a timer, and will not work with interrupts disabled. This is just a clean, quick and dirty way for me to show hard faults, both inside and outside of ISRs. I use it to flash an LED to indicate a hard fault. The rate of flashing indicates which fault.
How about this code to make your IO-pin switch high/low?
unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long
const byte OnBoard_LED = 2;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED,1000);
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer,BlinkPeriod) ) {
digitalWrite(IO_Pin,!digitalRead(IO_Pin) );
}
}