Is this what you meant? I cannot test it at the moment whether the height reaction causes the beeps but I would like your opinion whether this code is plausible. I still have one problem but the no signal window flashes on and off between the height and no Rx but I know why but would like it when there is really no RX after a certain time not just between the loop times, this would mean more delays.. Anyway your comments would be most welcome.
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const byte piezoPin = 5;
float Height = 0;
float oldHeight = 0;
int beep = 0;
int n = 0;
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
SoftwareSerial mySerial(2, 3); // RX, TX
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (mySerial.available() > 0 && newData == false) {
rc = mySerial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
} else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
mySerial.print(F("AT+C001\r\n"));
delay(100);
digitalWrite(7, HIGH);
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // initialize with the I2C addr 0x3D (for the 128x64)
noTone(piezoPin);
}
void loop() {
recvWithStartEndMarkers();
if (newData == false) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(27, 5);
display.println("NO RX ! ");
display.setCursor(2, 40);
display.println("TURN TX ON");
display.display();
// tone(piezoPin, 3000, 200);
// delay(1000);
}
if (newData == true) {
Serial.println(receivedChars);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(20, 1);
display.println("Altitude");
display.setCursor(20, 20);
display.println("in Feet");
display.setTextSize(2);
display.setCursor(55, 50);
display.println(Height);
display.display();
delay(50);
Height = atof(receivedChars);
beep = (oldHeight / 100 - Height / 100);
if (beep < 1) {
noTone;
oldHeight = Height;
}
if (beep >= 1) {
n = beep;
for (int i = 0; i <= n; i++)
tone(piezoPin, 3000, 200);
delay(1000);
oldHeight = Height;
}
if (beep >= 8) {
tone(piezoPin, 3000, 200);
delay(3000);
oldHeight = Height;
}
Serial.print ("oldHeight");
Serial.println (oldHeight);
}
newData = false;
}