Help Needed With Flight Computer Code

The context: I'm working on a flight computer for a small model rocket. The setup is basically that the rocket has a teensy 4.1, MPU5060, a relay, and a HC-12 onboard. That HC-12 communicates with a ground station based on an Arduino Nano that contains the Nano, HC-12, another relay and a HC-05 which communicates with a cell phone. Right now the communication coming from the rocket sending height above ground and vertical velocity data is working very well. However I want to be able to communicate from my phone to the rocket so that I can tell it to start logging data to an onboard SD card. When I send a character from the phone to the ground station however its not registering that character correctly instead it just shows up as a question mark inside of some other string of numbers. How can I get around this problem?

Ground Station Code;

#include <SoftwareSerial.h>

SoftwareSerial HC12(4, 5); // HC-12 TX Pin, HC-12 RX Pin

void setup() {
  Serial.begin(9600);             // Serial port to computer
  HC12.begin(9600);               // Serial port to HC
}


void loop() {
    if (HC12.available()) {        // If HC-12 has data
    Serial.write(HC12.read());      // Send the data to Serial Devices (BT module and serial monitor)
  }

  if (Serial.available()) {      // If Serial monitor/HC-05 has data 
  Serial.println(Serial.read()); // Print the data to serial monitor and HC-05
    HC12.write(Serial.read());      // Send that data to HC-12
  }

}

Flight Computer Code

//Include all libraries 
#include <Wire.h> //Important for SCL and SDA comunication from the pressure sensor and altimiter
#include "SparkFunMPL3115A2.h"  //Pressure sensor library 
#include <SoftwareSerial.h>     //Serialport from digital pins library 

int relayPin = 3; //initilizes the pin for the relay 

//Create an instance of the object
MPL3115A2 myPressure;

//Time variables
float Time1 = 0;
float Time2 = 0;

// Altitude varaibles callout
float altitude1 = 0;
float altitude2 = 0;
float altitude = 0;

// velocity variables callout
float velocity = 0;

// Height variables callout
float height = 0;

//correction for ground level
float altitude_initial = 0;


void setup()
{
  Wire.begin();        // Join i2c bus

  pinMode(3,OUTPUT); // sets the relay pin as an output

  Serial.begin(9600);  // Start serial for output

Serial2.begin(9600);  //HC12 hardware serail port 

  myPressure.begin(); // Get sensor online

  //Configure the altitude sensor
  myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
  myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
  myPressure.enableEventFlags(); // Enable all three pressure and temp event flags   

//altitude_initial = myPressure.readAltitudeFt();   //Takes a zero reading for altitude
int i=0;
float altsuminitial;
while (i < 100 )  {
altitude_initial = myPressure.readAltitudeFt();
altsuminitial = altsuminitial + altitude_initial ; 
i = i + 1 ; 
}
i = 0;
altitude_initial = altsuminitial/100;

}



void loop()  {

int i= 0 ;  //set counter varaible for the while loops
float altsum1 = 0; //altitude sum for loop one
float altsum2 = 0;  //altitude sum for loop two 


// Does two readings and then averages them out to get an altitude and time reading 
while (i < 2 )  {
altitude1 = myPressure.readAltitudeFt();
altsum1 = altsum1 + altitude1 ; 
i = i + 1 ; 
}
i = 0;
altitude1 = altsum1/2;
Time1 = millis();

delay(10);

// Does three readings and then averages them out to get asecond altitude and time reading 
while (i < 2 ) {
altitude = myPressure.readAltitudeFt();
altsum2 = altsum2 + altitude ; 
i = i + 1 ; 
}
altitude2 = altsum2/2;
Time2 = millis();


altitude = (altitude1+altitude2)/2 ;   //takes those two values and averages them again then posts the final altitude value

height = altitude - altitude_initial;    //takes the altitude value and subtracts the initial value plugged in up top to find the height above ground 

velocity = (altitude2 - altitude1) / (Time2 - Time1);  //takes velocity by doing the altitude difference over the time difference 

// //Velocity values checkout delete after they are verified
// Serial2.print(altitude2);
// Serial2.print(",");
// Serial2.print(altitude1);
// Serial2.print(",");
// Serial2.print(Time2);
// Serial2.print(",");
// Serial2.println(Time1);


if(velocity < -1 ) {
digitalWrite( relayPin, HIGH);
  Serial.println("chutes out");
  delay(5000);
  digitalWrite(relayPin, LOW);
}


// float temperature = myPressure.readTempF();  //collects temperature information 

// //Prints the altitude values that were pervously calculated to the serial monitor 
// Serial.print(altitude);
// Serial.print("[ft]");
// Serial.print("  ,  ");

// //Prints the altitude value to the HC12
// (Serial2.available());
// Serial2.print(altitude);
// Serial2.print("[ft]");
// Serial2.print("  ,  ");


//Prints the height value to the serial monitor 
Serial.print(height);
Serial.print("[ft]");
Serial.print("  ,  ");

//Prints the height value to the HC12
(Serial2.available());
Serial2.print(height);
Serial2.print("[ft]");
Serial2.print("  ,  ");

//Prints the velocity value to the serial monitor 
Serial.print(velocity);
Serial.println("[ft/s]");
Serial.println("  ");

//Prints the velocity value to the HC12
(Serial2.available());
Serial2.print(velocity);
Serial2.println("[ft/s]");
Serial2.println();

// //prints the temperature value to the serial monitor 
// Serial.print(temperature);
// Serial.println("[F]");
// Serial.println();

// //Prints the temp value to the HC12
// (Serial2.available());
// Serial2.print(temperature);
// Serial2.println("[F]");
// Serial2.println();


}

Compliments for your approach.
You can't share your serial port between serial monitor and bt module. At least contemporarily.
In theory you could have multiple softwareserials, one at a time, but I personally don't see it reliable.
Maybe better to substitute ground station board with esp32.

If you take input from the phone, are you still going to need input from the Serial monitor?

You can put the HC05 BT module on the hardware serial pins after the code has been loaded to the board. The Arduino will only be able to receive serial data from the phone but can output to both the phone and the monitor.

This is the method all the (Arduino) car kits use to upload then control it with BT.

I don't need to have input from the serial monitor just the input from the HC-05. And indeed with the Nano since there is just the one hardware serial port I disconnect the HC-05 before uploading code. Ive done stuff in the past like the Bluetooth cars however it seems that my trip up is happening when I ask it to do two way communication. Do I need to break the link between the two HC-12's in order to let my HC-05 get a word in?

I'm not clear how on the Nano base station the software serial connection for the HC12 is interactive with the Serial connection of the HC05?

Are there issues when the base Nano is only connected to the Serial monitor?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.