// This should be a basic slave, which will store pressure value from pin A0 for transmitting
// as well as receiving a value for setting a servo (Pin 6) open or close.
#include <SimpleModbusSlave.h>
#include <Servo.h>
Servo tankservo;
const int tankservoPin = 6;
int open = 180;
int close = 90;
int servo1State = 0; // current state of the button
int lastservo1State = 0; // previous state of the button
#define LED 9
enum
{
TANK_PRES,
CHAMB_PRES,
SERVO1,
SERVO2,
SERVO3,
SERVO4,
HOLDING_REGS_SIZE
};
unsigned int holdingRegs[HOLDING_REGS_SIZE];
void setup()
{
modbus_configure(9600, SERIAL_8N2, 1, 2, HOLDING_REGS_SIZE, holdingRegs);
tankservo.attach(tankservoPin);
}
void loop()
{
modbus_update();
holdingRegs[TANK_PRES] = analogRead(A0);
// digitalWrite(servo1State, holdingRegs[SERVO1]); unsure which to use - this is the original
servo1State = digitalRead(holdingRegs[SERVO1]); // unsure which to use - this is interpreted
if (servo1State != lastservo1State) {
// if the state has changed, increment the counter
if (servo1State == HIGH) {
tankservo.write(open);
}
else {
// if the current state is LOW then the button went from on to off:
tankservo.write(close);
}
lastservo1State = servo1State;
}
}