B4R Question How can I pass this code to B4R?

Discussion in 'B4R Questions' started by Johan Hormaza, Jun 7, 2018.

  1. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    How can I pass this code to B4R? It involves calculating the revolutions per minute of an engine with the HALL effect sensor
    this is code:

    Code:
    volatile int rpmcount = 0;
    int rpm = 
    0;
    unsigned long lastmillis = 
    0;

    void setup(){
    Serial.begin(9600);
    attachInterrupt(
    0, rpm_fan, FALLING);//interrupt cero (0is on pin two(2).
    }

    void loop(){

    if (millis() - lastmillis == 1000){ /*Uptade every one second, this will be equal to reading frecuency (Hz).*/

    detachInterrupt(0); //Disable interrupt when calculating

    rpm = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/

    Serial.print("RPM =\t"); //print the word "RPM" and tab.
    Serial.print(rpm); // print the rpm value.
    Serial.print("\t Hz=\t"); //print the word "Hz".
    Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/

    rpmcount = 0; // Restart the RPM counter
    lastmillis = millis(); // Uptade lasmillis
    attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
    }
    }

    void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
    rpmcount++;
    }
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Have you tried to implement it with the Pin StateChanged event? It will be less accurate than a solution based on interrupts however it might be accurate enough.
     
  3. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    Thanks EREL, I'm already developing it with that method ...
     
  4. Johan Hormaza

    Johan Hormaza Active Member Licensed User

    I did it this way which works excellent !!!

    Code:
    Sub Process_Globals
        
    Public Serial1 As Serial
        
    Private HallSensor As Int = 2
        
    Private sampleTimer As ULong = 500
        
    'Private maxRPM As Int = 8000
        Private rpmMaximum As Int = 0
        
    Private pinRPM As Pin   
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        pinRPM.Initialize(HallSensor, pinRPM.MODE_INPUT)
        AddLooper(
    "Looper1")
    End Sub
    '...
    Sub Looper1
        Delay(
    100)
        
    Dim rpm As Int = getRPM
        
    If  rpm > rpmMaximum Then rpmMaximum = rpm
        displayRPM(rpm)
    End Sub

    Sub getRPM As Int
        
    Dim count As Int = 0
        
    Dim countFlag As Boolean = False
        
    Dim currentTime As ULong = 0
        
    Dim  startTime As ULong = Millis
        
    Do While currentTime < = sampleTimer
            
    If pinRPM.DigitalRead = True Then
                countFlag = 
    True
            
    End If
            
    If pinRPM.DigitalRead = False And countFlag = True Then
                count = count + 
    1
                countFlag = 
    False
            
    End If
            currentTime = Millis - startTime
        
    Loop
        
    Dim countRPM As Int = (60000 / sampleTimer) * count
        
    Return countRPM
    End Sub

    Sub displayRPM( rpm As Int)
        
    Log("RPM: ",rpm,"  MAX RPM: ", rpmMaximum)
    End Sub
    This log

    Code:
    AppStart
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    0  MAX RPM: 0
    RPM: 
    960  MAX RPM: 960
    RPM: 
    1920  MAX RPM: 1920
    RPM: 
    2280  MAX RPM: 2280
    RPM: 
    2280  MAX RPM: 2280
    RPM: 
    2280  MAX RPM: 2280
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    1920  MAX RPM: 2400
    RPM: 
    2160  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2280  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2400  MAX RPM: 2400
    RPM: 
    2040  MAX RPM: 2400
    RPM: 
    1440  MAX RPM: 2400
    RPM: 
    960  MAX RPM: 2400
    RPM: 
    720  MAX RPM: 2400
    RPM: 
    360  MAX RPM: 2400
    RPM: 
    120  MAX RPM: 2400
    RPM: 
    0  MAX RPM: 2400
    RPM: 
    0  MAX RPM: 2400
    RPM: 
    0  MAX RPM: 2400
    RPM: 
    0  MAX RPM: 2400
     
    Erel likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice