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();
}
