In the code below I am receiving a height value via the HC12 serial TRX. This all works very well and I have coded a FOR loop to get a piezo buzzer to buzz the number of (100) as the height is reached. When the height changes and the threshold for N which is the height / 100 then this is not recognized because the loop keeps going. I want the number of buzzes to correspond with the height in 100's of feet. i.e. 100 feet one buzz then 200 feet two buzzes etc. until 800 feet. I also have a problem with the blanked out code, which should show that there is no received data from the HC12. I am not sure where to place this code so it recognizes the lack of data reception. I could do withou this but it would be the icing on the cake. Please help me... thank you.
the way your recvWithStartEndMarkers(); function works is non blocking - so you should not try to read or do anything with the receivedChars buffer until newData == true (which you clear when you call showNewData())
void loop() {
recvWithStartEndMarkers();
if (newData) {
// here you have received your full message
Serial.print("Height in Feet = ");
Serial.println(receivedChars);
// handle what you need to do
// ......
newData = false; // get ready for the next message
}
}
Thank you for the prompt reply. I am not sure what you mean, are you referring to the for loop or my problem where to put the blanked part of my code. If you could explain in more detail it would help me.
Thank you I will give this a try. I had to go out thus the late answer. Your last message appeared but the code was not there just your written message. Now i can see it all. Will get bak. In the eman time thank you very much.
When I start the TX the first data that comes to the rx is around 5000 feet. I then push a button on the TX arduino board and the data is set to the height that I am at as 0 feet. I have loaded your code and the Oled shows 5000 feet and the buzzer just buzzes as set in the code. When I push the button on the TX the height on the tx goes to 0 but the rx is still stuck in a loop and shows 5000 feet on the Oled.The buzzer should not be on but it does buzz. If I may repeat perhaps more clearly what I am trying to achieve.
When the Oled shows 100 feet the buzzer should only buzz once every say 2 seconds, if the height is 200 feet then twice very 2 seconds and so non until it buzzes 8 times every 2 seconds at 800 feet. After every buzzer sequence of 2 seconds the code should check whether 200 feet has been reached and not stick in the loop. There should be no buzzer until 100 feet is reached. I hope this is clearer now, did you misunderstand me?
The data reived only comes when I restart the receiver board. The data is correct. but the buzzing keeps on and the data receive is not possible until a reset is made.
The buzzer starts as soon as the receivedChars is over 0. Otherwise it stops. After I push the button to zero the height being sent by the TX there is a little fluctuation but this is not a problem. The flutuation varies between - over 0 to plus a few decimal places as you can see on the printout.
I am receiving something meaningful, the height is correct. The minus comes from the small changes in air pressure. The atof only changes the char to a float.The height value is a float value of the receivedChars. The height is not doubled. The values I am seeing are really OK, but I said at the moment when I set the height with the button it can vary a small amount. The buzzing should only start when I reach the first 100 feet.
Sorry I did not understand about the height. I have removed one line now. Yes the height is sent relative to 0 feet which I set with a button when the TX is on the ground. Then the TX is in a model glider and as it asends the data is sent to the RX and displayed on the Oled but I have to watch the plane so i need an indication with the buzzer when I rech 100 meters and so on up to 800 meters which is the max I can fly according to law. The 800 meter buzzer then has to be constant and not intermittent. I hope this shows what i am trying to achieve.
Ok so you get absolute height then and the data you showed above was just your glider being on the ground and small variations due to accuracy
So your ask is 1 beep when you cross 100m and then no beep until you cross 200m when you do 2 beeps etc and above 800m it’s a constant beep
This is easily programmed with a small state machine. Steps could be
BELOW_100
BELOW_200
BELOW_300
...
BELOW_800
TOO_HIGH
You monitor height and depending on current state you check when cross to a new level
switch(state) {
case BELOW_100: // we were below 100
if (Height >= 100) { // we are crossing the limit
state = BELOW_200; // set new state and beep once
beep(UP, 1); // write this function to beep the right number of times with frequency indicating up or down
}
break;
case BELOW_200: // we were below 200
if (Height >= 200) { // we are crossing the limit
state = BELOW_300; // set new state and beep twice
beep(UP, 2);
} else // check in case we went back below 100
if (Height <= 98) { // manage a bit of room to not beep all the time around 100m
state = BELOW_100; // record new state and beep once indicating crossing down
beep(DOWN, 1);
}
break;
case BELOW_300:
if (Height >= 300) {
state = BELOW_400;
beep(UP, 3);
} else
if (Height <= 198) {
state = BELOW_200;
beep(DOWN, 2);
}
break;
... you get the idea
}
This is by far not the best code and can heavily be simplified with just a variable holding previous height and checking if trasnsition was crossed but this has the advantage of being super easy to read and could get you started
Read about enum to represent states with keywords in your language
enum : byte {BELOW_100, BELOW_200, BELOW_300, ..., BELOW_800, TOO_HIGH} state; // of course don’t write ... I’m lazy you need all the keywords in there
Hi J-M-L, sorry for the long delay in a answering but I have been trying your suggestion.
You are very kind to help me but my knowledge is just not enough to follow your suggestion.
I think I am going to eave the problem for a bit, it is far more complicated than I ever imagined.
When I think through the logic thought I see now that it is complex. many thanks again for your kind efforts.
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.