Sending a voltage hc12

Hi everyone,

SO what im trying to do is have a transmitter on a drone and a receiver on the ground SO i can monitor the voltage of the battery. I have the Receiver set up with a 16x2 lcd screen. I have got it working so i can send messages over the serial connections on the computer but sending a analog read value seems to be the issue. The hc12 can only send 1 character at a time and i tried to send a string containing only 1 character but that doesn’t work it throws an error.
here is what ive got:

transmitter:

#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
String battvlt;
void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC12
}
void loop() {
  while (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial monitor
  }
  while (Serial.available()) {// If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }
  while (Serial.available() == false){
    battvlt = String(analogRead(2));
    delay(100);
    HC12.write("?");
  }
}

Reciever:

#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
SoftwareSerial HC12(6, 9); // HC-12 TX Pin, HC-12 RX Pin
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte test;
void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600); // Serial port to HC12
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("battery monitor");
  lcd.setCursor(0, 1);
}

void loop() {
  while (HC12.available()) { 
    test = HC12.read();// If HC-12 has data
    Serial.write(test);
    lcd.write(test);
    // Send the data to Serial monitor
  }
  if ((HC12.available()) == false){
    lcd.setCursor(0,1);
  }
  
  while (Serial.available()) {      // If Serial monitor has data
    HC12.write(Serial.read());      // Send that data to HC-12
  }

}

Thanks for any help

it throws an error

Please share the full error message with us

  while (Serial.available() == false){

Serial.available returns the number of bytes in the incoming buffer. Why would you compare that number to false?

    HC12.write("?");

How is THAT useful data?

Do you KNOW the difference between write() and print()? Do you know which one you should be using?

i dont really know, earlier in the example code it said while (serial.available) so i presumed it was a boolean. And the question mark shows that i dont know what to put there and how to write a string to it. Also im not 100% on the difference i think print is a string and write is the actual value representing the character. I cant give the exact error code because im away form my computer but it was saying it couldnt write a string i think.

earlier in the example code it said while (serial.available) so i presumed it was a boolean.

Read the documentation. Do not assume anything.

And the question mark shows that i dont know what to put there

I hope that that is only true from a "writing the code" point of view. You know what you want to send to the other device. Tell us that. We'll tell you how to code that, if it can be done.

Hey thanks for the help, all im trying to do is tosend a voltage off one of the analogue pins over the hc12 to my receiver.

and yes the question mark is because i don't know how to send it.

The bluetooth system sends bytes, not voltages.

What would your receiver do with a voltage?

You don't get a voltage from an analog pin, anyway. You get a value, from 0 to 1023, that indicates the relationship between the voltage input and the reference voltage.

yes i know that its 1-1023 at the receiver i convert it back to the battery voltage using relevant maths. If the system sends bytes then how can i send say a value of 500 across it? Thats what i dont know how to do. The receiver shows what the battery voltage is on the display so i know what the battery voltage is so i dont fly for too long.
Thanks for the help :slight_smile: