Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. //#include <ArduinoRS485.h> // ArduinoModbus depends on the ArduinoRS485 library
  2. #include <ArduinoModbus.h>
  3. #include <Servo.h>
  4.  
  5. const int ledPin = LED_BUILTIN;
  6.  
  7. Servo myservo;
  8.  
  9. void setup()
  10. {
  11. Serial.begin(9600);
  12.  
  13. myservo.attach(4);
  14. // start the Modbus RTU server, with (slave) id 1
  15. if (!ModbusRTUServer.begin(1, 9600)) {
  16. Serial.println("Failed to start Modbus RTU Server!");
  17. while (1);
  18. }
  19.  
  20. // configure the LED
  21. pinMode(ledPin, OUTPUT);
  22. digitalWrite(ledPin, HIGH);
  23.  
  24. // configure a single coil at address 0x00
  25. // ModbusRTUServer.configureCoils(0x02, 1);
  26. ModbusRTUServer.configureCoils(0x04, 1);
  27.  
  28. }
  29.  
  30. void loop()
  31. {
  32. // poll for Modbus RTU requests
  33. ModbusRTUServer.poll();
  34.  
  35. /* // read the current value of the coil
  36. int coilValue = ModbusRTUServer.coilRead(0x02);
  37.  
  38. if (coilValue) {
  39. // coil value set, turn LED on
  40. digitalWrite(ledPin, HIGH);
  41. digitalWrite(2, HIGH);
  42. } else {
  43. // coild value clear, turn LED off
  44. digitalWrite(ledPin, LOW);
  45. digitalWrite(2, LOW);
  46. }
  47. */
  48. int coilValueServo = ModbusRTUServer.coilRead(0x04);
  49.  
  50. if(coilValueServo==0)
  51. {
  52. myservo.write(0);
  53. }
  54.  
  55. if (coilValueServo==1)
  56. {
  57. myservo.write(180);
  58. }
  59.  
  60. if (coilValueServo==2)
  61. {
  62. myservo.write(90);
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement