So i am making a project where i want to measure the flow of water. For that i decided to use an encoder. Since i dont have that much knowledge in this i searched the web. On the web i found some codes that i modified and to some point it works. In the serial monitor i can see when i move it and for how much it moves. Now i want to calculate the average between a short time interval. so i can actually calculate an average. I am already trying the whole day and i didn't come anywhere. Do i have to use the timer liberally? how do i use it? or how do i wait a sec where i mencioned in the code. can you please help me?!?!
thank you!!
here is the code:
int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;
int a=0;
int b=0;
int c=0;
You need to record when you capture encoder0Pos, using millis(). On subsequent passes through loop(), see if enough time has passed to capture the value again. If so, do it, and recording the new time and computing whatever value the delta position corresponds to.
Look at the blink without delay example for inspiration.
int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;
int a=0;
int b=0;
int c=0;
long previousMillis = 0;
long interval = 1000;
int time=0;
You also need to see if it is time to capture another reading.
unsigned long currTime = millis();
if(currTime - prevTime >= 0)
{
valNew = encoder0Pos;
// Use valNew - valOld and currTIme - prevTime to compute whatever
valOld = valNew;
prevTime = currTime;
}
Notice that the first if test will only execute once. The second one will happen over and over, approximately once a second - not exactly once a second - so the delta encoder position needs to be divided by the delta time, not by 1000.
So first of all thanks for helpping me:-o i really need it. i gave it a lot of thought and still keeps bugging me:S
int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;
int c=0;
long interval = 1000;
unsigned long time=0;
int prevTime=0;
int currTime=0;
PLease use the # button to place appropiate tags around code. You can modify your previous posts too, select the code and press #. The code itself will not improve but it will look better
Thanks
zazhy:
So first of all thanks for helpping me:-o i really need it. i gave it a lot of thought and still keeps bugging me:S
int encoder0PinA = 2;
int encoder0PinB = 3;
volatile int encoder0Pos = 0;
volatile int encoder0PinALast = LOW;
volatile int n = LOW;
int valNew = 0;
int valOld = 0;
volatile int m = LOW;
int c=0;
long interval = 1000;
unsigned long time=0;
int prevTime=0;
int currTime=0;
zazhy:
can someone please tell me what i am doing wrong:(
You're not telling us what happens when you run the sketch.
int prevTime=0;
int currTime=0;
...
prevTime = millis();
...
unsigned long currTime = millis();
if(currTime - prevTime >= 0)
As Paul said earlier, millis() returns an unsigned long, not an int. Having global and local variables with the same name is not a good idea, particularly when they are different types.