Hello Community.
I seek help to code my setup.
I have 2x Arduinos, 2x HC 12, 1x Oled and 1x Ultrasonic distance sensor (maxbotix).
I need to transfer the distance data from one Arduino to the other and show it on the OLED panel.
This is the code I have been using with everything wired to only one Arduino, and it is working properly:
#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
const int anPin1 = 0;
long distance1;
void setup() {
Serial.begin(9600); // sets the serial port to 9600
u8g.begin();
}
void loop() {
distance1 = analogRead(anPin1)*5;
distance1 = (distance1/10+25);
u8g.firstPage();
do {
draw(distance1);
}
while (u8g.nextPage());
delay(100);
}
void draw(int distance1) {
u8g.setFont(u8g_font_fur30);
u8g.setPrintPos(0, 63);
u8g.print(distance1);
u8g.print("cm");
}
Now I need to implement the HC12 - but how?
My best try so far:
Transmitter:
#include <SoftwareSerial.h>
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
const int anPin1 = 0;
long distance1;
void setup(){
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
}
void loop(){
distance1 = analogRead(anPin1)*5;
distance1 = (distance1/10+25);
while (HC12.available()) { // If HC-12 has data
HC12.write(distance1); // Send the data to HC12
}
}
Reciever:
#include <SoftwareSerial.h>
#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST); // Fast I2C / TWI
SoftwareSerial HC12(10, 11); // HC-12 TX Pin, HC-12 RX Pin
const int anPin1 = 0;
long distance1;
void setup() {
Serial.begin(9600); // Serial port to computer
HC12.begin(9600); // Serial port to HC12
u8g.begin();
}
void loop() {
while (HC12.available()) { // If HC-12 has data
int distance1 = HC12.read();
}
u8g.firstPage();
do {
draw(distance1);
}
while (u8g.nextPage());
delay(100);
}
void draw(int distance1) {
u8g.setFont(u8g_font_fur30);
u8g.setPrintPos(0, 63);
u8g.print(distance1);
u8g.print("cm");
}
Thank you for all your help!!