Skip to content

[Blockly][Codegenerator] sensor polling in function instead of loop #486

@PaulaScharf

Description

@PaulaScharf

Block description

For many sensors the code for polling new sensor values is simply added to the loop. If I now for example want to check a sensor value during setup it still generates the code for polling the sensor in the loop instead of in the setup.

For example this blockly code:
Image
will result in this arduino code being generated:

// Code generated by senseBox Blockly on Fri May 30 2025 16:03:04 GMT+0200 (Central European Summer Time)

#include <Adafruit_DPS310.h> // http://librarymanager/All#Adafruit_DPS310

Adafruit_DPS310 dps;

void setup() {

  Serial.begin(300);
  dps.begin_I2C(0x76);

  dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES);
  dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES);

  Serial.println(pressure_event.pressure);

}

void loop() {
  sensors_event_t temp_event, pressure_event;
  dps.getEvents(&temp_event, &pressure_event);

}

As can be seen the "getEvents()" call is done in the loop, even though the value is being used beforehand in setup. In this case compilation will actually fail, because the pressure_event was not declared yet.

Expected block behaviour

Some sensors already generate a separate function to return current sensor values. This should be done for all sensors.
The correct behavior is for example implemented for the water temperature sensor. This blockly code:
Image
will result in this arduino code being generated:

// Code generated by senseBox Blockly on Fri May 30 2025 16:01:53 GMT+0200 (Central European Summer Time)

#include <OneWire.h> // http://librarymanager/All#OneWire
#include <DallasTemperature.h> // http://librarymanager/All#DallasTemperature

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float getWaterTemp(int index){
  sensors.requestTemperatures();
  return sensors.getTempCByIndex(index);
}

void setup() {

  Serial.begin(300);
  sensors.begin();
  Serial.println(getWaterTemp(0));

}

void loop() {

}

Affected Sensors

  • MPU6050
  • SPS30
  • DPS310
  • BME680
  • Button
  • RG-15

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions