Flow meter and timer

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;

void setup()
{

pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);

}

void loop()
{

encoder0PinALast = n;
valNew = encoder0Pos;
if (valNew != valOld) {
valOld = valNew;

a=encoder0Pos;
//here i want to wait one second, but i dont want the program to be interupted
b=encoder0Pos;

c=(b-a)/1000;

Serial.print (a, DEC);
//Serial.print (m);
Serial.println ();
}

}

void CountA()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (m == HIGH) {
encoder0Pos++;
}

}
}

void StateB()
{
m = digitalRead(encoder0PinB);
}

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.

i keep trying and trying but i dont know how to incorporate the blinking without delay into the code:S i dont know what am i doing wrong:S

i dont know what am i doing wrong

Not posting your new code is a start.

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;

void setup()
{
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);
}

void loop()
{
encoder0PinALast = n;
valNew = encoder0Pos;
if (valNew != valOld) {

valOld = valNew;

unsigned long currentMillis = millis();

time = millis();

a=encoder0Pos;

if(time=currentMillis){
b=encoder0Pos;
}

if((currentMillis - previousMillis) == interval) {

c=(b-a)/1000;

Serial.print (c, DEC);
//Serial.print (m);
Serial.println ();
previousMillis = currentMillis;

}
}
}

void CountA()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (m == LOW) {

}
else {
encoder0Pos++;
}
}
}

so confused right now i dont even now what i did:S

void StateB()
{
m = digitalRead(encoder0PinB);
}

time = millis();

millis() returns an unsigned long, not an int.

    if(time=currentMillis){

== would work better...

You want to define a variable, prevTime, with an initial value of 0.

On each pass through loop(), you need to determine if it is time to store the encoder reading.

if(prevTime == 0)
{
  valOld = encoder0Pos;
  prevTime = millis();
}

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;

void setup()
{
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);
}

void loop()
{
encoder0PinALast = n;
valNew = encoder0Pos;
if (valNew != valOld) {

time = millis();

if(prevTime == 0)
{
valOld = encoder0Pos;
prevTime = millis();
}

unsigned long currTime = millis();
if(currTime - prevTime >= 0)
{
valNew = encoder0Pos;

// Use valNew - valOld and currTIme - prevTime to compute whatever
c=(valNew - valOld)- (currTime - prevTime);

Serial.print (c, DEC);
//Serial.print (m);
Serial.println ();

valOld = valNew;
prevTime = currTime;
}
}
}

void CountA()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (m == HIGH) {
encoder0Pos++;
}

}
}

void StateB()
{
m = digitalRead(encoder0PinB);
}

any idea what might be wrong?

any idea what might be wrong?

You need to tell us. What are you expecting this code to do? What does it actually do?

i want to messure the flow of water. so i want to know how fast is the encoder spinning. how fast it changes values..

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 :wink:
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;

void setup()
{
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
Serial.begin (9600);
attachInterrupt(1, CountA, CHANGE);
attachInterrupt(0, StateB, FALLING);
}

void loop()
{
encoder0PinALast = n;
valNew = encoder0Pos;
if (valNew != valOld) {

time = millis();

if(prevTime == 0)
{
valOld = encoder0Pos;
prevTime = millis();
}

unsigned long currTime = millis();
if(currTime - prevTime >= 0)
{
valNew = encoder0Pos;

// Use valNew - valOld and currTIme - prevTime to compute whatever
c=(valNew - valOld)- (currTime - prevTime);

Serial.print (c, DEC);
//Serial.print (m);
Serial.println ();

valOld = valNew;
prevTime = currTime;
}
}
}

void CountA()
{
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == LOW) && (n == HIGH)) {
if (m == HIGH) {
encoder0Pos++;
}

}
}

void StateB()
{
m = digitalRead(encoder0PinB);
}

any idea what might be wrong?

can someone please tell me what i am doing wrong:(

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.