Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
- #include <ArduinoModbus.h>
- #include <Servo.h>
- const int ledPin = LED_BUILTIN;
- Servo myservo;
- void setup()
- {
- Serial.begin(9600);
- myservo.attach(4);
- // start the Modbus RTU server, with (slave) id 1
- if (!ModbusRTUServer.begin(1, 9600)) {
- Serial.println("Failed to start Modbus RTU Server!");
- while (1);
- }
- // configure the LED
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, HIGH);
- // configure a single coil at address 0x00
- // ModbusRTUServer.configureCoils(0x02, 1);
- ModbusRTUServer.configureCoils(0x04, 1);
- }
- void loop()
- {
- // poll for Modbus RTU requests
- ModbusRTUServer.poll();
- /* // read the current value of the coil
- int coilValue = ModbusRTUServer.coilRead(0x02);
- if (coilValue) {
- // coil value set, turn LED on
- digitalWrite(ledPin, HIGH);
- digitalWrite(2, HIGH);
- } else {
- // coild value clear, turn LED off
- digitalWrite(ledPin, LOW);
- digitalWrite(2, LOW);
- }
- */
- int coilValueServo = ModbusRTUServer.coilRead(0x04);
- if(coilValueServo==0)
- {
- myservo.write(0);
- }
- if (coilValueServo==1)
- {
- myservo.write(180);
- }
- if (coilValueServo==2)
- {
- myservo.write(90);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement