Advertisement
DrAungWinHtut

Slave2.ino

Sep 2nd, 2022
1,598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Software serial multple serial test
  3.  
  4.  Receives from the hardware serial, sends to software serial.
  5.  Receives from software serial, sends to hardware serial.
  6.  
  7.  The circuit:
  8.  * RX is digital pin 10 (connect to TX of other device)
  9.  * TX is digital pin 11 (connect to RX of other device)
  10.  
  11.  Note:
  12.  Not all pins on the Mega and Mega 2560 support change interrupts,
  13.  so only the following can be used for RX:
  14.  10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
  15.  
  16.  Not all pins on the Leonardo and Micro support change interrupts,
  17.  so only the following can be used for RX:
  18.  8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
  19.  
  20.  created back in the mists of time
  21.  modified 25 May 2012
  22.  by Tom Igoe
  23.  based on Mikal Hart's example
  24.  
  25.  This example code is in the public domain.
  26.  
  27.  */
  28. #include <SoftwareSerial.h>
  29.  
  30. SoftwareSerial mySerial(2, 3); // RX, TX
  31.  
  32. void setup() {
  33.   // Open serial communications and wait for port to open:
  34.   Serial.begin(9600);
  35.   while (!Serial) {
  36.     ; // wait for serial port to connect. Needed for native USB port only
  37.   }
  38.  
  39.  
  40.   Serial.println("I am Slave!");
  41.  
  42.   // set the data rate for the SoftwareSerial port
  43.   mySerial.begin(9600);
  44.   mySerial.println("I am Slave!");
  45.   pinMode(13,OUTPUT);
  46. }
  47.  
  48. void loop() { // run over and over
  49.   if (mySerial.available()) {
  50.     char cmd = mySerial.read();
  51.     Serial.write(cmd);
  52.     if(cmd=='o')
  53.     {
  54.       digitalWrite(13,HIGH);
  55.       Serial.println("LED ON");
  56.     }
  57.     if(cmd=='c')
  58.     {
  59.       digitalWrite(13,LOW);
  60.       Serial.println("LED OFF");
  61.     }
  62.   }
  63.   if (Serial.available()) {
  64.     mySerial.write(Serial.read());
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement