B4R Question Would I benefit from B4R as an experienced Arduino developer?

Martin Larsen

Active Member
Licensed User
Longtime User
For several years I have created many useful projects with Arduinos, Attinys and ESP8266. Such as light controls and timers, greenhouse watering and WIFI remotes for my TV etc.

But I also develop with B4A and in the last couple of months, B4i. And I like the B4X environment...

So my question is, would I benefit from B4R?

I guess for many developers the B4R language is much more accessible than Arduino C which in that case of course is a good for reason for using B4R. But I am used to C so the language itself would not be the primary reason.

What other advantages are there which might not be so apparent until you start working with B4R?

I see at least two disadvantages:

1. There is a huge number of C libraries. And not so many for B4R (in comparison).

(But it seems to be quite manageable to convert the libraries)

2. There is a huge number of C examples and forum posts which make a lot of work a matter of copy and paste.

(But in many cases you can use inline C and call it a day)
 

Cableguy

Expert
Licensed User
Longtime User
...

So my question is, would I benefit from B4R?

After reading you, I would put it in another way... "How and how much would this community benefit from your knowledge?"
And the answer would "Tons!"
You seem to be quite at ease with Arduino and it's C language, and, since most "not yet wrapped" libraries are being used directly with the inline C feature, you could make a huge difference in this Forum, firstly by taking a lot off EREL's shoulders since he is the one we turn to to ask for a particular wrapper, and secondly, by helping the general community of "not so C oriented" developers.
You have the power to become a huge player in B4R development
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
Thanks for the feedback. I will give B4R a try.

I am planning a major upgrade of my greenhouse watering system, this time with an accompanying Android app and ESP8266 instead of Attiny85.

I have decided to make it in B4R and see how it goes.
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
The advantage I've seen with using B4R is in how it works with the other B4X products. Using it to communicate easily between the various platforms (desktop/RasPi for B4J, Android with B4A, iOS with B4i). I can pass objects around (with some limitations on the B4R side regarding some types) and leverage the strengths of each platform while staying within the same coding eco-system. For those projects where I'm just staying (and know I'll be staying) on Arduino, and it's something somewhat complex, I'll just stay in C.
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
The advantage I've seen with using B4R is in how it works with the other B4X products. Using it to communicate easily between the various platforms (desktop/RasPi for B4J, Android with B4A, iOS with B4i). I can pass objects around (with some limitations on the B4R side regarding some types) and leverage the strengths of each platform while staying within the same coding eco-system. For those projects where I'm just staying (and know I'll be staying) on Arduino, and it's something somewhat complex, I'll just stay in C.

This is also why I think my watering project would be a good candidate as I want to make an Android client. I am looking forward to see how well these two platforms integrate.
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
I just made my first blink program :)

One drawback I have noticed is that compilation + upload is much slower. The blink example takes 19 secs to upload. Using Arduino IDE it takes 3 seconds.

For real-life projects this might be a lesser issue, though, as most of the time is spent thinking, not uploading.

Also, the B4R core takes up a lot relatively speaking for tiny examples like this. For real projects using several libraries, the time difference will probably be less.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The Time differences are easily explained by the fact that B4R kinda bootstraps the Arduino IDE for come of the tasks, such as uploading the "sketch" to the board.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
One drawback I have noticed is that compilation + upload is much slower. The blink example takes 19 secs to upload. Using Arduino IDE it takes 3 seconds.
It will be faster when you compile again as many resources will be reused.

The compilation speed should be quite similar to compilation with Arduino IDE as both use the same compiler.

(Last reply for me here, I'm not going to convince you to use B4R.)
 
Last edited:
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
I'm not going to convince you to use B4R.

You don't have to. After looking more into B4R I am sure I can benefit from it, especially for projects involving an Android client.

Also, the more event driven nature of B4R is perfect for my watering project as it is built around timers. The pump should start at certain times, run for a set number of minutes and stop again etc.

Right now I keep track of these watering events in the main loop by counting the millis, but B4R's CallSubPlus is perfect for this as I can schedule an event and let B4R do the house keeping.

I am actually very excited to get started!
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
I have already rewritten the code in B4X. It was a joy!

The code takes up half the numbers of lines and it is much more readable.

This project is very well-suited for B4R since it is built around timers. It uses this remote control:



Each Button A to D can be programmed (in my project) to give a certain amount of rainwater from my rainwater barrel via a 12V pump. By long-pressing a button the watering system enters a "Learn mode" where I manually stop the pump by pressing the button again. The amount of water pumped out is then stored in the EEPROM for the next time I press the button. So this a programmable watering system for my greenhouse. There is also a timer function where it will water periodically.

In the C code I keep track of the timings in the main loop. This is cumbersome, error prone and not very readable. However, it has worked flawlessly for 3 years. And doesn't take up much code so it runs off an Attiny85.

With B4X I utilize the CallSubPlus function. It turns out to be nearly perfect for the timers as they are essentially one-shot timers (except the periodic watering). I say nearly because there is catch. Sometimes a timer should be cancelled, namely if I choose to stop the pump before it turns off automatically. I have worked around this problem by
using the tag parameter, but I will make a wish about a small but useful augmentation of CallSubPlus.

My adventure is a three-step process. Now I have simply converted the code to B4X to replicate the same functionality. I have tested it with an Arduino, substituting the remote controller with a wire from ground into of the inputs and with LED 13 instead of the pump. It works!

Next, I will try to upload the code to an Attiny85. With the Serial class and logging functions it takes up way to much space and memory to fit. 10000+ bytes and there is only room for 8192 on the small chip! But the logging doesn't work anyway for Attiny (not even in C) and if I out-comment the Log statements, the code takes up 6322 bytes and uses 442 bytes of ram for global variables out of 512. That is still twice as much as the C code, but it should be fine. If it works, I will physically replace the Attiny85 with one with the B4X code to try it out for real.

The third and final step is to replace the Attiny with an ESP8266 or one of its relatives for wifi access. My plan is to make a simple Android companying app for controlling and monitoring my watering project. It is this final step that made me decide it was worth converting the code to B4X as I believe it will be much easier when they have to talk to each other.

Thanks for convincing me :)
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You should now turn this project to a post in the "share my creation" sub-forum for other to profit from your journey
 
Upvote 0

jwgf

Member
Hello, I am going to give my opinion as a new user in B4X. I have been designing industrial automation for many years, working in assembler and C, when I entered the Arduino world, it was not difficult for me since only a few things changed. Very recently I got to know the B4X environment, and I only have words of thanks for the creators of such a wonderful tool. It is true that you have to change the way of thinking linear programming to which one is accustomed, but the advantages are spectacular. I have performed tasks in which I should have used interrupts, but the task queue that is generated works almost like a hardware interrupt. Also the connection with applications in B4A using socket is wonderful. The community always helps !! Finally, the ease and help that B4R gives to writing and documentation, which has no comparison with the Arduino IDE. P.S. sorry for my English
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
and if I out-comment the Log statements,
Hej Martin
B4R has a little flaw, you must have a log statement in the timer event or the timer will eventually stop.
I do it by having a build configuration called DEV
B4X:
Sub tim_Tick
    #if not(DEV)
        Log(" ")
    #End If
End Sub
 
Upvote 0
Top