// If I have a range of values (from a rotary encoder)that vary between 0 _and 40 _and I want to know the elapsed _time that the values
// are between 5 _and 20 how would I define _this range so it could be timed? If I use a statement such as
//_start the timer at =5 _and _stop when =20. This will tell me how _long it took to go from 5 to 20 but _if I am
//transitioning downward from 40 I would want to _do the _timing in reverse(_start timer at =20 _and _stop when =5).
//If I _start timing by saying "start timer _if value is >5 and value is <20 or value is <20 and value is >5 this does not work.
//I am looking _for a way to code "start timer when value is between 5 and 20 or between 20 and 5 else stop timer"
//see sketch below:
// This program is made to check (and time) the position of a rotary encoder within a certain range (5-20).
#define rled 11 // red LED -time exceeds threshold
#define outputA 6 // Rotary Encoder (RE)
#define outputB 7 //data pins 6 and 7
int counter = 0; // count the steps of the RE 0 to 40
int aState; // current state of RE
int aLastState; // last state of RE
unsigned long start, finished, elapsed; //for elapsed timer
void setup() {
pinMode (outputA,INPUT); // sets RE as
pinMode (outputB,INPUT); // inputs pin 6 and 7
pinMode (rled,OUTPUT); // LED Output
Serial.begin (9600); //begin serial monitor
aLastState = digitalRead(outputA); // Reads the initial state of the outputA
}
void displayResult()// results of timer
{
float s,ms;
unsigned long over;
elapsed=finished-start;
over=elapsed%3600000;
over=over%60000;
s=int(over/1000);
Serial.println(elapsed);
Serial.println();
}
void loop() {
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
}
else
{
counter --;
}
Serial.print("Position: ");
Serial.println(counter);
}
aLastState = aState; // Updates the previous state of the outputA with the current state
if (int(counter)== 5) // start the timer when the RE equals 5
{
start=millis();
delay(10);
}
if (int(counter)==20) // stop the timer when the RE equals 20
{
finished=millis();
delay(10);
displayResult();
}
if(elapsed/1000 > 10 && int(counter) >= 5 && int(counter) <= 20) //If the elapsed time (in seconds) and the RE remains within 5-20 switch on an led
{
digitalWrite(rled,HIGH); // time excedes threshold
}
else
{
digitalWrite(rled,LOW); // time is within threshold - (the threshold will be used to take other actions)
}
} // this example will return the count time once the RE has exceeded 20 but does not report the time while below 20.
// another issue with this program is that the rotary encoder will loose count intermittently (debounce?).
if((value == 5 || value == 20) && value != oldValue){
if(timerStarted){
stopTimer()
}
else{
startTimer()
}
}
oldValue = value
Thanks for your quick reply, I will check how I can implement yor suggestion and let you know how it turns out. What are code tags?
johnnydiode:
What are code tags?
You used them in your first post so some part of your brain knows what they are.
@septillion was overzealous when he created his signature. Everything below the second horizontal line can be ignored.
@johnnydiode, you used them in your whole first post. Nobody knows why you did that...