This will continually print the most current count. Never ending, even though I think I have it set to only print when previousCount == currentCount. But, maybe I don't quite understand that portion.
Sorry to not think things through before I post, but I realize now that previousCount and currentCount will ALWAYS be equal. I think I need to utilize the for() command and step through values. I am not sure how to do that though.
You are going in the right direction, but its simpler than you expect. There were a few things that needed to be changed. Have a look at this code and see if it does what you want.
int pulsePin = 2;
int previousCount = 0;
int currentCount = 0;
void setup(){
pinMode(pulsePin, INPUT);
attachInterrupt(0, count, FALLING);
Serial.begin(9600);
}
void loop(){
delay(1000);
if(currentCount == previousCount){
Serial.print("shaft stopped with count ");
Serial.println(currentCount);
currentCount = 0; // reset so the count will start from zero if it begins to rotate again ?
}
else{
// count is changing so the shaft is turning and we save the count
previousCount = currentCount;
}
}
void count(){
currentCount++;
}
Alright, first, thanks for all your help. I tried your code and it kind of did what I wanted. I made some changes and came up with this which works very well:
// Setup pins
int pulsePin = 2;
int previousCount = 0;
int currentCount = 0;
int ledPin = 13;
int counter=0;
void setup(){
pinMode(pulsePin, INPUT);
Serial.begin(9600);
// Ensure pulsePin is HIGH to watch for falling
digitalWrite(pulsePin, HIGH);
// Randomly create a "start time" for the encoder counter including an LED
randomSeed(analogRead(0));
// CHANGE FIRST NUMBER TO MIN ALLOWABLE AND SECOND NUMBER TO MAX FOR START TIMING
int delaySeconds=(random(6, 12)*1000);
delay(delaySeconds);
digitalWrite(ledPin, HIGH);
}
void loop(){
attachInterrupt(0, count, FALLING);
delay(1000);
if(currentCount == previousCount &&(currentCount != 0)){
Serial.println(counter);
digitalWrite(ledPin, LOW);
delay(5000000);}
// large delay to simulate shutting off the program
else{
previousCount = currentCount;}
}
This program has a random delay in the setup to cause a random start time for the encoder to start recording. I am doing this to monitor a human reaction, so basically the light turns on and then the human reacts by shutting off supply to the shaft. The encoder then measures total number of rotations from when the "alarm light" was trigger to when the shaft comes to a stop.
Thanks for all your help and I hope I explained this well.