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