Hello All, I am working on a project where I am using a proximity sensor and displaying the distance on an OLED display. My issue is I need to separate the oled and the sensor because I cant have a cable track. I want to use two nodemcu's for the small footprint. I have used them as a transmitter and receiver to turn on a relay. However, I am not sure how to use them where I can transmit the distance to the receiver? Would anyone be able to steer me in the right direction? I will paste the working distance code. I hope I am following correct protocol here. Thank you in advance.
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define CommonSenseMetricSystem
//#define ImperialNonsenseSystem
#define trigPin 13
#define echoPin 12
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128x64)
display.clearDisplay();
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); //PULSE ___|---|___
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
#ifdef CommonSenseMetricSystem
distance = (duration / 2) / 29.1;
#endif
#ifdef ImperialNonsenseSystem
distance = (duration / 2) / 73.914;
#endif
display.setCursor(0, 0); //oled display
display.setTextSize(4);
display.setTextColor(WHITE);
display.println(distance);
display.setCursor(95, 0);
display.setTextSize(2);
#ifdef CommonSenseMetricSystem
display.println("cm");
#endif
#ifdef ImperialNonsenseSystem
display.println("in");
#endif
display.display();
delay(500);
display.clearDisplay();
Serial.println(distance); //debug
}