Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. /* HC12 Send/Receive Example Program 2
  2. By Mark J. Hughes
  3. for AllAboutCircuits.com
  4.  
  5. This code will automatically detect commands as sentences that begin
  6. with AT and both write them and broadcast them to remote receivers
  7. to be written. Changing settings on a local transceiver will
  8. change settings on a remote receiver.
  9.  
  10. Connect HC12 "RXD" pin to Arduino Digital Pin 4
  11. Connect HC12 "TXD" pin to Arduino Digital Pin 5
  12. Connect HC12 "Set" pin to Arduino Digital Pin 6
  13.  
  14. Do not power HC12 via Arduino over USB. Per the data sheet,
  15. power the HC12 with a supply of at least 100 mA with
  16. a 22 uF to 1000 uF reservoir capacitor and connect a 1N4007 diode in
  17. series with the positive supply line if the potential difference exceeds 4.5 V.
  18.  
  19. Upload code to two Arduinos connected to two computers
  20. that are separated by at least several meters.
  21.  
  22. */
  23.  
  24. //#include <SoftwareSerial.h>
  25. //
  26. //const byte HC12RxdPin = 15; // "RXD" Pin on HC12
  27. //const byte HC12TxdPin = 14; // "TXD" Pin on HC12
  28. //const byte HC12SetPin = 18; // "SET" Pin on HC12
  29. //
  30. //unsigned long timer = millis(); // Delay Timer
  31. //
  32. //char SerialByteIn; // Temporary variable
  33. //char HC12ByteIn; // Temporary variable
  34. //String HC12ReadBuffer = ""; // Read/Write Buffer 1 for HC12
  35. //String SerialReadBuffer = ""; // Read/Write Buffer 2 for Serial
  36. //boolean SerialEnd = false; // Flag to indicate End of Serial String
  37. //boolean HC12End = false; // Flag to indiacte End of HC12 String
  38. //boolean commandMode = false; // Send AT commands
  39. //
  40. //// Software Serial ports Rx and Tx are opposite the HC12 Rx and Tx
  41. //// Create Software Serial Port for HC12
  42. //SoftwareSerial HC12(HC12TxdPin, HC12RxdPin);
  43. //
  44. //void setup() {
  45. //
  46. // HC12ReadBuffer.reserve(64); // Reserve 64 bytes for Serial message input
  47. // SerialReadBuffer.reserve(64); // Reserve 64 bytes for HC12 message input
  48. //
  49. // pinMode(HC12SetPin, OUTPUT); // Output High for Transparent / Low for Command
  50. // digitalWrite(HC12SetPin, HIGH); // Enter Transparent mode
  51. // delay(80); // 80 ms delay before operation per datasheet
  52. // Serial.begin(9600); // Open serial port to computer
  53. // HC12.begin(9600); // Open software serial port to HC12
  54. //}
  55. //
  56. //void loop() {
  57. //
  58. //
  59. // // while (HC12.available()) { // If Arduino's HC12 rx buffer has data
  60. // // Serial.println(" mensagem recebida: ");
  61. // // Serial.write(HC12.read()); // Send the data to the computer
  62. // // }
  63. // // while (Serial.available()) { // If Arduino's computer rx buffer has data
  64. // // HC12.print(Serial.read()); // Send that data to serial
  65. // // }
  66. //
  67. //
  68. // while (HC12.available()) { // While Arduino's HC12 soft serial rx buffer has data
  69. // HC12ByteIn = HC12.read(); // Store each character from rx buffer in byteIn
  70. // HC12ReadBuffer += char(HC12ByteIn); // Write each character of byteIn to HC12ReadBuffer
  71. // if (HC12ByteIn == '\n') { // At the end of the line
  72. // HC12End = true; // Set HC12End flag to true
  73. // }
  74. // }
  75. //
  76. // while (Serial.available()) { // If Arduino's computer rx buffer has data
  77. // SerialByteIn = Serial.read(); // Store each character in byteIn
  78. // SerialReadBuffer += char(SerialByteIn); // Write each character of byteIn to SerialReadBuffer
  79. // if (SerialByteIn == '\n') { // Check to see if at the end of the line
  80. // SerialEnd = true; // Set SerialEnd flag to indicate end of line
  81. // }
  82. // }
  83. //
  84. // if (SerialEnd) { // Check to see if SerialEnd flag is true
  85. //
  86. // if (SerialReadBuffer.startsWith("AT")) { // Has a command been sent from local computer
  87. // HC12.print(SerialReadBuffer); // Send local command to remote HC12 before changing settings
  88. // delay(100); //
  89. // digitalWrite(HC12SetPin, LOW); // Enter command mode
  90. // delay(100); // Allow chip time to enter command mode
  91. // Serial.print(SerialReadBuffer); // Echo command to serial
  92. // HC12.print(SerialReadBuffer); // Send command to local HC12
  93. // delay(500); // Wait 0.5s for a response
  94. // digitalWrite(HC12SetPin, HIGH); // Exit command / enter transparent mode
  95. // delay(100); // Delay before proceeding
  96. // } else {
  97. // HC12.print(SerialReadBuffer); // Transmit non-command message
  98. // }
  99. // SerialReadBuffer = ""; // Clear SerialReadBuffer
  100. // SerialEnd = false; // Reset serial end of line flag
  101. // }
  102. //
  103. // if (HC12End) { // If HC12End flag is true
  104. // if (HC12ReadBuffer.startsWith("AT")) { // Check to see if a command is received from remote
  105. // digitalWrite(HC12SetPin, LOW); // Enter command mode
  106. // delay(100); // Delay before sending command
  107. // Serial.print(SerialReadBuffer); // Echo command to serial.
  108. // HC12.print(HC12ReadBuffer); // Write command to local HC12
  109. // delay(500); // Wait 0.5 s for reply
  110. // digitalWrite(HC12SetPin, HIGH); // Exit command / enter transparent mode
  111. // delay(100); // Delay before proceeding
  112. // HC12.println("Remote Command Executed"); // Acknowledge execution
  113. // } else {
  114. // Serial.print(HC12ReadBuffer); // Send message to screen
  115. // }
  116. // HC12ReadBuffer = ""; // Empty buffer
  117. // HC12End = false; // Reset flag
  118. // }
  119. //}
  120.  
  121.  
  122. #include <SoftwareSerial.h>
  123.  
  124. const byte HC12TxdPin = 14; // Transmit Pin on HC12 TX Pin
  125. const byte HC12RxdPin = 15; // Recieve Pin on HC12 RX Pin
  126. const byte HC12SetPin = 18; // "SET" Pin on HC12
  127.  
  128. //SoftwareSerial HC12(HC12TxdPin, HC12RxdPin); // Create Software Serial Port
  129.  
  130. const int ledCount = 10; // the number of LEDs in the bar graph
  131.  
  132. int ledPins[] = {
  133. 2, 3, 4, 5, 6, 7, 8, 9, 10, 16
  134. }; // an array of pin numbers to which LEDs are attached
  135.  
  136. //Armazena os dados que chegam pela serial
  137. String inputString = "";
  138. //Variavel de string completa
  139. boolean stringComplete = false;
  140.  
  141. //Dados do contador
  142. int inChar = 0;
  143. int ledLevel = 0;
  144.  
  145. void setup() {
  146. pinMode(HC12SetPin, OUTPUT);
  147. digitalWrite(HC12SetPin, HIGH);
  148. Serial.begin(9600);
  149. Serial1.begin(9600);
  150. // Serial1.listen();
  151. // loop over the pin array and set them all to output:
  152. for (int thisLed = 0; thisLed < ledCount; thisLed++) {
  153. pinMode(ledPins[thisLed], OUTPUT);
  154. }
  155.  
  156. digitalWrite(HC12SetPin, LOW); // Set HC-12 into AT Command mode
  157. delay(100); // Wait for the HC-12 to enter AT Command mode
  158. Serial1.print("AT+C003"); // Send AT Command to HC-12
  159. delay(200);
  160. digitalWrite(HC12SetPin, HIGH);
  161. delay(200);
  162. while (Serial1.available()) { // If HC-12 has data (the AT Command response)
  163. Serial.print(Serial1.read()); // Send the data to Serial monitor
  164. }
  165. }
  166.  
  167. void loop() {
  168. // read the potentiometer:
  169. // int sensorReading = analogRead(analogPin);
  170. // map the result to a range from 0 to the number of LEDs:
  171.  
  172.  
  173. //Recebe os dados pelo HC-12 via porta serial
  174. // digitalWrite(3, HIGH);
  175. // Serial1.print(9);
  176. // Serial1.write(9);
  177.  
  178. while (Serial1.available()) {
  179. // digitalWrite(5, HIGH);
  180. inChar = Serial1.read();
  181. ledLevel = inChar;
  182.  
  183. if (ledLevel != 0) {
  184. Serial.println(inChar);
  185.  
  186. for (int thisLed = 0; thisLed < ledCount; thisLed++) {
  187. // if the array element's index is less than ledLevel,
  188. // turn the pin for this element on:
  189. if (thisLed < ledLevel) {
  190. digitalWrite(ledPins[thisLed], HIGH);
  191. }
  192. // turn off all pins higher than the ledLevel:
  193. else {
  194. digitalWrite(ledPins[thisLed], LOW);
  195. }
  196. }
  197. }
  198. }
  199. //
  200. //
  201. // // loop over the LED array:
  202. // // for (int thisLed = 0; thisLed < ledCount; thisLed++) {
  203. // // // if the array element's index is less than ledLevel,
  204. // // // turn the pin for this element on:
  205. // // if (thisLed < ledLevel) {
  206. // // digitalWrite(ledPins[thisLed], HIGH);
  207. // // }
  208. // // // turn off all pins higher than the ledLevel:
  209. // // else {
  210. // // digitalWrite(ledPins[thisLed], LOW);
  211. // // }
  212. // // }
  213. delay(2000);
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement