I already made my code but when I run it my screen does not show the menu and the buttons do not work

I already made my code but when I run it my screen does not show the menu and the buttons do not work

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C // Dirección del dispositivo OLED

#define OLED_RESET 4 // Reset pin #
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int boton1Pin = 2; // Pin del botón 1
const int boton2Pin = 3; // Pin del botón 2
// Definición de los puntos de tiempo y valor
int tiempo[] = {0, 2, 4};
int valor[] = {2, 1, 3};

int tiempo_x0[] = {0, 2, 4};
int valor_x0[] = {0, 1, 1};

int tiempo_x1[] = {0, 2, 4};
int valor_x1[] = {0, 1, 1};
int botonState = 0; // Estado del botón (0 = menu, 1 = suma, 2 = resta)

void setup() {
Serial.begin(9600);
displayMenu();
delay(5000);
display.display();
pinMode(boton1Pin, INPUT_PULLUP);
pinMode(boton2Pin, INPUT_PULLUP);

if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("Fallo en la asignación SSD1306"));
for (;:wink: {} // No continuar, bucle infinito
}

display.clearDisplay();
displayMenu();
}
void loop() {

int boton1State = digitalRead(boton1Pin);
int boton2State = digitalRead(boton2Pin);

if (boton1State == HIGH || boton2State == HIGH) {
// Esperar a que se suelte el botón
while (digitalRead(boton1Pin) == HIGH || digitalRead(boton2Pin) == HIGH) {
delay(50);
}
botonState = (botonState + 1) % 3;

switch (botonState) {
case 1:
display.clearDisplay();
display.setCursor(0, 0);
display.println("Operacion: Suma");
display.println("5 + 10 = 15");
display.display();
delay(2000);
botonState = 0;

  break;
case 2:
  display.clearDisplay();
  encapsulatedCode(); // Llamar a la función encapsulada aquí
  botonState = 0;
  display.display();
  delay(2000);
  botonState = 0;
  break;
default:
   displayMenu(); // Mostrar el menú por defecto
  break;

}
}

static int state = 0; // Variable para rastrear el estado actual

while (digitalRead(boton1Pin) == HIGH) {
delay(50);
}

// Limpiar pantalla
display.clearDisplay();

// Realizar acciones según el estado actual
switch(state) {
case 0: // Mostrar tabla de valores
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Tabla de valores:");
display.print("Tiempo\tValor\n");
for (int i = 0; i < sizeof(tiempo) / sizeof(tiempo[0]); i++) {
display.print(tiempo[i]);
display.print("\t");
display.println(valor[i]);
}
break;

case 1: // Mostrar tabla en binario
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("Tabla en binario:");
  display.println("Valor\tBinario");
  for (int i = 0; i < sizeof(valor) / sizeof(valor[0]); i++) {
    display.print(valor[i]);
    display.print("\t");
    display.println(String(valor[i], BIN));
  }
  break;

case 2: // Mostrar gráfica
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, SCREEN_HEIGHT / 2 + 10); // Ajuste para centrar la etiqueta X
  display.println("");
  
  // Dibujar ejes
  display.drawLine(0, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, SSD1306_WHITE); // Eje X
  display.drawLine(0, SCREEN_HEIGHT - 1, 0, 0, SSD1306_WHITE); // Eje Y
  
  // Dibujar etiquetas
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(SCREEN_WIDTH - 30, SCREEN_HEIGHT - 10); // Etiqueta X
  display.print("Tiempo");
  display.setCursor(5, 0); // Etiqueta Y
  display.print("Valor");
  
  // Conectar puntos de datos con líneas
  for (int i = 0; i < sizeof(tiempo) / sizeof(tiempo[0]) - 1; i++) {
    int x1 = map(tiempo[i], 0, 4, 0, SCREEN_WIDTH); // Ajuste para aumentar el tamaño
    int y1 = map(valor[i], 0, 4, SCREEN_HEIGHT, 0); // Ajuste para aumentar el tamaño
    int x2 = map(tiempo[i+1], 0, 4, 0, SCREEN_WIDTH); // Ajuste para aumentar el tamaño
    int y2 = map(valor[i+1], 0, 4, SCREEN_HEIGHT, 0); // Ajuste para aumentar el tamaño
    display.drawLine(x1, y1, x2, y2, SSD1306_WHITE);
  }
  break;
  
case 3: // Mostrar mensaje de agradecimiento
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0,0);
  display.println("MUESTREO DE TIEMPO:");
  break;

