B4R Question Mega 2560 - accurate ticks and counter

micro

Well-Known Member
Licensed User
Longtime User
Hi guys
I've only done a few things with B4R so I'm not quite prepared.
I have a Mega 2560, I need to have a trick accurate every 0.1 ms to increment a long type variable and count input pulses, for counting I would like to use two of the 16 bit counters but I don't know how to initialize them and I don't know which pins they correspond to.
Is it better to use the counters or use the interrupt to capture the pulses?
Are they fast pulses coming out of an encoder (pulses already separated, up and down)
Do you have any examples to propose on how to initialize timers as counters to capture external pulses and to generate a cadence every 0.1 ms?
Thanks to all
 

peacemaker

Expert
Licensed User
Longtime User
B4R does not support the interruptions, it's the universal wrapper over Arduino, the transpiller of B4X-language into Arduino's C-language code.
 
Last edited:
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User

https://www.b4x.com/android/forum/threads/timers-loopers-and-callsubplus.65989/#content - check about AddLooper
Thanks peacemaker

One more piece of information
How are the 4 serials present on the mega 2560 defined in B4R?
Serial1
Serial2
........
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
defined in B4R
IMHO, only one Serial can be used by the B4R libs, other interfaces can be used after switching, or by SoftwareSerial

For the internal counter usage - only C-code can be used, somehow: https://forum.arduino.cc/t/count-in...t-counter-present-in-arduino-mega-2560/693431
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Hi
another question
I have a long variable into the code and the behavior is different between Log and astream1.Write (astream1 is connected to the other free serial of mega2560).
Example:
B4X:
varlong = "12345678"
With
B4X:
Log(varlong, CRLF)
the value written in the window logs is correct, but if I do
B4X:
Dim buff() As Byte = JoinBytes(Array(bc.LongsToBytes(Array As Long(varlong)), Array As Byte(13)))
astream1.Write(buff)
The values received are wrong, where am I going wrong?
Thanks
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
No idea, it's another topic.
And strange that "long" is string.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
No idea, it's another topic.
And strange that "long" is string.
Sorry I misrepresented the value, it is without quotes, varlong is a variable that is always incremented and in my example the value reached is 12345678
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Sorry I misrepresented the value, it is without quotes, varlong is a variable that is always incremented and in my example the value reached is 12345678
Okay, it was my mistake, now work.
Only one major problem, I used the StateChanged function to capture the econder pulses but some count loses it.
Can In line C be used to interrupt on pin state change or a more effective method?
Thanks
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
IMHO, only one Serial can be used by the B4R libs, other interfaces can be used after switching, or by SoftwareSerial

For the internal counter usage - only C-code can be used, somehow: https://forum.arduino.cc/t/count-in...t-counter-present-in-arduino-mega-2560/693431
Hi Peacemaker
I have one app using all 4 serial all together on a Mega 2560. The issue you describe, is relative to ESP32.
I have updated Arduino library to support hardware handshake.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Link to the update lib is better to mention
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Then
just a refresher, with B4R it is much more familiar to produce code but I had to switch directly to working with the Arduino IDE to use the interrupt better.
With StateChanged I can discriminate with a symmetrical square wave and from the width of at least 400 us all pulses and just better with Looper but not so reliable counting especially if I insert an acceleration to the incremental encoder.
For additional serials I repeat no problem with B4R.

Thanks to all anyway as always.
 
Upvote 0
Top