News:

Let's find out together what makes a PIC Tick!

Main Menu

waterflowsensor

Started by rleg, Jul 02, 2021, 08:17 AM

Previous topic - Next topic

rleg

Hello, I am a beginner in the field of programming,
I am trying to make a water meter with the waterflow sensor yF-b1
Here are the program codes
I display the frequency and other variables
in fact the problem is in the sampling
the only data in my position is F '(frequency) = 11 * Q ( litre/min)
it would be necessary to be able to store the quantity during a given period of time and to make the sum when the sensor stops
this is my problem, I don't see how to solve it
thank you for any answers you may have

Stephen Moss

Are you getting any reading for F?
If not then that may be because the hall output is unprocessed and so like many hall sensors I have come across the output voltage sits at half the supply voltage and is varied up and down my a few tens of millivolts (unless using a very powerful magnet) and so the voltage change may not be significant enough to trigger an input.

Depending on the accuracy you need you might want to redefine q as Float or Word instead of Byte as 100/11 = 9.090 and 84/11 = 7.636, if you do not need that accuracy if f may be greater than 2805 at any time then q will be greater than 255 and so will overflow a Byte. 

To store a running total you only need to add the required data (F, L or Q) to a variable after you have calculated them, i.e.
Total_Q = Total_Q + q
if you want an average then you would require another variable to track the number of times you have done that and divide the Total by the number of samples.

You might want to add f=0 to the start of you While loop to ensure that the current Count is cleared before taking a new one, the compiler command probably restarts the count from 0 every time it is called but if in doubt clear it yourself.

You might want to try using an If-Then statement in your While loop after f = Counter pin,1000, i.e.
If F > 0 then  'Sensor Turing
[Existing Code to calculate and display current values of f, q & l (plus total) here]
Else  'Sensor Stopped, f=0
[Code to Display Total]
End if

Unless you meant storing it in EPROM in which case refer to the compiler manual sections regarding the EDATA, EREAD and EWRITE commands

trastikata

To add to Stephen Moss' answer, if you have to display the total quantity every time the sensor stops in one power-on cycle, you will need to reinitialize the counters and accumulator variables.
 
Also I noticed Variable "l" is a Float, then the following line:

Print At 3,1,"quantite ", Dec l ,"     "

will cause problem displaying "l" since you did not limit the number of decimal places to display.

rleg

Hello, thank you for your answers and comments
I have the display of F , and q the hall output is a perfect 5v amplitude modulated signal measured at scope 0 to 84 hz depending on the quantity
I will code as you suggest and see the result which I will share with you
thanks

rleg

Good morning,
I coded as you advised but unfortunately it doesn't work
the display of F and Q no problem
but no display of the total (totalQ and X) always remains at -0.00
Here is the program
'****************************************************************
'*  Name    : debitmètre     *
'*  Author  : RLE                                *
'*  Notice  : réglage débit arrosage                    *
'*          : All Rights Reserved                               *
'*  Date    : 15/06/2021                                       *
'*  Version : 1.0                                              *
'*  Notes                             *
'****************************************************************
;-------------------------------------------------------------------------------

' Device = 16F877A
Device = 18F25K22
Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
__Config Config1H, 0x28 ;FOSC_INTIO67 & PLLCFG_OFF & PRICLKEN_ON & FCMEN_OFF & IESO_OFF
__Config Config2L, 0x19 ;PWRTEN_OFF & BOREN_OFF & BORV_190
__Config Config2H, 0x3F ;WDTEN_ON & WDTPS_32768
__Config Config3H, 0x39 ;CCP2MX_PORTC1 & PBADEN_OFF & CCP3MX_PORTC6 & HFOFST_ON & T3CMX_PORTC0 & P2BMX_PORTB5 & MCLRE_INTMCLR
__Config Config4L, 0x80 ;STVREN_OFF & LVP_OFF & XINST_OFF & DEBUG_OFF
__Config Config5L, 0x00 ;CP0_ON & CP1_ON & CP2_ON & CP3_ON
__Config Config5H, 0x80 ;CPB_ON & CPD_OFF
__Config Config6L, 0x0F ;WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF
__Config Config6H, 0xE0 ;WRTC_OFF & WRTB_OFF & WRTD_OFF
__Config Config7L, 0x0F ;EBTR0_OFF & EBTR1_OFF & EBTR2_OFF & EBTR3_OFF
__Config Config7H, 0x40 ;EBTRB_OFF
Endasm-
Declare Reminders On


;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------

  Declare Xtal = 8 
Declare All_Digital = true
OSCCON = %01100000
Declare LCD_Type 0
Declare LCD_DTPin PORTB.4
Declare LCD_ENPin PORTB.2
Declare LCD_RSPin PORTB.3
Declare LCD_Interface 4
Declare LCD_Lines 4   
    Dim q As Float           'quantité litre/min
    Dim totalQ As Float     ' total
    Dim X As Float           ' fact div
    Dim f As Word            'fréquence sensor
    Symbol pin = PORTA.4
    TRISA=%11111111     ' port A en entrée       
     
Cls
Print At 1,1,"debitmetre"
      q=0
      f=0
