Advertisement
Guest User

Master

a guest
Jun 28th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /*-----( Import needed libraries )-----*/
  2. #include <SoftwareSerial.h>
  3. /*-----( Declare Constants and Pin Numbers )-----*/
  4. #define SSerialRX 10 //Serial Receive pin
  5. #define SSerialTX 11 //Serial Transmit pin
  6.  
  7. #define SSerialTxControl 3 //RS485 Direction control
  8.  
  9. #define RS485Transmit HIGH
  10. #define RS485Receive LOW
  11.  
  12. #define Pin13LED 13
  13.  
  14. /*-----( Declare objects )-----*/
  15. SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
  16.  
  17. /*-----( Declare Variables )-----*/
  18. int byteReceived;
  19. int byteSend;
  20.  
  21. void setup() /****** SETUP: RUNS ONCE ******/
  22. {
  23. // Start the built-in serial port, probably to Serial Monitor
  24. Serial.begin(9600);
  25. Serial.println("YourDuino.com SoftwareSerial remote loop example");
  26. Serial.println("Use Serial Monitor, type in upper window, ENTER");
  27. pinMode(Pin13LED, OUTPUT);
  28. pinMode(SSerialTxControl, OUTPUT);
  29.  
  30. digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
  31.  
  32. // Start the software serial port, to another device
  33. RS485Serial.begin(4800); // set the data rate
  34.  
  35. }//--(end setup )---
  36.  
  37.  
  38. void loop() /****** LOOP: RUNS CONSTANTLY ******/
  39. {
  40. digitalWrite(Pin13LED, HIGH); // Show activity
  41. if (Serial.available())
  42. {
  43. byteReceived = Serial.read();
  44.  
  45. digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
  46. RS485Serial.write(byteReceived); // Send byte to Remote Arduino
  47.  
  48. digitalWrite(Pin13LED, LOW); // Show activity
  49. delay(10);
  50. digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
  51. }
  52.  
  53. if (RS485Serial.available()) //Look for data from other Arduino
  54. {
  55. digitalWrite(Pin13LED, HIGH); // Show activity
  56. byteReceived = RS485Serial.read(); // Read received byte
  57. Serial.write(byteReceived); // Show on Serial Monitor
  58. delay(10);
  59. digitalWrite(Pin13LED, LOW); // Show activity
  60.  
  61. }
  62.  
  63. }//--(end main loop )---
  64.  
  65. /*-----( Declare User-written Functions )-----*/
  66.  
  67. //NONE
  68. //*********( THE END )***********
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement