B4R Code Snippet Mini Battery Display - Inline C

SubName: Show your battery level on a purpose made mini battery display.
Description: Here is a quick example of inline C being used to power a mini battery display. As there's no library for this display on the forum, I used inline C with the original Arduino IDE source code.

I've slimmed down the inline C code to make it easier to read. You can find the original library on Github, just copy the library files into your Arduino IDE library folder and use the code below.
B4X:
'WIRE LEGEND for TM1651 battery display
'VCC = 3.3V to 5V
'GND = GND
'CLK = D4
'DIO = D2

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("AppStart")

    RunNative("setup", Null)
    AddLooper("Loopey")
End Sub

Sub Loopey
    RunNative("loop", Null)
End Sub

#if C
    #include "TM1651.h"
    #define CLK 4
    #define DIO 2

    TM1651 batteryDisplay(CLK, DIO);

    void charging()
    {
     for(uint8_t level = 0; level < 8; level ++)
     {
       batteryDisplay.displayLevel(level);
       delay(500);
     }
    }

    void setup(B4R::Object* unused)
    {
     batteryDisplay.init();
     batteryDisplay.set(2); //Set brightness from 0 to 7
    }

    void loop(B4R::Object* unused)
    {
     charging();
    }
#End if

Tags: Mini, Battery, Module, Arduino, Inline, C, C++

An UNO board running a mini battery display
.
IMG_20170509_004612.jpg


Enjoy...
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Using Delay with non-small intervals is not scalable. Your program will not be able to do anything else. A better approach is to use Timer or CallSubPlus.

Example (not tested):
B4X:
'WIRE LEGEND for TM1651 battery display
'VCC = 3.3V to 5V
'GND = GND
'CLK = D4
'DIO = D2

Sub Process_Globals
   Public Serial1 As Serial
   Private tmrLevel As Timer
   Private level As Byte
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   RunNative("setup", Null)
   tmrLevel.Initialize("tmrLevel_Tick", 500)
   tmrLevel.Enabled = True
End Sub

Sub tmrLevel_Tick
   RunNative("displayLevel", Null)
   level = (level + 1) Mod 8
End Sub

#if C
  #include "TM1651.h"
  #define CLK 4
  #define DIO 2

  TM1651 batteryDisplay(CLK, DIO);

  void displayLevel(B4R::Object* unused)
  {
  batteryDisplay.displayLevel(b4r_main::_level);
  }

  void setup(B4R::Object* unused)
  {
  batteryDisplay.init();
  batteryDisplay.set(2); //Set brightness from 0 to 7
  }
   
#End if
 

Peter Simpson

Expert
Licensed User
Longtime User
That's a good point for others to take note of @Erel. I will not be using a battery display for anything, I purchased it because it was cheap and just to test in B4R :)

I received the battery display yesterday and first got it working in the Arduino IDE, then moved it instantly over to B4R, so posted the test code that I used. Shortly I will be back home from one of my clients warehouses, I'll test your code but obviously it's going to work already

Thanks for the post move too...
 
Last edited:
Top