Hi Jakob,
I have another 328 lying around somewhere, I will hook it up on a bread board and connect them. Give me a few days...
In the mean time, try to remove a packet. Try just requesting a read function (F3) from the slave and see if you can get the LED to vary when turning the slave pot. Then use just F16 to request a write to the slave and see if the master pot varies the slave LED.
Use the two Read sketches together and the two Write sketches together.
SimpleModbusMasterArduinoRead.ino
#include <SimpleModbusMaster.h>
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 5
#define TxEnablePin 2
#define LED 9
enum
{
PACKET1,
TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
unsigned int readRegs[1];
void setup()
{
modbus_construct(packet1, 1, READ_HOLDING_REGISTERS, 0, 1, readRegs);
modbus_configure(baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
analogWrite(LED, readRegs[0]/4);
}
SimpleModbusSlaveArduinoRead.ino
#include <SimpleModbusSlave.h>
#define LED 9
enum
{
ADC_VAL,
HOLDING_REGS_SIZE
};
unsigned int holdingRegs[HOLDING_REGS_SIZE];
void setup()
{
modbus_configure(9600, SERIAL_8N2, 1, 2, HOLDING_REGS_SIZE, holdingRegs);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
holdingRegs[ADC_VAL] = analogRead(A0);
}
SimpleModbusMasterArduinoWrite.ino
#include <SimpleModbusMaster.h>
#define baud 9600
#define timeout 1000
#define polling 200
#define retry_count 5
#define TxEnablePin 2
#define LED 9
enum
{
PACKET1,
TOTAL_NO_OF_PACKETS
};
Packet packets[TOTAL_NO_OF_PACKETS];
packetPointer packet1 = &packets[PACKET1];
unsigned int writeRegs[1];
void setup()
{
modbus_construct(packet1, 1, PRESET_MULTIPLE_REGISTERS, 0, 1, writeRegs);
modbus_configure(baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
writeRegs[0] = analogRead(A0);
}
SimpleModbusSlaveArduinoWrite.ino
#include <SimpleModbusSlave.h>
#define LED 9
enum
{
PWM_VAL,
HOLDING_REGS_SIZE
};
unsigned int holdingRegs[HOLDING_REGS_SIZE];
void setup()
{
modbus_configure(9600, SERIAL_8N2, 1, 2, HOLDING_REGS_SIZE, holdingRegs);
pinMode(LED, OUTPUT);
}
void loop()
{
modbus_update();
analogWrite(LED, holdingRegs[PWM_VAL]/4);
}