B4R Question attachInterrupt

AndroidMadhu

Active Member
Licensed User
Hello
I m working on water flow sensor project and here to work with the water rotor system I need to use
attachInterrupt().
Could anyone suggest how I can make working water flow sensor without attachInterrupt()????
 

AndroidMadhu

Active Member
Licensed User
In Water flow sensor the attachInterrupt() function is required to get the pulse for the flow.
Do I need to include attachInterrupt() in inline C/C++?
Please suggest
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Use the forum search engine, there many rWire related examples.
Note that rWire is part is the standard B4R libs
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Hello @Erel
Thanks for the Link. The only problem is I cannot understand as how to incorporate the code using rWire library.
Here is the code written in arduino. I am fine with all the calculation regarding water sensor. [YF-S201].
But the only point I am not able to incorporate the attachInteruppt() using rWire.
Below is the code from Arduino

B4X:
void flow () // Interrupt function
{
   flow_frequency++;
}
void setup()
{
   pinMode(flowsensor, INPUT);
   digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
   Serial.begin(9600);
   attachInterrupt(0, flow, RISING); // Setup Interrupt  <-------------- Here I am facing Issue
   sei(); // Enable interrupts   <--------- Here I am facing issue
   currentTime = millis();
   cloopTime = currentTime;
}

Could you please advice on this please.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
I am using YF-S201 water flow sensor to get the pulse when the water passing through the rotor.
I am using the below code but nothing is working...
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private pin2 As Pin
    Private timer1 As Timer
    Private counter As UInt
    Dim set= False As Boolean
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pin2.Initialize(2,pin2.MODE_INPUT_PULLUP)
    pin2.AddListener("pin2_StateChanged")
    timer1.Initialize("timer1_Tick",1000)   
End Sub

Private Sub timer1_Tick()
    Log(counter)
    counter=0
    set= False
End Sub

Private Sub pin2_StateChanged(state As Boolean)
    Select state
        Case True
            counter=counter+1
        Case False
    End Select
End Sub

Am I doing any mistake ?
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
B4X:
timer1.enabled=true

Maybee you should add the conversion to Liter/second.
B4X:
   Log(counter/450)
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Sorry... My bad... I enabled the timer. But Still I am not able to get the Number of counters.
Only thing I am getting is like below :
B4X:
AppStart
0
0
0
0
0
0
0
0
0

I have incorporated
B4X:
Log(counter/450)
... but no luck...
Can anyone please advice
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
What device are you using (esp8266, arduino, esp32, etc.)? It almost sounds like something is not wired right or possibly the pin you are using is not a pin that support INPUT_PULLUP pin mode (hence asking for the device and pin your sensor is connected to).

I would try a real simple example that doesn't involve listeners or anything just in the loop, log the pin state and manually connect your pin to ground or 3.3v/5v (through a 10K or so resistor of course). If you can see the state change then try adding the listener and see if it catches it.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Thank you @miker2069 for your advice and support. I have completed the measurement of Liquids using Water flow sensor .
Below is the complete Code snippet..
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Private pin2 As Pin
    Private timer1 As Timer
    Private counter As UInt
    Private set= False As Boolean
    Private flowrate As Double
    Private totalmililiter=0 As Double
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pin2.Initialize(2,pin2.MODE_INPUT_PULLUP)
    pin2.AddListener("pin2_StateChanged")
    timer1.Initialize("timer1_Tick",2000)
    timer1.Enabled=True   
End Sub

Private Sub timer1_Tick()
    'Start the math
    'As per the datasheet 450 Pulse per Liter. So we can get 1000/450 or 2.25ML per Pulse.
    flowrate=(counter*2.25)
    flowrate=flowrate*60 'ML(MilliLitres) per minutes
    'Conversion of MilliLitres to Liters
    flowrate=NumberFormat((flowrate/1000),0,2)
    totalmililiter=(totalmililiter+flowrate)
    Log("Flowrate: ",flowrate, " Liter/Minute")
    'Print the cumulative total of litres flowed since starting
    Log("Total Output Liquid Quantity: ", NumberFormat(totalmililiter,0,2))
    'Log("Counter is :",counter)
    counter=0     'Resetting the Counter
    set= False
    
End Sub

Private Sub pin2_StateChanged(state As Boolean)
    Select state
        Case True
            counter=counter+1
        Case False
    End Select
End Sub

If there is any changes required to fine tune the code please feel free to advice on this.
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
Great that you finished it (so I assume it's all working)? What turned out to be the issue (curious). It looks like you really only changed the timer fire count from 1s to 2s and you're only increment your counter when states is true? Did it turn out to be a wiring/hardware issue?
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
@miker2069 , Yes... It is working fine as of now. :).
I have only changed the timer from 1s to 2s and added 10k ohm resister [hardware] with the output of Waterflow Sensor. [The yellow one].

Please advice if there are any issues with the code...
 
Upvote 0
Top