Go Down

Topic: Time between two events - no delay (Read 2836 times) previous topic - next topic

ebweiss2

What do you mean can't make it happen?  It's just a variable assignment.  Do you want the time between interrupts?  So the fin time for this one should be the start time for the next one?


That did it!  I can't believe it was that simple.  Thank you!
All I had to do was add start = fin to the end.  Elapsed is for future use.
Seems to work great.


Code: [Select]

long int start =0;
long int revs = 0;
volatile int elapsed =0;

void setup()
{
  pinMode (2,INPUT);
  digitalWrite(2,HIGH);
  Serial.begin(9600);
  

    
attachInterrupt(0,react,FALLING);  
digitalWrite(2, HIGH);

  //get the start time
  start = millis();  
 
}

void loop()
{
 
  
  }

void react()
{
  //start = millis();
  
  long fin = millis();
  revs =(fin - start);
  Serial.println(revs);
  start = fin;
  elapsed = revs;
}




Code: [Select]

MorganS

DON'T ATTEMPT TO PRINT INSIDE AN ISR! How many times do we have to say it?

It's like "Don't run with scissors." Yes, you can run a lot with scissors but when it goes wrong it is a really serious injury.

Have your ISR set a global variable like boolean isReady; When the main loop detects that isReady is true, then it can grab a 'safe copy' of the volatile variables and print that. Inside the 'safe copy' area, it can set isReady back to false, so the next time the ISR hits it, the main loop will know that it has a new thing ready to print.
"The problem is in the code you didn't post."

Go Up