Communicating with a drive using modbusTCP

I would like to communicate with a pair of drives to control post lift.
I need to send each drive a control word to tell it to rotate left or right and to enable it etc.

I have wrote a sketch based on the arduino modbus library.

When I press a button to go up or down I need to send the appropriate control word to perform that action.

However when I try this with my program, it doesn't work. The program seems to stop until the button is released.

#include <SPI.h>
#include <Ethernet.h>

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

#define LHTopLimit 4
#define LHObsLimit 5
#define LHBotLimit 6
#define RHTopLimit 7
#define RHObsLimit 8
#define RHBotLimit 9
#define UPButton 10
#define DWNButton 11

int period = 50;
unsigned long time_now = 0;

int LH;
int RH;

int val=0;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:

char dataChar;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 1);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress servera(192, 168, 1, 11); // update with the IP Address of your Modbus server
IPAddress serverb(192, 168, 1, 12);
int Speed =0;

void setup() {

time_now = millis();
   
//Initialize serial and wait for port to open:
Serial.begin(9600);


#define LHTopLimit 4
#define LHObsLimit 5
#define LHBotLimit 6
#define RHTopLimit 7
#define RHObsLimit 8
#define RHBotLimit 9
#define UPButton 10
#define DWNButton 11


pinMode(LHTopLimit,INPUT);
pinMode(LHObsLimit,INPUT);
pinMode(LHBotLimit,INPUT);
pinMode(RHTopLimit,INPUT);
pinMode(RHObsLimit,INPUT);
pinMode(RHBotLimit,INPUT);
pinMode(UPButton,INPUT);
pinMode(DWNButton,INPUT);

LH = 0x00;
RH = 0x00;

  while (!Serial) {
    ;
    // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  if (!modbusTCPClient.connected()) {
    
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected");
    }

    }
    // client connected

        if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||   // 05.006 - Motor rated frequency = 50Hz
        (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||    // 05.007 - Motor rated current = 3.4A
        (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
        (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||  // 05.009 - Motor rated voltage = 400V
        (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||   // 05.010 - Motor rated power factor = 0.83
        (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||    // 05.011 - Motor poles = 4
        (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))     // 06.043 - Control word enable = 1
    {
      Serial.print("Failed to write reg!");
      Serial.println(modbusTCPClient.lastError());
    }  

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");


 if (!modbusTCPClient.connected()) {
    
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) 
    {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }

    }
   
    // client connected

        if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||   // 05.006 - Motor rated frequency = 50Hz
        (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||    // 05.007 - Motor rated current = 3.4A
        (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
        (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||  // 05.009 - Motor rated voltage = 400V
        (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||   // 05.010 - Motor rated power factor = 0.83
        (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||    // 05.011 - Motor poles = 4
        (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))     // 06.043 - Control word enable = 1
    {
      Serial.print("Failed to write int reg!");
      Serial.println(modbusTCPClient.lastError());
    }
  
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");

}

void loop() {

if (!modbusTCPClient.connected()) {
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP server A");

    if (!modbusTCPClient.begin(servera, 502)) 
    {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected!");
    }
} else {
  Serial.println("Modbus TCP Server A already connected!");
}

    // write the value of 0x183, to the register at address 0x281 to tell the drive to go up
       if (!modbusTCPClient.holdingRegisterWrite(0,641,LH)) 
       {
       Serial.print("Failed to write reg 1! ");
       Serial.println(modbusTCPClient.lastError());
          } else {
            Serial.println("Reg 1 write sucessfull!");
          }
Serial.println(LH);        
modbusTCPClient.end();
Serial.println("Modbus TCP Server A disconnected");

while(millis() < time_now + period){
        //wait approx. [period] ms
}
  if (!modbusTCPClient.connected()) {
    
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502))
    {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
} else { 
  Serial.println("Modbus TCP Server B already connected");
}
   
      if (!modbusTCPClient.holdingRegisterWrite(0,641,RH)) 
            {
            Serial.print("Failed to write reg 2! ");
            Serial.println(modbusTCPClient.lastError());
            } else {
            Serial.println("Reg 2 write sucessfull!");
            }
    modbusTCPClient.end();
    Serial.println("Modbus TCP Server B disconnected");

while(millis() < time_now + period){
        //wait approx. [period] ms
    }

 
val=digitalRead(UPButton);

if (val==1) {
  LH=0x0183;
  } else {
    LH=0x00;
}
Serial.println(val);

   
}

I would be grateful if anyone could provide some tips please?

You will get a better response if you used the IDE Tools/Auto Format and then post the code again.
Next is copy the Serial output and place in code tags as well.
Maybe the Serial output will tell us where it is getting 'stuck' since you have not.

1 Like
#include <SPI.h>
#include <Ethernet.h>

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

#define LHTopLimit 4
#define LHObsLimit 5
#define LHBotLimit 6
#define RHTopLimit 7
#define RHObsLimit 8
#define RHBotLimit 9
#define UPButton 10
#define DWNButton 11

int period = 50;
unsigned long time_now = 0;

int LH;
int RH;

int val = 0;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:

char dataChar;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 1);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress servera(192, 168, 1, 11);  // update with the IP Address of your Modbus server
IPAddress serverb(192, 168, 1, 12);
int Speed = 0;

void setup() {

  time_now = millis();

  //Initialize serial and wait for port to open:
  Serial.begin(9600);


#define LHTopLimit 4
#define LHObsLimit 5
#define LHBotLimit 6
#define RHTopLimit 7
#define RHObsLimit 8
#define RHBotLimit 9
#define UPButton 10
#define DWNButton 11


  pinMode(LHTopLimit, INPUT);
  pinMode(LHObsLimit, INPUT);
  pinMode(LHBotLimit, INPUT);
  pinMode(RHTopLimit, INPUT);
  pinMode(RHObsLimit, INPUT);
  pinMode(RHBotLimit, INPUT);
  pinMode(UPButton, INPUT);
  pinMode(DWNButton, INPUT);

  LH = 0x00;
  RH = 0x00;

  while (!Serial) {
    ;
    // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected");
    }
  }
  // client connected

  if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||  // 05.006 - Motor rated frequency = 50Hz
      (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||   // 05.007 - Motor rated current = 3.4A
      (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
      (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||   // 05.009 - Motor rated voltage = 400V
      (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||    // 05.010 - Motor rated power factor = 0.83
      (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||     // 05.011 - Motor poles = 4
      (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))       // 06.043 - Control word enable = 1
  {
    Serial.print("Failed to write reg!");
    Serial.println(modbusTCPClient.lastError());
  }

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");


  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
  }

  // client connected

  if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||  // 05.006 - Motor rated frequency = 50Hz
      (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||   // 05.007 - Motor rated current = 3.4A
      (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
      (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||   // 05.009 - Motor rated voltage = 400V
      (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||    // 05.010 - Motor rated power factor = 0.83
      (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||     // 05.011 - Motor poles = 4
      (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))       // 06.043 - Control word enable = 1
  {
    Serial.print("Failed to write int reg!");
    Serial.println(modbusTCPClient.lastError());
  }

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");
}

void loop() {

  if (!modbusTCPClient.connected()) {
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP server A");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected!");
    }
  } else {
    Serial.println("Modbus TCP Server A already connected!");
  }

  // write the value of 0x183, to the register at address 0x281 to tell the drive to go up
  if (!modbusTCPClient.holdingRegisterWrite(0, 641, LH)) {
    Serial.print("Failed to write reg 1! ");
    Serial.println(modbusTCPClient.lastError());
  } else {
    Serial.println("Reg 1 write sucessfull!");
  }
  Serial.println(LH);
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");

  while (millis() < time_now + period) {
    //wait approx. [period] ms
  }
  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
  } else {
    Serial.println("Modbus TCP Server B already connected");
  }

  if (!modbusTCPClient.holdingRegisterWrite(0, 641, RH)) {
    Serial.print("Failed to write reg 2! ");
    Serial.println(modbusTCPClient.lastError());
  } else {
    Serial.println("Reg 2 write sucessfull!");
  }
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");

  while (millis() < time_now + period) {
    //wait approx. [period] ms
  }


  val = digitalRead(UPButton);

  if (val == 1) {
    LH = 0x0183;
  } else {
    LH = 0x00;
  }
  Serial.println(val);
}

Attempting to connect to Modbus TCP serverB

Modbus TCP Server B failed to connect!

Failed to write reg 2! Invalid argument

Modbus TCP Server B disconnected

1

Attempting to connect to Modbus TCP server A

Modbus TCP Server A now connected!

Reg 1 write sucessfull!

387

Modbus TCP Server A disconnected

Attempting to connect to Modbus TCP serverB

Attempting to connect to Modbus TCP server A
Modbus TCP Server A now connected!
Reg 1 write sucessfull!
387
Modbus TCP Server A disconnected
Attempting to connect to Modbus TCP serverB
Modbus TCP Server B failed to connect!
Failed to write reg 2! Invalid argument
Modbus TCP Server B disconnected
1
Attempting to connect to Modbus TCP server A
Modbus TCP Server A now connected!
Reg 1 write sucessfull!
387
Modbus TCP Server A disconnected
Attempting to connect to Modbus TCP serverB
Modbus TCP Server B failed to connect!
Failed to write reg 2! Invalid argument
Modbus TCP Server B disconnected
1
Attempting to connect to Modbus TCP server A
Modbus TCP Server A now connected!
Reg 1 write sucessfull!
387
Modbus TCP Server A disconnected
Modbus TCP Server A disconnected
Attempting to connect to Modbus TCP serverB
Modbus TCP Server B failed to connect!
Failed to write reg 2! Invalid argument
Modbus TCP Server B disconnected
0
Attempting to connect to Modbus TCP server A
Modbus TCP Server A failed to connect!
Failed to write reg 1! Invalid argument
0
Modbus TCP Server A disconnected
Attempting to connect to Modbus TCP serverB
Modbus TCP Server B failed to connect!
Failed to write reg 2! Invalid argument
Modbus TCP Server B disconnected
0
Attempting to connect to Modbus TCP server A
Modbus TCP Server A failed to connect!
Failed to write reg 1! Invalid argument
0
Modbus TCP Server A disconnected
Attempting to connect to Modbus TCP serverB
Modbus TCP Server B failed to connect!
Failed to write reg 2! Invalid argument
Modbus TCP Server B disconnected
0
Attempting to connect to Modbus TCP server A
Modbus TCP Server A failed to connect!
Failed to write reg 1! Invalid argument
0


Please be aware that I only have a drive connected to server A for testing.

You need to remove the network code.
Practice reading a button.
https://docs.arduino.cc/built-in-examples/digital/Debounce/

Are you really going to hook a lift to an intermittent network?

The error happens when the UPButton is pressed

Without your wiring diagram, I would guess that you are creating a dead short from power to ground.

I know I need to debouce the button, I will correct that once the rest of the code is working.

What do you mean remove the network code?

Then comment out all the server B code.

Since you have a lot of messages that are similar, assign a unique number to each one. That is how real systems are built.

Also, you are using a variable before you determine what to store in it. That is your bug I think.

Is a 'lift' what I call an elevator? As a former 'lift' designer I never saw any code like this.

Modbus operates on a network.
TCP operates on a network.
Servers operate on a network.
Your errors are full of network issues.

Then why did you mention "when I press a button"?

You're building the Titanic and need to ask how to use a paintbrush.

Start with fixing the "I know" button.

1 Like

Do you have any examples of similar projects which would demonstrate how to do this more effectively please?

Post #7
https://docs.arduino.cc/built-in-examples/digital/Debounce/

I have debounced the button properly now and moved the variable to the beginning of the program so that its loaded with a value before its sent out by the modbus function. I have also commented out server b. Its still does the same thing in that it stops transmitting and comes up with the following in the serial monitor:


Attempting to connect to Modbus TCP server A

Modbus TCP Server A now connected!

Reg 1 write sucessfull!

0

Modbus TCP Server A disconnected
0

"BUTTON PRESSED AT THIS POINT"

Attempting to connect to Modbus TCP server A
Modbus TCP Server A failed to connect!
Failed to write reg 1! Invalid argument
0
Modbus TCP Server A disconnected
0
Modbus TCP Server A already connected!
Failed to write reg 1! Invalid argument

#include <SPI.h>
#include <Ethernet.h>

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

#define LHTopLimit 4
#define LHObsLimit 5
#define LHBotLimit 6
#define RHTopLimit 7
#define RHObsLimit 8
#define RHBotLimit 9
#define UPButton 10
#define DWNButton 11
#define LEDPIN 13

int period = 50;
unsigned long time_now = 0;

int LH;
int RH;

int val = 0;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
// The IP address will be dependent on your local network:

char dataChar;

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 1);

EthernetClient ethClient;
ModbusTCPClient modbusTCPClient(ethClient);

IPAddress servera(192, 168, 1, 11);  // update with the IP Address of your Modbus server
IPAddress serverb(192, 168, 1, 12);
int Speed = 0;

int ledState = HIGH;
int buttonState = 0;
int lastButtonState = 0;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {

  time_now = millis();

  //Initialize serial and wait for port to open:
  Serial.begin(9600);

  pinMode(LHTopLimit, INPUT);
  pinMode(LHObsLimit, INPUT);
  pinMode(LHBotLimit, INPUT);
  pinMode(RHTopLimit, INPUT);
  pinMode(RHObsLimit, INPUT);
  pinMode(RHBotLimit, INPUT);
  pinMode(UPButton, INPUT);
  pinMode(DWNButton, INPUT);
  pinMode(LEDPIN, OUTPUT);

  LH = 0x00;
  RH = 0x00;

  while (!Serial) {
    ;
    // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1);
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected");
    }
  }
  // client connected

  if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||  // 05.006 - Motor rated frequency = 50Hz
      (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||   // 05.007 - Motor rated current = 3.4A
      (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
      (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||   // 05.009 - Motor rated voltage = 400V
      (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||    // 05.010 - Motor rated power factor = 0.83
      (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||     // 05.011 - Motor poles = 4
      (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))       // 06.043 - Control word enable = 1
  {
    Serial.print("Failed to write reg!");
    Serial.println(modbusTCPClient.lastError());
  }

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");


  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
  }

  // client connected

  if ((!modbusTCPClient.holdingRegisterWrite(0, 505, 0x1388)) ||  // 05.006 - Motor rated frequency = 50Hz
      (!modbusTCPClient.holdingRegisterWrite(0, 506, 0x154)) ||   // 05.007 - Motor rated current = 3.4A
      (!modbusTCPClient.holdingRegisterWrite(0, 507, 0x3714)) ||  // 05.008 - Motor rated speed = 1410rpm
      (!modbusTCPClient.holdingRegisterWrite(0, 508, 0x190)) ||   // 05.009 - Motor rated voltage = 400V
      (!modbusTCPClient.holdingRegisterWrite(0, 509, 0x53)) ||    // 05.010 - Motor rated power factor = 0.83
      (!modbusTCPClient.holdingRegisterWrite(0, 510, 0x4)) ||     // 05.011 - Motor poles = 4
      (!modbusTCPClient.holdingRegisterWrite(0, 642, 0x1)))       // 06.043 - Control word enable = 1
  {
    Serial.print("Failed to write int reg!");
    Serial.println(modbusTCPClient.lastError());
  }

  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");
}

void loop() {

  int reading = digitalRead(UPButton);

  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }
  val = ledState;

  if (val == HIGH) {
    LH = 0x00183;
    digitalWrite(LEDPIN, HIGH);
  } else {
    LH = 0;
    digitalWrite(LEDPIN, LOW);
  }

  Serial.println(val);

  lastButtonState = reading;

  if (!modbusTCPClient.connected()) {
    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP server A");

    if (!modbusTCPClient.begin(servera, 502)) {
      Serial.println("Modbus TCP Server A failed to connect!");
    } else {
      Serial.println("Modbus TCP Server A now connected!");
    }
  } else {
    Serial.println("Modbus TCP Server A already connected!");
  }

  // write the value of 0x183, to the register at address 0x281 to tell the drive to go up
  if (!modbusTCPClient.holdingRegisterWrite(0, 641, LH)) {
    Serial.print("Failed to write reg 1! ");
    Serial.println(modbusTCPClient.lastError());
  } else {
    Serial.println("Reg 1 write sucessfull!");
  }
  Serial.println(LH);
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server A disconnected");

  while (millis() < time_now + period) {
    //wait approx. [period] ms
  }
  /*  if (!modbusTCPClient.connected()) {

    // client not connected, start the modbus TCP
    Serial.println("Attempting to connect to Modbus TCP serverB");

    if (!modbusTCPClient.begin(serverb, 502)) {
      Serial.println("Modbus TCP Server B failed to connect!");
    } else {
      Serial.println("Modbus TCP Server B now connected");
    }
  } else {
    Serial.println("Modbus TCP Server B already connected");
  }

  if (!modbusTCPClient.holdingRegisterWrite(0, 641, RH)) {
    Serial.print("Failed to write reg 2! ");
    Serial.println(modbusTCPClient.lastError());
  } else {
    Serial.println("Reg 2 write sucessfull!");
  }
  modbusTCPClient.end();
  Serial.println("Modbus TCP Server B disconnected");

  while (millis() < time_now + period) {
    //wait approx. [period] ms
  }
*/
}