'Problem with HC12 transmitter and receiver code

I am new to arduino and I want to send values for temperature, pressure, x, y and z acceleration using a HC12 transmitter and I don't know why its not working. Any help would be greatly appreciated. The code is fine apart from any parts regarding the HC12 receiver and transmitter.

This is my code for the receiver:

#include <Adafruit_BMP280.h> // For the purple BMP280 sensor board
#include <SoftwareSerial.h>

SoftwareSerial HC12(7,6);

// Purple BMP280 Sensor Board
Adafruit_BMP280 bmp; //use I2C interface
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();

//initialising variables and function
float x_acc1;
float x_acc2;
float x_ms2;
float y_acc1;
float y_acc2;
float y_ms2;
float z_acc1;
float z_acc2;
float z_ms2;
float acc_modulus1;
float acc_modulus2;
float acceleration;
float x_modulus;
float y_modulus;
float z_modulus;
float temp;
float pressure;
float calculate_altitude(float temp, float pressure);
float altitude;
float altitude1;
float altitude2;
float altitude3;
float x;
float y;
float z;
float overall_acceleration(float x, float y, float z);

void setup(){
//Setup for purple BMP280
Serial.begin(115200);
HC12.begin(115200);

Serial.println(F("BMP280 Sensor event test"));

if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
while (1) delay(10);
}

//Default settings from datasheet
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, //Opertaing Mode
Adafruit_BMP280::SAMPLING_X2, //Temp. oversampling
Adafruit_BMP280::SAMPLING_X16, //Pressure oversampling
Adafruit_BMP280::FILTER_X16, //Filtering
Adafruit_BMP280::STANDBY_MS_500); //Standby time

bmp_temp->printSensorDetails();

}

//Calculation for altitude using temperature and pressure values via function (to declutter loop function)

float calculate_altitude(float temp, float pressure)
{
float altitude;

altitude1 = 1013.25/(pressure);
altitude2 = pow(altitude1,0.190222560);
altitude3 = altitude2-1;
altitude = (altitude3*(temp +273.15))/0.0065;

return altitude;
}

float overall_acceleration(float x, float y, float z)
{
float acc_modulus2;

x_acc1= (x-360.5);
x_acc2= x_acc1*981;
x_ms2= x_acc2/7350;

y_acc1= (y-360.5);
y_acc2= y_acc1*981;
y_ms2= y_acc2/7150;

z_acc1= (z-396);
z_acc2= z_acc1*981;
z_ms2= z_acc2/7200;

x_modulus= pow(x_ms2,2);
y_modulus= pow(y_ms2,2);
z_modulus= pow(z_ms2,2);

acc_modulus1= x_modulus + y_modulus+ z_modulus;
acc_modulus2= sqrt(acc_modulus1);
}
void loop() {

//Obtaining temperature and pressure events off BMP280
sensors_event_t temp_event, pres_event;
bmp_temp->getEvent(&temp_event);
bmp_pressure->getEvent(&pres_event);

//Taking temperature and pressure values from the events

temp = temp_event.temperature;
pressure = pres_event.pressure;

//calling function declared previously
altitude = calculate_altitude(temp, pressure);
acceleration = overall_acceleration(x,y,z);

//Reading acceleration values in X, Y and Z axis directions from accelerometer
x = analogRead(A1);
y = analogRead(A2);
z = analogRead(A3);

String data = ",";
data = data + x + "," + y + "," + z + "," + temp + "," + pressure;

HC12.println(data);
Serial.print(" temperature (C) = ");
Serial.print(temp); //Print temperature value
Serial.print(",");
Serial.print(" pressure (hPa) = ");
Serial.print(pressure); //Print pressure value
Serial.print(",");
Serial.print(" altitude (m) = ");
Serial.print(altitude); //Read altitude value
Serial.print(",");
Serial.print(" x axis acceleration (m/s^2) = ");
Serial.print(x_ms2); //Read acceleration in X direction
Serial.print(",");
Serial.print(" y axis acceleration (m/s^2) = ");
Serial.print(y_ms2); //Read acceleration in Y direction
Serial.print(",");
Serial.print(" z axis acceleration (m/s^2) = ");
Serial.println(z_ms2); //Read acceleration in Z direction

delay(100);
}

This is my code for the transmitter:

#include <SoftwareSerial.h>

SoftwareSerial HC12(7, 6); //RX, TX

void setup() {
Serial.begin(9600);
HC12.begin(9600);
}

void loop() {

if(Serial.available() > 0){
String data = Serial.readString();
HC12.println(data);
}

if(HC12.available() > 1){
String data = HC12.readString();
Serial.println(data);
}
delay(20);
}



I have tried to use methods from many examples online but have not found any of them useful as I am new to Arduino and C. Without the code regarding HC12 and just plugging the arduino nano into the computer, the arduino works. This makes me certain that there is a problem with the HC12 code as I am repeatedly receiving this error:

avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0xe7



Solution 1:[1]

You must first remove all other devices connected to the arduino nano and upload the LED blink code.

If it uploads correctly and works, we can proceed to the next step. But if there is a problem, it must be solved first.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Duruthu Automation