B4R Question HX711 inline C error

Mostez

Well-Known Member
Licensed User
I'm trying to include HX711 library into inline C code, but I get this error, any suggestions?, Main and HX711 module:

Thanks

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    HX711.Begin(16,17)
End Sub

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private DataPin,CLKpin As Byte
   
End Sub

Public Sub Begin(sData As Byte , sCLK As Byte)
    DataPin = sData
    CLKpin = sCLK
    RunNative("begin", Null)
End Sub

Public Sub Tare()
    RunNative("tare", Null)
End Sub


#If C
#include <HX711.h>
void begin(B4R::Object* o) {HX711.begin(b4r_hx711::_datapin,b4r_hx711::_clkpin);}
void tare(B4R::Object* o) {HX711.tare();}

#End If


B4R version: 1.80 BETA #2
Parsing code. (0.00s)
Compiling code. (0.01s)
Building project (0.01s)
Compiling & deploying Ino project (Arduino/Genuino Mega or Mega 2560 - COM35) Error
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
sketch\b4r_hx711.cpp: In function 'void begin(B4R::Object*)':
b4r_hx711.cpp:9: error: expected unqualified-id before '.' token
void begin(B4R::Object* o) {HX711.begin(b4r_hx711::_datapin,b4r_hx711::_clkpin);}
^
sketch\b4r_hx711.cpp: In function 'void tare(B4R::Object*)':
b4r_hx711.cpp:10: error: expected unqualified-id before '.' token
void tare(B4R::Object* o) {HX711.tare();}
^
exit status 1
 

rwblinn

Well-Known Member
Licensed User
Hi,

seems HX711 object is not declared in InlineC code - see the examples of the C library.
This compiles - but can not test as do not have such a device:
B4X:
#If C
#include <HX711.h>

HX711 scale;

void begin(B4R::Object* o)
{
    scale.begin(b4r_hx711::_datapin,b4r_hx711::_clkpin);
}

void tare(B4R::Object* o)
{
    scale.tare();
}
#End If
 
Upvote 0

derez

Expert
Licensed User
I use a modified code in arduino for use of HX711 with a 4-digit display, but would like to re-write it for B4R.
B4X:
/*
 Example using the SparkFun HX711 breakout board with a scale
 By: Nathan Seidle
 SparkFun Electronics
 Date: November 19th, 2014
 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
 This example demonstrates basic scale output. See the calibration sketch to get the calibration_factor for your
 specific load cell setup.
 This example code uses bogde's excellent library:  https://github.com/bogde/HX711
 bogde's library is released under a GNU GENERAL PUBLIC LICENSE
 The HX711 does one thing well: read load cells. The breakout board is compatible with any wheat-stone bridge
 based load cell which should allow a user to measure everything from a few grams to tens of tons.
 Arduino pin 2 -> HX711 CLK
 3 -> DAT
 5V -> VCC
 GND -> GND
 The HX711 board can be powered from 2.7V to 5V so the Arduino 5V power should be fine.
*/
#include "HX711.h"
#include <TM1637Display.h>
#define calibration_factor 345.5 //This value is obtained using the SparkFun_HX711_Calibration sketch
// pins for the HX
#define DOUT  3
#define CLK  2
// pins for the display
const int TMCLK = 6; //Set the CLK pin connection to the display
const int TMDIO = 7; //Set the DIO pin connection to the display
HX711 scale(DOUT, CLK);
TM1637Display display(TMCLK, TMDIO);  //set up the 4-Digit Display.
 float weight ;
void setup() {
 // Serial.begin(9600);
 // Serial.println("HX711 scale demo");
  scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
  scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
 // Serial.println("Readings:");
  display.setBrightness(0x0a);  //set the diplay to maximum brightness
}
void loop() {
 
  //Serial.print("Reading: ");
  weight = scale.get_units() ; //scale.get_units() returns a float
  //Serial.print(weight);
 // Serial.print(" gram"); //You can change this to kg but you'll need to refactor the calibration_factor
  //Serial.println();
  display.showNumberDec(weight);
  delay(500);
 
}
 
Upvote 0

derez

Expert
Licensed User
Got it to work:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private tmr As Timer
    Private Weight As Float
    Private tm As TM1637Display
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    tm.Initialize(6, 7)  ' clock pin and dataout pin of the display
   
    tm.SetBrightness(7, True)
   
    RunNative("Begin", Null)
    RunNative("Set_scale", Null)
    RunNative("Tare", Null)
   
     tmr.Initialize("tmr_Tick",500)
     tmr.Enabled = True
End Sub

Sub tmr_tick
    RunNative("Get_units", Null)
    Log(Weight)
    If Weight < 10 Then Weight = 0
    tm.ShowNumberDec(Weight)
End Sub

#If C
#include <HX711.h>
#define DOUT  3
#define CLK  2
#define calfactor 329

HX711 scale( DOUT , CLK);

void Begin(B4R::Object* o)
{
    scale.begin(DOUT , CLK);
}

void Set_scale(B4R::Object* o)
{
    scale.set_scale(calfactor);
}

void Tare(B4R::Object* o)
{
    scale.tare();
}

void Get_units(B4R::Object* o)
{
    b4r_main::_weight = scale.get_units();
}
#End If
 
Upvote 0

derez

Expert
Licensed User
The following is the calibration program for the scale. I modified slightly the inline c.
You should use B4R serial connector and the inputs are as defined in the Astream_newdata sub.
Start with no weight and put a known weight on the scale, then change the calibration factor to meet the known weight.
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private astream As AsyncStreams
    Private tmr As Timer
    Private Weight As Float
    Private bc As ByteConverter
    Private Calfactor = 329, mult = 1 As Float
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    astream.Initialize(Serial1.Stream, "Astream_NewData", "Astream_Error")
   
    RunNative("Begin", Null)
    RunNative("Set_scale", Null)
    RunNative("Tare", Null)
   
    tmr.Initialize("tmr_Tick",1000)
'    tmr.Enabled = True
End Sub

Sub Astream_NewData (Buffer() As Byte)
    Log("Received: ", Buffer)
    If Buffer = "a" Then Calfactor = Calfactor + mult
    If Buffer = "z" Then Calfactor = Calfactor - mult
    If Buffer = "q" Then mult = 10*mult
    If Buffer = "w" Then mult = mult/10
    RunNative("Set_scale", Null)
    Delay(100)
    RunNative("Get_units", Null)
    Delay(100)
    Log("Weight= " , Weight, "  cal factor= " , Calfactor, " mult= " ,mult)

End Sub

Sub AStream_Error
    Log("error")
End Sub

Sub tmr_tick
    RunNative("Get_units", Null)
    Log(Weight)
End Sub

#If C
#include <HX711.h>
#define DOUT 3
#define CLK  2
//#define calfactor 329

HX711 scale( DOUT , CLK);

void Begin(B4R::Object* o)
{
    scale.begin(DOUT , CLK);
}

void Set_scale(B4R::Object* o)
{
    scale.set_scale(b4r_main::_calfactor);
}

void Tare(B4R::Object* o)
{
    scale.tare();
}

void Get_units(B4R::Object* o)
{
    b4r_main::_weight = scale.get_units();
}
#End If
 
Upvote 0
Top