Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* HC12 Send/Receive Example Program 2
- By Mark J. Hughes
- for AllAboutCircuits.com
- This code will automatically detect commands as sentences that begin
- with AT and both write them and broadcast them to remote receivers
- to be written. Changing settings on a local transceiver will
- change settings on a remote receiver.
- Connect HC12 "RXD" pin to Arduino Digital Pin 4
- Connect HC12 "TXD" pin to Arduino Digital Pin 5
- Connect HC12 "Set" pin to Arduino Digital Pin 6
- Do not power HC12 via Arduino over USB. Per the data sheet,
- power the HC12 with a supply of at least 100 mA with
- a 22 uF to 1000 uF reservoir capacitor and connect a 1N4007 diode in
- series with the positive supply line if the potential difference exceeds 4.5 V.
- Upload code to two Arduinos connected to two computers
- that are separated by at least several meters.
- */
- //#include <SoftwareSerial.h>
- //
- //const byte HC12RxdPin = 15; // "RXD" Pin on HC12
- //const byte HC12TxdPin = 14; // "TXD" Pin on HC12
- //const byte HC12SetPin = 18; // "SET" Pin on HC12
- //
- //unsigned long timer = millis(); // Delay Timer
- //
- //char SerialByteIn; // Temporary variable
- //char HC12ByteIn; // Temporary variable
- //String HC12ReadBuffer = ""; // Read/Write Buffer 1 for HC12
- //String SerialReadBuffer = ""; // Read/Write Buffer 2 for Serial
- //boolean SerialEnd = false; // Flag to indicate End of Serial String
- //boolean HC12End = false; // Flag to indiacte End of HC12 String
- //boolean commandMode = false; // Send AT commands
- //
- //// Software Serial ports Rx and Tx are opposite the HC12 Rx and Tx
- //// Create Software Serial Port for HC12
- //SoftwareSerial HC12(HC12TxdPin, HC12RxdPin);
- //
- //void setup() {
- //
- // HC12ReadBuffer.reserve(64); // Reserve 64 bytes for Serial message input
- // SerialReadBuffer.reserve(64); // Reserve 64 bytes for HC12 message input
- //
- // pinMode(HC12SetPin, OUTPUT); // Output High for Transparent / Low for Command
- // digitalWrite(HC12SetPin, HIGH); // Enter Transparent mode
- // delay(80); // 80 ms delay before operation per datasheet
- // Serial.begin(9600); // Open serial port to computer
- // HC12.begin(9600); // Open software serial port to HC12
- //}
- //
- //void loop() {
- //
- //
- // // while (HC12.available()) { // If Arduino's HC12 rx buffer has data
- // // Serial.println(" mensagem recebida: ");
- // // Serial.write(HC12.read()); // Send the data to the computer
- // // }
- // // while (Serial.available()) { // If Arduino's computer rx buffer has data
- // // HC12.print(Serial.read()); // Send that data to serial
- // // }
- //
- //
- // while (HC12.available()) { // While Arduino's HC12 soft serial rx buffer has data
- // HC12ByteIn = HC12.read(); // Store each character from rx buffer in byteIn
- // HC12ReadBuffer += char(HC12ByteIn); // Write each character of byteIn to HC12ReadBuffer
- // if (HC12ByteIn == '\n') { // At the end of the line
- // HC12End = true; // Set HC12End flag to true
- // }
- // }
- //
- // while (Serial.available()) { // If Arduino's computer rx buffer has data
- // SerialByteIn = Serial.read(); // Store each character in byteIn
- // SerialReadBuffer += char(SerialByteIn); // Write each character of byteIn to SerialReadBuffer
- // if (SerialByteIn == '\n') { // Check to see if at the end of the line
- // SerialEnd = true; // Set SerialEnd flag to indicate end of line
- // }
- // }
- //
- // if (SerialEnd) { // Check to see if SerialEnd flag is true
- //
- // if (SerialReadBuffer.startsWith("AT")) { // Has a command been sent from local computer
- // HC12.print(SerialReadBuffer); // Send local command to remote HC12 before changing settings
- // delay(100); //
- // digitalWrite(HC12SetPin, LOW); // Enter command mode
- // delay(100); // Allow chip time to enter command mode
- // Serial.print(SerialReadBuffer); // Echo command to serial
- // HC12.print(SerialReadBuffer); // Send command to local HC12
- // delay(500); // Wait 0.5s for a response
- // digitalWrite(HC12SetPin, HIGH); // Exit command / enter transparent mode
- // delay(100); // Delay before proceeding
- // } else {
- // HC12.print(SerialReadBuffer); // Transmit non-command message
- // }
- // SerialReadBuffer = ""; // Clear SerialReadBuffer
- // SerialEnd = false; // Reset serial end of line flag
- // }
- //
- // if (HC12End) { // If HC12End flag is true
- // if (HC12ReadBuffer.startsWith("AT")) { // Check to see if a command is received from remote
- // digitalWrite(HC12SetPin, LOW); // Enter command mode
- // delay(100); // Delay before sending command
- // Serial.print(SerialReadBuffer); // Echo command to serial.
- // HC12.print(HC12ReadBuffer); // Write command to local HC12
- // delay(500); // Wait 0.5 s for reply
- // digitalWrite(HC12SetPin, HIGH); // Exit command / enter transparent mode
- // delay(100); // Delay before proceeding
- // HC12.println("Remote Command Executed"); // Acknowledge execution
- // } else {
- // Serial.print(HC12ReadBuffer); // Send message to screen
- // }
- // HC12ReadBuffer = ""; // Empty buffer
- // HC12End = false; // Reset flag
- // }
- //}
- #include <SoftwareSerial.h>
- const byte HC12TxdPin = 14; // Transmit Pin on HC12 TX Pin
- const byte HC12RxdPin = 15; // Recieve Pin on HC12 RX Pin
- const byte HC12SetPin = 18; // "SET" Pin on HC12
- //SoftwareSerial HC12(HC12TxdPin, HC12RxdPin); // Create Software Serial Port
- const int ledCount = 10; // the number of LEDs in the bar graph
- int ledPins[] = {
- 2, 3, 4, 5, 6, 7, 8, 9, 10, 16
- }; // an array of pin numbers to which LEDs are attached
- //Armazena os dados que chegam pela serial
- String inputString = "";
- //Variavel de string completa
- boolean stringComplete = false;
- //Dados do contador
- int inChar = 0;
- int ledLevel = 0;
- void setup() {
- pinMode(HC12SetPin, OUTPUT);
- digitalWrite(HC12SetPin, HIGH);
- Serial.begin(9600);
- Serial1.begin(9600);
- // Serial1.listen();
- // loop over the pin array and set them all to output:
- for (int thisLed = 0; thisLed < ledCount; thisLed++) {
- pinMode(ledPins[thisLed], OUTPUT);
- }
- digitalWrite(HC12SetPin, LOW); // Set HC-12 into AT Command mode
- delay(100); // Wait for the HC-12 to enter AT Command mode
- Serial1.print("AT+C003"); // Send AT Command to HC-12
- delay(200);
- digitalWrite(HC12SetPin, HIGH);
- delay(200);
- while (Serial1.available()) { // If HC-12 has data (the AT Command response)
- Serial.print(Serial1.read()); // Send the data to Serial monitor
- }
- }
- void loop() {
- // read the potentiometer:
- // int sensorReading = analogRead(analogPin);
- // map the result to a range from 0 to the number of LEDs:
- //Recebe os dados pelo HC-12 via porta serial
- // digitalWrite(3, HIGH);
- // Serial1.print(9);
- // Serial1.write(9);
- while (Serial1.available()) {
- // digitalWrite(5, HIGH);
- inChar = Serial1.read();
- ledLevel = inChar;
- if (ledLevel != 0) {
- Serial.println(inChar);
- for (int thisLed = 0; thisLed < ledCount; thisLed++) {
- // if the array element's index is less than ledLevel,
- // turn the pin for this element on:
- if (thisLed < ledLevel) {
- digitalWrite(ledPins[thisLed], HIGH);
- }
- // turn off all pins higher than the ledLevel:
- else {
- digitalWrite(ledPins[thisLed], LOW);
- }
- }
- }
- }
- //
- //
- // // loop over the LED array:
- // // for (int thisLed = 0; thisLed < ledCount; thisLed++) {
- // // // if the array element's index is less than ledLevel,
- // // // turn the pin for this element on:
- // // if (thisLed < ledLevel) {
- // // digitalWrite(ledPins[thisLed], HIGH);
- // // }
- // // // turn off all pins higher than the ledLevel:
- // // else {
- // // digitalWrite(ledPins[thisLed], LOW);
- // // }
- // // }
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement