Advertisement
Guest User

Untitled

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