Hello Community.
I seek your help to add the value "20" to every measure coming from a wireless ultrasonic sensor.
This might be a noob question, but I am having a lot of trouble getting it right, so all help is appreciated!
#include <SoftwareSerial.h>
#include <U8glib.h>
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
long distance1;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
SoftwareSerial HC12(5, 4); // HC-12 TX Pin, HC-12 RX Pin
void setup()
{
Serial.begin(19200); // Serial port to computer
HC12.begin(19200); // Serial port to HC12
u8g.begin();
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (HC12.available() > 0 && newData == false) {
rc = HC12.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.println(receivedChars);
u8g.firstPage();
do
{
u8g.setFont(u8g_font_fur30);
u8g.setPrintPos(0, 63);
u8g.print(receivedChars);
u8g.print("cm");
newData = false;
}
while (u8g.nextPage());
}
}