change modbus slave address on the go [SOLVED]

I am using this example of a arduino modbus slave, it works fine.
I want to be able to change the slave id from my program (the user will do it from a display and buttons), but how can I program this change since the object slave is declared outside setup() and loop()?
If I put the line "Modbus slave(2,0,TXEN);" inside setup(), it has "setup" as scope, and loop() will not see it.

/**
 *  Modbus slave example 3:
 *  The purpose of this example is to link a data array
 *  from the Arduino to an external device through RS485.
 *
 *  Recommended Modbus Master: QModbus
 *  http://qmodbus.sourceforge.net/
 */

#include <ModbusRtu.h>

// assign the Arduino pin that must be connected to RE-DE RS485 transceiver
#define TXEN 7

// data array for modbus network sharing
uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1 };

/**
 *  Modbus object declaration
 *  u8id : node id = 0 for master, = 1..247 for slave
 *  u8serno : serial port (use 0 for Serial)
 *  u8txenpin : 0 for RS-232 and USB-FTDI 
 *               or any pin number > 1 for RS-485
 */



  Modbus slave(2,0,TXEN); // this is slave @2 and RS-485


void setup() {
  slave.begin( 19200 ); // baud-rate at 19200
}

void loop() {
  slave.poll( au16data, 16 );
}
// Global
Modbus *slave = NULL;

// In setup():
   slave = new Modbus(2,0,TXEN);

// In loop():
if(slave)
   slave->poll(au16data, 16);

want to be able to change the slave id from my program (the user will do it from a display and buttons), but how can I program this change since the object slave is declared outside setup() and loop()?

ModbusRtu.h has a setID() method to change the slave ID.

thanks, it worked with .setID();

:slight_smile: