So, I've made a simple script to capture changes in a digital signal and just send them over serial back to my PC for further examination.
int recievePin = 2;
void setup() {
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(recievePin),onRisingInterrupt, RISING);
}
void loop() {
if(Serial.available() > 0){
int readByte = Serial.read();
if(readByte == 97){
noInterrupts();
}
}
}
//Called on each rising signal
void onRisingInterrupt() {
volatile unsigned long currentTime = micros();
attachInterrupt(digitalPinToInterrupt(recievePin),onFallingInterrupt, FALLING);
Serial.print(currentTime);
Serial.println(" 1");
}
//Gets called on each falling edge
void onFallingInterrupt() {
volatile unsigned long currentTime = micros();
attachInterrupt(digitalPinToInterrupt(recievePin),onRisingInterrupt, RISING);
Serial.print(currentTime);
Serial.println(" 0");
}
But the problem with this script is that I keep getting time values that are smaller than the previous time value(examine attachment).
What is the reason for this strange behaviour, and how can I work around it?