B4R Question My first approach difficult to convert into B4R

micro

Well-Known Member
Licensed User
Longtime User
Hi to all
this is my first attempt to code development for B4R
How convert optimally this routine?
B4X:
long getValue()
{
 
byte data[3];

    while (digitalRead(DOUT))
        ;

    for (byte j = 3; j--;)
    {
        for (char i = 8; i--;)
        {
            digitalWrite(SCK, HIGH);
            bitWrite(data[j], i, digitalRead(DOUT));
            digitalWrite(SCK, LOW);
        }
    }

    digitalWrite(SCK, HIGH);
    digitalWrite(SCK, LOW);

    data[2] ^= 0x80;

    return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8)
            | (uint32_t) data[0] << 1;
}

Where the SCK and DOUT pins are respectively A3(output) and A2(input)
Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private sck, dout As Pin
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Log("AppStart")
   sck.Initialize(sck.A3, sck.MODE_OUTPUT)
   dout.Initialize(dout.A2, dout.MODE_INPUT)
   Log(GetValue)
End Sub


Sub GetValue As Long
   Do While dout.DigitalRead
     
   Loop
   Dim data(3) As Byte
   For j = 2 To 0 Step -1
     For i = 7 To 0 Step -1
       sck.DigitalWrite(True)
       If dout.DigitalRead Then Bit.Set(data(j), i) Else Bit.Clear(data(j), i)
       sck.DigitalWrite(False)
     Next
   Next
   sck.DigitalWrite(True)
   sck.DigitalWrite(False)
   data(2) = Bit.Xor(data(2), 0x80)
   Dim u1 As ULong = Bit.ShiftLeft(data(2), 16)
   Dim u2 As ULong = Bit.ShiftLeft(data(1), 8)
   Dim u3 As ULong = Bit.ShiftLeft(data(0), 1)
   Return Bit.Or(Bit.Or(u1, u2), u3)
End Sub
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks Erel for your availability, I tried but work bad.

Con this code in arduino
B4X:
void loop() {
  String str;
  str += getValue();
  Serial.print(str + "\n")
  delay(1000);
}

I have this output:
B4X:
8493424
8493562
8493394
8493450
8493388
8493430
8493408
8493504
8493444
8493322
8493368
8493358
8493540
8493390
8493408

and this output with B4R code (your code)
B4X:
39418
39264
39330
39174
39166
39422
39248
39362
39312
39352
39196
39368
39316
39390
39324


it seems to be missing the MSB and part of the next byte
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks for your patience but i have same problems
I have declared in Process Global a long variable result
and this segment code for loop reading
B4X:
Private Sub TimerRead_Tick
    RunNative("readvalue", Null)
    Log(result)
    Delay(1000)
End Sub
and at the end of code this inline C
B4X:
#if C
void readvalue (B4R::Object* o){
b4r_main::_result = GetValue();
long GetValue()
{

byte data[3];

    While (digitalRead(16))
        ;

    For (byte j = 3; j--;)
    {
        For (char i = 8; i--;)
        {
            digitalWrite(17, HIGH);
            bitWrite(data[j], i, digitalRead(16));
            digitalWrite(17, LOW);
        }
    }

    digitalWrite(17, HIGH);
    digitalWrite(17, LOW);

    data[2] ^= 0x80;

    Return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8)
            | (uint32_t) data[0] <

}
}
#end if

But i have this error

B4X:
...........
sketch\b4r_main.cpp: In function 'void readvalue(B4R::Object*)':
b4r_main.cpp:16: error: 'GetValue' was not declared in this scope
b4r_main::_result = GetValue();
                              ^
b4r_main.cpp:18: error: a function-definition is not allowed here before '{' token
{
^
b4r_main.cpp:206: error: expected '}' at end of input
}
^
exit status 1
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

try below - have used information from the previous threads - the code compiles without error, but can not check the result as do not have a test setup.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private sck, dout As Pin
    Private result As Long    'ignore
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    sck.Initialize(sck.A3, sck.MODE_OUTPUT)
    dout.Initialize(dout.A2, dout.MODE_INPUT)
    RunNative("getvalue", Null)
    Log(result)
End Sub

#if C
long getvalue(B4R::Object* o) {
     byte data[3];
     while (digitalRead(16))
       ;

     for (byte j; j = 3; j--){
       for (char i; i = 8; i--){
         digitalWrite(17, HIGH);
         bitWrite(data[j], i, digitalRead(16));
        digitalWrite(17, LOW);
       }
     }

     digitalWrite(17, HIGH);
     digitalWrite(17, LOW);

     data[2] ^= 0x80;

     b4r_main::_result = ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8) | (uint32_t) data[0] << 1;
}
#end if
 
Last edited:
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Update to post #8
  • reworked the Inline C code because the loops did not work and tested with pins 10 & 9 resulting in value of 8388608.
  • Note the usage of ::Serial.println in InlineC - the output is written to the B4R IDE log = good way of debugging.

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private sck, dout As Pin
    Private result As Long    'ignore
    Private sckpin As Byte = 10        '17
    Private doutpin As Byte = 9        '16
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Delay(500)
    Log("AppStart")
    sck.Initialize(sck.A3, sck.MODE_OUTPUT)
    dout.Initialize(dout.A2, dout.MODE_INPUT)
    RunNative("getvalue", Null)
    'Example result = 8388608
    Log(result)
End Sub

#if C
long getvalue(B4R::Object* o) {
        ::Serial.println("start");
        byte data[3];
        while (digitalRead(b4r_main::_doutpin));

        for (int j = 3; j > -1; j--){
            ::Serial.println(j);
            for (int i = 8; i > -1; i--){
                digitalWrite(b4r_main::_sckpin, HIGH);
                bitWrite(data[j], i, digitalRead(b4r_main::_doutpin));
                digitalWrite(b4r_main::_sckpin, LOW);
                ::Serial.println(i);
            }
        }

        digitalWrite(b4r_main::_sckpin, HIGH);
        digitalWrite(b4r_main::_sckpin, LOW);

        data[2] ^= 0x80;
        ::Serial.println("done");

        b4r_main::_result = ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8) | (uint32_t) data[0] << 1;
}
#end if
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
Thanks Rob
I appreciate your interest but not work.
This is the output:
B4X:
14907390
14880766
15040510
14968830
15138814
15099902
14952446
14944254
15052798
15063038
15048702
15015934
and it is incorrect :(
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

suggest to take a step back and try the full C++ code in the Arduino IDE and check again if the results are ok.
May be you can share also the circuit and the devices/sensors used plus the controller, UNO?/MEGA?/ESP? or other ... and the full B4R code.

The code I shared in post #9 ran on an UNO with LEDs connected to pins D10 & D9 - with a result of 8388608.
 
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
suggest to take a step back and try the full C++ code in the Arduino IDE and check again if the results are ok.
May be you can share also the circuit and the devices/sensors used plus the controller, UNO?/MEGA?/ESP? or other ... and the full B4R code.
The code in arduino ide (device arduino nano) work, the final project work fine and the sub is that posted
B4X:
#include <stdlib.h>
#include <Serial.h>
#include <Stdio.h>
#include <Arduino.h>

void setup() {
pinMode(16, INPUT);
pinMode(17, OUTPUT);
  Serial.begin(9600);
  Serial.flush();
  delay(1000);
  digitalWrite(17, HIGH);
  delayMicroseconds(100);
  digitalWrite(17, LOW);
}

void loop() {
  String str;
  str += getValue();
  Serial.print(str + "\n");
  delay(1000);
}

long getValue()
{

byte data[3];

    while (digitalRead(16))
        ;

    for (byte j = 3; j--;)
    {
        for (char i = 8; i--;)
        {
            digitalWrite(17, HIGH);
            bitWrite(data[j], i, digitalRead(16));
            digitalWrite(17, LOW);
        }
    }

    digitalWrite(17, HIGH);
    digitalWrite(17, LOW);

    data[2] ^= 0x80;

    return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8)
            | (uint32_t) data[0] << 1;
}

The sensor is Hx711.
I get the result 8388608 if disconnect A2 and A3 from device.

Also on Arduino IDE the result is the same if disconnect A2 and A3

Thanks
 
Last edited:
Upvote 0

micro

Well-Known Member
Licensed User
Longtime User
SOLVED
I put the original 'for' double cycle
B4X:
for (byte j = 3; j--;){
 for (char i = 8; i--;){
 digitalWrite(17, HIGH);
 bitWrite(data[j], i, digitalRead(16));
 digitalWrite(17, LOW);
 }

Thanks
 
Upvote 0
Top