The problem is that in my program the variable that prints at the end "resultado" does not appear with the parameters that I want which are the equations, instead it always appears 0.00.
I use an OLED display SDD1306 with i2c communication and the other variables like "dato1" or "dato2" seem to work fine.
I have tried reconnecting the wires but that is not the problem because with other programs they work correctly. The SDA is connected to the pin A4 and the SCK is connected to the pin A5. The voltage is 5V and the ground is connected normally.
I would be very grateful if you could help me ![]()
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
//Variables
float dato1;
float dato2;
float resultado;
String msg1="Ingresa 1er dato";
String msg2="Ingresa 2do dato";
String msg0="Resolver para:";
String tipos="V I R";
String operacion;
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
}
void loop() {
display.setTextSize(1.8);
display.setTextColor(WHITE);
display.setCursor(0, 10);
// Display static text
display.println(msg0);
display.setCursor(0, 45);
display.println(tipos);
display.display();
while (Serial.available() == 0){
}
operacion = Serial.readString();
display.clearDisplay();
Serial.print(operacion);
//DATO 1
display.setCursor(0,10);
display.print(msg1);
display.display();
delay(500);
while (Serial.available()==0){
}
dato1 = Serial.parseFloat();
display.clearDisplay();
Serial.print(dato1);
//DATO 2
display.setCursor(0,10);
display.print(msg2);
display.display();
delay(5000);
while (Serial.available()==0){
}
dato2 = Serial.parseFloat();
display.clearDisplay();
Serial.print(dato2);
//Realizar operaciones
if (operacion=="v"){
resultado = dato1*dato2;
}
else if (operacion=="i"){
resultado = dato1/dato2;
}
else if (operacion=="r"){
resultado = dato1/dato2;
}
//Resultado
Serial.println(resultado);
display.setCursor(0,10);
display.print("El resultado es:");
display.setCursor(0,40);
display.print(resultado);
display.display();
delay(10000);
display.clearDisplay();
}

