sending a analog read hc12

@georgegohl888

You may play around with the following setup to have some of the answers of your questions like:
1. How to transfer Battery Voltage like 1005 from NANO to UNO via HC12 Radio Module.
2. How to re-assemble the received data to get back the original data/Battery Voltage.

Operating Procedures:
1. Upload the following sketch into UNO-1. Bring in the Serial Monitor-1 at 9600 baud.

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12's STX Pin, HC-12's SRX Pin
void setup() 
{
  Serial.begin(9600);             // Harware Serial port to computer
  HC12.begin(9600);               // Software Serial port to HC12
}

void loop() 
{
  while (HC12.available())          // If HC-12 has data
  {        
    Serial.write(HC12.read());      // Send the data to Serial Monitor
  }
}

2. Upload the following sketch into NANO. This program of NANO acquires analog voltage on channel A0 at 5-sec interval, and sends it to UNO via HC12 Radio Module. For transmission of data, the program has used HC12.print() method.

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin

unsigned long int pmillis;
void setup()
{
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
  pmillis = millis();
}

void loop()
{
  while (millis() - pmillis < 5000)
    ;
  pmillis = millis();
  unsigned int x = analogRead(A0); //4.0V = 0x0333
  HC12.print(x);
  HC12.println();
  //while(1);

}

3. Use a jumper wire and connect 3.3V (3V3) point (it is physically 3.63V; also 5V is 4.7V) of the NANO with A0-pin of NANO.

4. Press the RESET button of NANO. Check that around 791 (decimal) has appeared on the Serial Monitor-1 of UNO.

Why and how is it 791
In the sketch of NANO, there is an instruction unsigned int x = analogRead(A0) which has stored around (theoretically) 0x0317 [(1024/4.7)*3.63 = 791] into variable x.

The .print() method/function reads the value of x, uses %10 and /10 operators (my understanding) and find the indices 7, 9, 1 for the corresponding decimal value of 791.

After that, the .print() method writes 0x37 (ASCII code of 7), 0x39 (ASCII code of 9), and 0x31 (ASCII code 1) one after another into HC12 Module.

And thhen, the HC12 module transmits the data using asynchronous serial transmission protocol.

How to get 3.63V back from the received data: 0x37, 0x39, and 0x31?
Reverse process!
Extract 791 from 0x37, 0x39, and 0x31
Evaluate (791*4.7)/1024 to get = 3.63.