Main:  Cls
 While 1=1
     f = Counter pin,1000 'f=11* q (litre/min)
     If f>0 Then
      q=f/11
      totalQ=totalQ + q
     Print At 1,1,"totalQ = " ,Dec2 totalQ, "         "
     Print At 2,1,"freq  f= " ,Dec2 f, "        "
     Print At 3,1,"debit q= " ,Dec2 q, "        "                         
'     Print At 4,1,"quant T= ", Dec3 X, "        " 
     Else f=0
     Cls
       totalQ=totalQ + q
       X=totalQ /2
      Print At 1,5,"test"
'     Print At 2,1,"freq  f= " ,Dec2 f, "       "
'     Print At 3,1,"debit q= " ,Dec2 q, "       "                         
     Print At 4,1,"total T= ", Dec2 X, "       "   
     End If
     Wend
    GoTo Main         

 End   

RGV250

Hi,
Just a thought but what values are you expecting, could it be that they are smaller than 2 dec spaces?
What happens if you print Dec X (totalQ) rather than Dec2 X(totalQ)?

You have said it shows -0.00, is this is a typo, I cannot see how it would be a negative value?

Bob

RGV250

Hi,
I cannot simulate it entirely but in the demo VSM if I add totalQ = 1 I get 0.05 as expected and if I then edit so f = 1 I get a count up as you would expect.

What version of the compiler are you using as your example will not compile in the latest version as Pin is a protected word??

Regards,
Bob

trastikata

#7
Few things I noticed:
- You didn't clear the RAM, neither initialized totalQ thus when you start adding to them, they might have some initial value.
- You don't clear q and when F = 0 you add q to totalQ  but it keeps the value from the previous iteration when F was > 0 
- what is the expected value of f at each iteration? For totalQ  you are displaying only decimal 2 digits, if q=F/11 is small enough, you might not get to 2 decimal digits?
- When you Print a line, why do you print the white space at the end of each line - the total number of characters becomes for sure more than 15 for each line (you are starting at position 1)

Stephen Moss

Quote from: rleg on Jul 05, 2021, 07:18 AMGood morning,
I coded as you advised but unfortunately it doesn't work
the display of F and Q no problem
but no display of the total (totalQ and X) always remains at -0.00
With X being derived from totalQ then it would suggest the problem lies with either q or totalq, so what values are you seeing for F and Q?

I am sure someone will correct me if I am wrong but...
1) I think it is bad practice to put a command on the same line as a label as it make the label harder to see and I am not sure the compiler sees anything after the label so consider changing...
Main: Cls to
Main:
Cls

2) I cannot really see anything obviously wrong with your code that would affect the display of Totalq and X other then that you may be overflowing the display.
If you are using a 4 x 16 Alphanumeric display then try removing the "        " from you print commands, i.e. change
Print At 1,1,"totalQ = " ,Dec2 totalQ, "        "to
Print At 1,1,"totalq= ", Dec2 totalQYour descriptive text is 9 characters, then you have your value and then you appear to be adding 9 spaces after your value which creates a string of at least 18 characters. As I recall overflowing the number of characters in a line does odd things like appending the over flow character to the next line of the display.

If that does not resolve the issue then try temporarily replacing... Print At 1,1,"totalQ = " ,Dec2 totalQ, "         " with Print At 1,1, Dec totalQ to allow the maximum number of characters to display totalq.

I think Else f=0 should be either ElseIf f=0   'If checking F is 0 to confirm that the flow has stopped rather than assuming it has as F is not > 0 or Else
f=0 'Clear previous value of F



While probably not relevant to the problem with totalq and X I noticed you have the WatchDog timer enabled (WDTEN_ON), there are two general uses for the Watchdog timer...
a) To wake the PIC from sleep - but you are not not sending it to sleep so you do not need it for that.
b) To automatically re-start the PIC if the code hangs for any reason at which point the current value of TotalQ will be lost if not stored in EEPROM. Consequently, either you must clear the current Watchdog timer value back to 0 somewhere in you main loop to prevent it restarting when you may not want it to, or, if you application is a non-critical operation and manual restart is sufficient then consider turning the WDT off.

rleg

Hello to all,
the display is in LCD 4 lines 20 characters and I do not exceed them with the codes of the program
the reading of F is 1 to 84 Hz
so q = 0,09 to 7,63
I took note for WDTEN off (0x3C)
but still no total displayed remains at -0.00
I coded -> elseif f=0 then
but no effect
the lord's ways are inscrutable



trastikata

When I try to compile your program, I get an error in the following line:

 "Symbol Pin = PORTA.4"

Because "Pin" is a protected keyword. I remember Les implemented it not long ago, so what version of the compiler are you using?

RGV250

Hi,
I replaced the counter with a value and added a delay and it works in a simulation, try this just to see what you get.
While 1=1
>>>>>>>>>>>>>>>   f = 1              'Counter Pin,1000 'f=11* q (litre/min)
     If f>0 Then
      q=f/11
      totalQ=totalQ + q
     Print At 1,1,"totalQ = " ,Dec2 totalQ, "         "
     Print At 2,1,"freq  f= " ,Dec2 f, "        "
>>>>>>>>>>>>>>    DelayMS 500
     Else f=0

Bob

top204

The word "Pin" is now a unique variable type within the compilers so it cannot be used as a variable name.

Asking questions based on older versions of the compilers is not always a good idea because the answers given are, generally, based on more recent compiler versions that may have had improvements carried out or anomalies corrected, so the correct answer may not work in the compiler version you have because of changes etc...


rleg

 thank you for your help
I just changed the program before I saw your comment
now everything works correctly, I just have to check if the quantity recorded is correct
thank you very much
here is the program
*  Name    : debitmètre     *
'*  Author  : RLE                                *
'*  Notice  : réglage débit arrosage                    *
'*          : All Rights Reserved                               *
'*  Date    : 15/06/2021                                       *
'*  Version : 1.0                                              *
'*  Notes                             *
'****************************************************************
;-------------------------------------------------------------------------------

Device = 18F25K22
Declare Reminders Off
@ CONFIG_REQ = 0 ; Override Compiler's configuration settings
Asm-
__Config Config1H, 0x28 ;FOSC_INTIO67 & PLLCFG_OFF & PRICLKEN_ON & FCMEN_OFF & IESO_OFF
__Config Config2L, 0x19 ;PWRTEN_OFF & BOREN_OFF & BORV_190
__Config Config2H, 0x3C ;WDTEN_Off & WDTPS_32768
__Config Config3H, 0x39 ;CCP2MX_PORTC1 & PBADEN_OFF & CCP3MX_PORTC6 & HFOFST_ON & T3CMX_PORTC0 & P2BMX_PORTB5 & MCLRE_INTMCLR
__Config Config4L, 0x80 ;STVREN_OFF & LVP_OFF & XINST_OFF & DEBUG_OFF
__Config Config5L, 0x00 ;CP0_ON & CP1_ON & CP2_ON & CP3_ON
__Config Config5H, 0x80 ;CPB_ON & CPD_OFF
__Config Config6L, 0x0F ;WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF
__Config Config6H, 0xE0 ;WRTC_OFF & WRTB_OFF & WRTD_OFF
__Config Config7L, 0x0F ;EBTR0_OFF & EBTR1_OFF & EBTR2_OFF & EBTR3_OFF
__Config Config7H, 0x40 ;EBTRB_OFF
Endasm-
Declare Reminders On
;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal = 8 
Declare All_Digital = true
OSCCON = %01100000
Declare LCD_Type 0
Declare LCD_DTPin PORTB.4
Declare LCD_ENPin PORTB.2
Declare LCD_RSPin PORTB.3
Declare LCD_Interface 4
Declare LCD_Lines 4
    Dim i As Byte 
    Dim q As Float           'quantité litre/min
    Dim totalQ As Float     ' total
    totalQ = ERead 3
    Dim X As Float           ' fact div
    Dim f As Word            'fréquence sensor
    Symbol pin = PORTA.4
    TRISA=%11111111     ' port A en entrée       
     
Cls
Print At 1,1,"debitmetre"
     
Main: q=0
      f=0
      totalQ=0
       Cls
     While 1=1

     f = Counter pin,1000 'f=11* q (litre/min)
     If f>0 Then
      q=f/11
      totalQ=totalQ+q
     Print At 1,1,"totalQ = " ,Dec2 totalQ, "         "
     Print At 2,1,"freq  f= " ,Dec2 f, "        "
     Print At 3,1,"debit q= " ,Dec2 q, "        "                         
'     Print At 4,1,"quant T= ", Dec3 X, "        " 
     ElseIf f=0  Then
     totalQ=totalQ + q
     EWrite 3,[totalQ]
     DelayMS 10
     Cls
       X=totalQ /44
     
      Print At 1,5,"test"
'     Print At 2,1,"freq  f= " ,Dec2 f, "       "
'     Print At 3,1,"debit q= " ,Dec2 q, "       "                         
     Print At 4,1,"total T= ", Dec2 X, "       "
     DelayMS 2000

     End If

     Wend
    GoTo Main

               

 End   





RGV250

Hi,
I do not do a lot with EEPROM but I would look at your code as when f=0 I think you will be incrementing the value by q continually which may burn out the EEPROM quite quickly if there is no flow.

Bob

trastikata

Just to mention something, besides the EEPROM constant writing issue which RGV250 mentioned...

- when f = 0, you add q to totalQ, but q is holding the value from the last time when f was > 0

- you don't need the "DelayMS 10" after "EWrite 3,[totalQ]" because you have "DelayMS 2000" at the end

- the If statement is ended by EndIf and not End If

- you don't need thos two lines "GoTo Main" and "End" at the bottom of the program, you are already in the "While 1 = 1 : Wend" loop

- You are using only one EEPROM address - so if the power is cut while writing in the EEPROM, you will lose the data - Write in three addresses and at the beginning of the program, read the three and compare them, if different, use both holding the same value and update the EEPROM with the correct values. 

keytapper

I would say that measuring quantity by those small turbines with hall sensor it's rather imprecise and leading some huge variation. Particularly with different flow speeds.
Ignorance comes with a cost