I’ve been trying to determine if I need to add debouncing code to my project. In my investigations I noticed that the user code seems to execute at an period (approximately every 1ms). This is much greater than I would anticipate with a 120MHz processor. Below is the code I flashed to the Photon.
void loop() {
ButtonOut = digitalRead(ButtonPIN);
if(ButtonOut == HIGH && ButtonLast == LOW) {
ButtonCount = ButtonCount + 1;
ButtonLast = HIGH;
} else if (ButtonOut == LOW) {
ButtonLast = LOW;
}
if (SerialTimer < millis()) {
SerialTimer = millis()+1000;
PotOut = analogRead(PotPIN);
Serial.print("Button Count = ");
Serial.print(ButtonCount);
Serial.print(" , Level = ");
Serial.print(PotOut);
Serial.print(" , duration(us) = ");
LoopPeriod = EndTime-StartTime;
Serial.println(LoopPeriod);
}
StartTime = EndTime;
EndTime = micros();
}
This results in the following serial terminal output:
Button Count = 8 , Level = 693 , duration(us) = 1000
Button Count = 9 , Level = 697 , duration(us) = 1000
Button Count = 9 , Level = 695 , duration(us) = 997
Button Count = 11 , Level = 694 , duration(us) = 1000
First, do I have an error in my test case? If I understand it correctly in the loop() immediately before the serial transmission, only five statements are evaluated ButtonOut, IF(FALSE), IF(FALSE), StartTime, and EndTime. At most this should generate a few dozen assembly instructions.
Second, is there a documentation source I should be reading about the interaction between loop() and the system processes that would contain this information? I have reviewed https://docs.particle.io/reference/firmware/core/#automatic-mode and searched for “loop” in the documentation.
Most importantly, the topic question: What is the minimum period over which loop() is executed on the Photon in its default mode of operation (ie., SYSTEM_MODE(AUTOMATIC), etc.)?
Finally, What other configuration settings and/or use methodologies should I look at to find cases where the execution rate would be fast enough, I might need to debounce switches. Again assuming we’re using Automatic mode and just the web IDE, nothing fancy. I’m using the Photon for a College Freshman EE course and I’m certain my students will find some way to break it!
Thanks for your time,
David Orser