Hello,
Is somewhere example how use for example distance sensor and potentiometer module in one time?
My guess is that I doing something wrong with event registration because I see modules on the bus but I can read data only from second one
Hello,
Is somewhere example how use for example distance sensor and potentiometer module in one time?
My guess is that I doing something wrong with event registration because I see modules on the bus but I can read data only from second one
Hello. Does this example work?
#include <Totem.h>
// Create objects for modules
TotemModule sensor(11); // Distance sensor module (number 11)
TotemModule pot(15); // Potentiometer module (number 15)
// Function called on module connect (over TotemBUS)
void onModuleConnect(uint16_t number, uint16_t serial) {
// Print connected module information
Serial.printf("Module connected. Number: %d Serial: %d\n", number, serial);
}
// Hold current sensor distance
int distance;
int knob[3];
// Function called on module event
void onModuleData(ModuleData data) {
// Update variables with latest data
if (data.is("distance")) distance = data.getInt();
if (data.is("knobA")) knob[0] = data.getInt();
if (data.is("knobB")) knob[1] = data.getInt();
if (data.is("knobC")) knob[2] = data.getInt();
}
// Arduino setup function.
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Initialize X4 module
Totem.X4.beginNoBluetooth(); // Uploads faster
// Register function to receive events when module is connected
Totem.X4.attachOnModuleConnected(onModuleConnect);
// Register module data receiver function "onModuleData"
sensor.attachOnData(onModuleData);
pot.attachOnData(onModuleData);
// Subscribe to distance command
sensor.subscribe("distance");
// Subscribe to potentiometer knob change
pot.subscribe("knobA");
pot.subscribe("knobB");
pot.subscribe("knobC");
}
// Arduino loop function
void loop() {
// Print data
Serial.printf("Distance: %4d, KnobA: %3d, KnobB: %3d, KnobC: %3d\n",
distance, knob[0], knob[1], knob[2]);
delay(300);
}
Yes, it works like magic
This is a very useful example for me, because now I have a better understanding of how it all works. My misunderstanding was not in the initialization, but in how to properly retrieve data from the topics.
And thank you for the very quick response!
There are 2 ways how to retrieve data:
So, you can also write:
// Arduino loop function
void loop() {
// Get latest data
distance = sensor.readWait("distance").getInt();
knob[0] = pot.readWait("knobA").getInt();
knob[1] = pot.readWait("knobA").getInt();
knob[2] = pot.readWait("knobA").getInt();
// Print data
Serial.printf("Distance: %4d, KnobA: %3d, KnobB: %3d, KnobC: %3d\n",
distance, knob[0], knob[1], knob[2]);
delay(300);
}
Thanks for additions. I prefer “event based” way and in future I must read docs much more carefully