I have a project ready and I need to add an OLED display to it, but as the project is encapsulated and resined, it is not possible to add it directly to the Arduino I am using. As the project uses serial communication, I thought about adding another Arduino along with the OLED Display to receive data from the Arduino that is encapsulated and send it to the display, but the Arduino that should receive the data is not receiving anything. I need help to solve this problem, I'm using two Arduinos for the mini.
Transmitter
#include <EEPROM.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define EEPROM_UNIT sizeof(float)
#define ONE_WIRE_BUS 4
float freq;
float H2O;
float AR;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
pinMode(3, INPUT);
pinMode(13, OUTPUT);
sensors.begin();
Serial.begin(9600); // Usando a porta serial padrão (Serial) para comunicação
}
void loop() {
digitalWrite(13, HIGH);
EEPROM.get(0, H2O);
EEPROM.get(100, AR);
lerFreq(500);
float Frelativa = (freq - AR) / (H2O - AR);
// Crie uma string para armazenar os dados dos sensores
String sensorData = "Sinal de umidade: ";
sensorData += String(Frelativa, 4); // Adicione o valor da umidade com 4 casas decimais
sensorData += "\nTemperatura: ";
sensors.requestTemperatures();
sensorData += String(sensors.getTempCByIndex(0));
sensorData += " °C";
// Envie os dados pela porta serial
Serial.println(sensorData);
digitalWrite(13, LOW);
delay(500);
}
void lerFreq(float intervalo) {
float pulsos = 0;
float tempoInicial = millis();
float tempo = tempoInicial;
boolean pulso;
while (tempo <= (tempoInicial + intervalo)) {
tempo = millis();
if (digitalRead(3) == HIGH) {
if (pulso == HIGH) {
pulsos = pulsos + 1;
}
pulso = LOW;
} else {
pulso = HIGH;
}
}
freq = pulsos / (intervalo / 1000);
}
void serialEvent() {
while (Serial.available()) {
String inputString = Serial.readStringUntil('\n');
if (inputString.substring(0, 3) == "AR#") {
for (int i = 1; i <= 10; i++) {
lerFreq(1000);
int soma = soma + freq;
AR = soma / i;
}
Serial.println(AR);
EEPROM.put(100, AR);
Serial.println(AR);
}
if (inputString.substring(0, 3) == "H2O") {
for (int i = 1; i <= 10; i++) {
lerFreq(1000);
int soma = soma + freq;
H2O = soma / i;
}
Serial.println(H2O);
EEPROM.put(0, H2O);
Serial.println(H2O);
}
}
}
Receiver
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
float receivedData = 0.0; // Variável para armazenar os dados recebidos
void setup() {
// Inicialize o display OLED
if(!display.begin(SSD1306_I2C_ADDRESS, OLED_RESET)) {
Serial.println(F("Falha ao inicializar o display OLED"));
while (true); // Se falhar, pare a execução
}
// Limpe o buffer do display
display.clearDisplay();
display.display();
// Inicialize a comunicação serial
Serial.begin(9600);
}
void loop() {
// Verifique se há dados disponíveis na porta serial
if (Serial.available() > 0) {
// Leia os dados da porta serial e armazene na variável receivedData
receivedData = Serial.parseFloat();
// Limpe o display
display.clearDisplay();
// Exiba os dados recebidos no display OLED
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(F("Dados Recebidos:"));
display.setCursor(0,10);
display.print(receivedData, 4); // Exibe os dados com 4 casas decimais
// Atualize o display
display.display();
}
}