How use more TotemBus modules together?


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 :cry:

1 Like

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 ("distance")) distance = data.getInt();
    if ("knobA")) knob[0] = data.getInt();
    if ("knobB")) knob[1] = data.getInt();
    if ("knobC")) knob[2] = data.getInt();
// Arduino setup function.
void setup() {
    // put your setup code here, to run once:
    // Initialize X4 module
    Totem.X4.beginNoBluetooth(); // Uploads faster
    // Register function to receive events when module is connected
    // Register module data receiver function "onModuleData"
    // Subscribe to distance command
    // Subscribe to potentiometer knob change
// 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]);
1 Like

Yes, it works like magic :grin:

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:

  • Non-blocking - does not delay code execution. Event based. Module broadcasts topics at specified interval. Used with attachOnData() and subscribe() or read() functions.
  • Blocking - delays currently executing code until data is received. Convenient to use in single line commands with readWait().

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]);

Thanks for additions. I prefer “event based” way and in future I must read docs much more carefully :wink: