Dear all,
My software almost works like intended and I have only one problem.
When communicating with the Arduino Slave it works fine, but when it is part of a group it will only respond if it's the first. If placed as second or later the slave will not respond. I think it's because of the responds time, how do I change the time frame of responding to a request?
See code used below:
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
const int ledPin = LED_BUILTIN;
const int digitalInputPin3 = 3;
const int digitalOutputPin4 = 4;
const int analogOutputPin5 = 5;
const int digitalInputPin6 = 6;
const int digitalInputPin7 = 7;
const int digitalInputPin8 = 8;
const int digitalInputPin9 = 9;
const int digitalInputPin10 = 10;
const int digitalInputPin11 = 11;
const int digitalInputPin12 = 12;
const int digitalInputPin13 = 13;
int slaveAddress = 0;
int digitalInputPin3State = 0;
int dipSwitch1 = 0;
int dipSwitch2 = 0;
int dipSwitch3 = 0;
int dipSwitch4 = 0;
int dipSwitch5 = 0;
int dipSwitch6 = 0;
int dipSwitch7 = 0;
int dipSwitch8 = 0;
int input = 0;
int counter = 0;
int rpm = 0;
int output = 0;
int inputWasHigh = 0;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 5000;
void setup() {
startMillis = millis();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(digitalInputPin3, INPUT_PULLUP);
pinMode(digitalOutputPin4, OUTPUT);
pinMode(analogOutputPin5, OUTPUT);
pinMode(digitalInputPin6, INPUT_PULLUP);
pinMode(digitalInputPin7, INPUT_PULLUP);
pinMode(digitalInputPin8, INPUT_PULLUP);
pinMode(digitalInputPin9, INPUT_PULLUP);
pinMode(digitalInputPin10, INPUT_PULLUP);
pinMode(digitalInputPin11, INPUT_PULLUP);
pinMode(digitalInputPin12, INPUT_PULLUP);
pinMode(digitalInputPin13, INPUT_PULLUP);
counter = 0;
output = 0;
rpm = 0;
if (digitalRead(digitalInputPin6) == 0) {
dipSwitch1 = 128;
} else {
dipSwitch1 = 0;
}
if (digitalRead(digitalInputPin7) == 0) {
dipSwitch2 = 64;
} else {
dipSwitch2 = 0;
}
if (digitalRead(digitalInputPin8) == 0) {
dipSwitch3 = 32;
} else {
dipSwitch3 = 0;
}
if (digitalRead(digitalInputPin9) == 0) {
dipSwitch4 = 16;
} else {
dipSwitch4 = 0;
}
if (digitalRead(digitalInputPin10) == 0) {
dipSwitch5 = 8;
} else {
dipSwitch5 = 0;
}
if (digitalRead(digitalInputPin11) == 0) {
dipSwitch6 = 4;
} else {
dipSwitch6 = 0;
}
if (digitalRead(digitalInputPin12) == 0) {
dipSwitch7 = 2;
} else {
dipSwitch7 = 0;
}
if (digitalRead(digitalInputPin13) == 0) {
dipSwitch8 = 1;
} else {
dipSwitch8 = 0;
}
slaveAddress = dipSwitch1 + dipSwitch2 + dipSwitch3 + dipSwitch4 + dipSwitch5 + dipSwitch6 + dipSwitch7 + dipSwitch8;
Serial.begin(9600);
Serial.println("Modbus RTU Server LED");
if (!ModbusRTUServer.begin(slaveAddress, 9600, SERIAL_8E1)) {
Serial.println("Failed to start Modbus RTU Server!");
while (1);
}
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
ModbusRTUServer.configureHoldingRegisters(0x00, 2);
}
void loop() {
ModbusRTUServer.poll();
int registerValue1 = ModbusRTUServer.holdingRegisterRead(0x00);
output = map(registerValue1, 0, 100, 255, 0);
analogWrite(analogOutputPin5, output);
if (registerValue1) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
digitalInputPin3State = digitalRead(digitalInputPin3);
if ( digitalInputPin3State == 1 && inputWasHigh != 1) {
counter++;
inputWasHigh = 1;
} else if ( digitalInputPin3State == 1 && inputWasHigh == 1) {
inputWasHigh = 1;
} else {
inputWasHigh = 0;
}
currentMillis = millis();
if (currentMillis - startMillis >= period)
{
rpm = counter*12;
ModbusRTUServer.holdingRegisterWrite(1, rpm);
counter = 0;
startMillis = currentMillis;
}
ModbusRTUServer.holdingRegisterWrite(1, rpm);
}