B4R Question Strange: sensor code is slowing down the button

peacemaker

Expert
Licensed User
Longtime User
Hi, All

Very strange situation:

1) I had the first old code1 for ds18b20 sensor https://www.b4x.com/android/forum/threads/initialization-inside-inline-c-from-b4r.150832/, where there is double declaration of the pin number, B4R and also in the Inline-C code. I do not like such code, but it worked OK. Objects are static.

2) Recently AI helped me to make the code2 snippet with dynamically creating inline-C objects, and passing the pin number at just one place: https://www.b4x.com/android/forum/threads/ds18b20-1wire-temperature-sensor-b4r-pin-variable.168124/

3) But today i have found that this code2 ... makes some trouble, that looks like a slowdown of the button pin processing: the short-click is not caught, during 100 ms. Only long-click is caught OK.
And this code3 again works OK:

With static objects and B4R pin variable::
'Module name = "esp_ds18b20", (c) Peacemakerv
'v.1.5
'Libs: https://github.com/PaulStoffregen/OneWire; https://github.com/milesburton/Arduino-Temperature-Control-Library
'1wire port is defined below in Inline-C code

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Private pin1wire As Pin
    Dim pinNumber As Byte = 3    'setup before start
    Public temperature_ds18b20 As Float    'centigrees
    Private Timer1 As Timer
    Dim Ready_flag As Boolean    'ignore
End Sub

Sub Start_ds18b20
    Log("Start reading ds18b20...")
    pin1wire.Initialize(pinNumber, pin1wire.MODE_INPUT_PULLUP)
    RunNative("setup_ds18b20", pinNumber)
    Timer1.Initialize("Timer1_Tick", 1000)
    Timer1.Enabled = True
End Sub

Sub Stop
    Timer1.Enabled = False
    Log("ds18b20 stopped")
End Sub

Private Sub Timer1_Tick
    RunNative("read_ds18b20",Null)
    Log("ds18b20 temp=", others.Numb2(temperature_ds18b20))
    Ready_flag = True
End Sub

#if C
// Include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>

// DS18B20 temperature sensor data wire is plugged into pin 23

// Setup a oneWire instance to communicate with any OneWire devices
    OneWire oneWire(b4r_esp_ds18b20::_pinnumber);

// Pass oneWire reference to Dallas Temperature.
    DallasTemperature sensors_ds18b20(&oneWire);

void setup_ds18b20 (B4R::Object* unused) {
  // Start up the library.
  sensors_ds18b20.begin();
  sensors_ds18b20.setResolution(12); //ADC resulution 9...12
}

void read_ds18b20 (B4R::Object* unused) {
//    Request temperature readings from ALL devices on the bus
    sensors_ds18b20.requestTemperatures();
    delay(20);
//    You can have more than one DS18B20 on the same bus. 0 refers to the first IC on the wire. sensors_ds18b20.getTempCByIndex(0) = °C, sensors_ds18b20.getTempFByIndex(0) = °F
     b4r_esp_ds18b20::_temperature_ds18b20 = sensors_ds18b20.getTempCByIndex(0);
}
#End if

But why such is OK (with static objects and B4R pin variable) ?
 
Top