case 4:  // Mostrar gráfica de x0
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, SCREEN_HEIGHT / 2 + 10); // Ajuste para centrar la etiqueta X
  display.println("");
  
  // Dibujar ejes
  display.drawLine(10, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, SSD1306_WHITE); // Eje X
  display.drawLine(10, SCREEN_HEIGHT - 1, 10, 0, SSD1306_WHITE); // Eje Y
  
  // Dibujar etiquetas
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(SCREEN_WIDTH - 30, SCREEN_HEIGHT - 10); // Etiqueta X
  display.print("T: X0");
  display.setCursor(15, 0); // Etiqueta Y
  display.print("V: X0");

  // Conectar puntos de datos con líneas para señal x0
  for (int i = 0; i < sizeof(tiempo_x0) / sizeof(tiempo_x0[0]) - 1; i++) {
    int x1 = map(tiempo_x0[i], 0, 4, 10, SCREEN_WIDTH - 10); // Ajuste para reducir el tamaño y mover a la izquierda
    int y1 = map(valor_x0[i], 0, 1, SCREEN_HEIGHT - 10, 10); // Ajuste para reducir el tamaño
    int x2 = map(tiempo_x0[i+1], 0, 4, 10, SCREEN_WIDTH - 10); // Ajuste para reducir el tamaño y mover a la izquierda
    int y2 = map(valor_x0[i+1], 0, 1, SCREEN_HEIGHT - 10, 10); // Ajuste para reducir el tamaño
    display.drawLine(x1, y1, x2, y1, SSD1306_WHITE); // Línea horizontal
    display.drawLine(x2, y1, x2, y2, SSD1306_WHITE); // Línea vertical
    display.setCursor(x1 - 5, y1 - 10); // Ajustar posición del texto
    display.print(valor_x0[i]);
  }
  // Mostrar el último valor
  display.setCursor(map(tiempo_x0[2], 0, 4, 10, SCREEN_WIDTH - 10) - 5, map(valor_x0[2], 0, 1, SCREEN_HEIGHT - 10, 10) - 10);
  display.print(valor_x0[2]);
  break;
  
case 5:// Mostrar gráfica de x1
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, SCREEN_HEIGHT / 2 + 10); // Ajuste para centrar la etiqueta X
  display.println("");
  
  // Dibujar ejes
  display.drawLine(10, SCREEN_HEIGHT - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT - 1, SSD1306_WHITE); // Eje X
  display.drawLine(10, SCREEN_HEIGHT - 1, 10, 0, SSD1306_WHITE); // Eje Y
  
  // Dibujar etiquetas
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(SCREEN_WIDTH - 30, SCREEN_HEIGHT - 10); // Etiqueta X
  display.print("T1: X1");
  display.setCursor(15, 0); // Etiqueta Y
  display.print("V1: X1");

  // Encontrar el valor máximo de valor_x0
  float max_valor_x0 = valor_x0[0];
  for (int i = 0; i < sizeof(valor_x0) / sizeof(valor_x0[0]); i++) {
    if (valor_x0[i] > max_valor_x0) {
      max_valor_x0 = valor_x0[i];
    }
  }

  // Ajustar valores de valor_x1 para que sigan el patrón deseado
  for (int i = 0; i < sizeof(valor_x1) / sizeof(valor_x1[0]); i++) {
    valor_x1[i] = abs(valor_x0[i] - max_valor_x0);
  }

  // Conectar puntos de datos con líneas para señal x1
  for (int i = 0; i < sizeof(tiempo_x1) / sizeof(tiempo_x1[0]) - 1; i++) {
    int x1 = map(tiempo_x1[i], 0, 4, 10, SCREEN_WIDTH - 20); // Ajuste para reducir el tamaño y mover a la izquierda
    int y1 = map(valor_x1[i], 0, max_valor_x0, SCREEN_HEIGHT - 10, 12); // Ajuste para reducir el tamaño
    int x2 = map(tiempo_x1[i+1], 0, 4, 10, SCREEN_WIDTH - 20); // Ajuste para reducir el tamaño y mover a la izquierda
    int y2 = map(valor_x1[i+1], 0, max_valor_x0, SCREEN_HEIGHT - 10, 10); // Ajuste para reducir el tamaño
    display.drawLine(x1, y1, x2, y1, SSD1306_WHITE); // Línea horizontal
    display.drawLine(x2, y1, x2, y2, SSD1306_WHITE); // Línea vertical
    display.setCursor(x1 - 5, y1 - 10); // Ajustar posición del texto
    display.print(valor_x1[i]);
  }
  // Mostrar el último valor
  display.setCursor(map(tiempo_x1[2], 0, 4, 10, SCREEN_WIDTH - 10) - 5, map(valor_x1[2], 0, max_valor_x0, SCREEN_HEIGHT - 10, 10) - 10);
  display.print(valor_x1[2]);
  break;

}

// Mostrar
display.display();

// Incrementar el estado o reiniciar si llega al final
state = (state + 1) % 6;

// Esperar a que se suelte el botón
while (digitalRead(boton1Pin) == LOW) {
delay(50); // Esperar
}

// Esperar un momento antes de pasar al siguiente estado
delay(500);
displayMenu(); // Mostrar el menú por defecto

display.clearDisplay();
}

void encapsulatedCode() {
Serial.begin(9600);
displayMenu(); // Mostrar el menú por defecto

pinMode(boton1Pin, INPUT_PULLUP); // Configura el pin del botón como entrada con pull-up interno
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("Fallo en la asignación SSD1306"));
for (;:wink: {} // No continuar, bucle infinito
}
}
void displayMenu() {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Menu");
display.println("1. Suma");
display.println("2. Resta");
display.display();
}

What does your code do?
When did it stop working?

Look at this line... it has no closing parenthesis:

    for (; : wink : {} // No continuar, bucle infinito

Your paste without using < CODE > button caused this error... twice.

You called this twice, too.

You did not write this. You copy/pasted it. You need to clean it up and present it again on this same topic.

Hi @klysmgt ,

Welcome to the forum..
lol, you'd made a big mess.. :slight_smile:

add this..

display.setTextColor(SSD1306_WHITE);

inside of displayMenu()..
preferably before you print text..
black on black is hard to see for humans..

good luck.. ~q

Why did you not test each small addition to your code as you went along? Then you would know what and where the failure was at.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.