#include <ModbusRTU.h>
#define SLAVE_ID 1
ModbusRTU mb;
int a1 = 0;
int b = 0;
int d = 0;
int f = 0;
void setup() {
Serial.begin(9600, SERIAL_8N1);
mb.begin(&Serial);
mb.setBaudrate(9600);
mb.slave(SLAVE_ID);
mb.addCoil(0);
mb.addIsts(0);
mb.addHreg(0);
mb.addIreg(0);
mb.addIreg(1);
mb.addIreg(2);
pinMode(2, OUTPUT);
pinMode(3, INPUT_PULLUP);
pinMode(A0, INPUT_PULLUP);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
}
void loop() {
mb.task();
mb.Ists(0, !digitalRead(3));
digitalWrite(2, mb.Coil(0));
switch (mb.Hreg(0)) {
case 0:
analogWrite(11, 0);
analogWrite(10, 0);
analogWrite(9, 0);
break;
case 1:
analogWrite(11, 165);
analogWrite(10, 42);
analogWrite(9, 42);
break;
case 2:
analogWrite(11, 255);
analogWrite(10, 0);
analogWrite(9, 0);
break;
case 3:
analogWrite(11, 255);
analogWrite(10, 165);
analogWrite(9, 0);
break;
case 4:
analogWrite(11, 255);
analogWrite(10, 255);
analogWrite(9, 0);
break;
case 5:
analogWrite(11, 0);
analogWrite(10, 128);
analogWrite(9, 0);
break;
case 6:
analogWrite(11, 0);
analogWrite(10, 0);
analogWrite(9, 255);
break;
case 7:
analogWrite(11, 160);
analogWrite(10, 32);
analogWrite(9, 240);
break;
case 8:
analogWrite(11, 128);
analogWrite(10, 128);
analogWrite(9, 128);
break;
case 9:
analogWrite(11, 255);
analogWrite(10, 255);
analogWrite(9, 255);
break;
}
int a = digitalRead(A0);
if (a != a1 && a) b++;
a1 = a;
if (b > 9) b = 0;
int c = analogRead(A1);
switch (c) {
case 0:
d = 2;
break;
case 512:
d = 0;
break;
case 1023:
d = 1;
break;
}
int e = analogRead(A2);
switch (e) {
case 0:
f = 2;
break;
case 512:
f = 0;
break;
case 1023:
f = 1;
break;
}
mb.Ireg(0, b);
mb.Ireg(1, d);
mb.Ireg(2, f);
delay(100);
}