B4R Question Frequency measuring on MEGA 2560

thetahsk

Active Member
Licensed User
Longtime User
Hi,

I need to measure a frequency on Arduino Mega2560. I found a library for Arduino, which works OK : https://www.pjrc.com/teensy/td_libs_FreqCount.html
Unfortunately I don't know how to use it in B4R.
Could anybody help me with it, please ?

Petr
Assuming the lib is installed and you want to measure square wave signal with TTL level.
Connect your Signal to Pin 47 and don't use Pins 9,10,44,45,46.
Use the Inline C feature of B4R. You need only a few lines of code.
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User

This works for an Arduino Nano clone.
B4R:
#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
    End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("Counting pulses during a fixed time")
   
    
    RunNative("setup",Null)
    Delay(100)
    AddLooper("Looper1")
End Sub

Private Sub Looper1
    RunNative("loop",Null)
End Sub



#if C


#include <FreqCount.h>

void setup(B4R::Object* o){

   FreqCount.begin(1000);  // Gate intervall 1000 ms
}


void loop(B4R::Object* o){
    
if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);   
    }
}

#End if
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…