[SOLUCIONADO] Cambio en el código para cambiar de una pantalla oled 128x64 i2c SSD1306 de 0,96 pulgadas a una pantalla de 1,3 pulgadas

Hola a toda la comunidad.
Estoy realizando un proyecto de un analizador de oxígeno con arduino nano y me funciona correctamente con una pantalla pantalla oled 128x64 i2c SSD1306 de 0,96 pulgadas.
El tema es que la pantalla es bastante pequeña y había pensado en cambiarla por otra pantalla más grande, la pantalla oled 128x64 i2c SSD1106 de 1,3 pulgadas.
Lo que no controlo son los cambios en el código que debo efectuar. Ya he incluido las nuevas librerías para la nueva pantalla y he conseguido el típico mensaje "Hello world" en la pantalla. Lo que no he conseguido es que la nueva pantalla me arroje las mismos datos que me daba la pantalla pequeña. Os paso el código que uso con la pantalla de 0,96 pulgadas por si alguien me puede ayudar con los cambios a realizar en este código.
Muchas gracias por cualquier ayuda

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_ADS1X15.h>
#include <EEPROM.h>
#include <RunningAverage.h>


#define RA_SIZE 20
RunningAverage RA(RA_SIZE);

Adafruit_ADS1015 ads;

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const int buttonPin=2; // push button
const int buzzer = 9; // buzzer
const int ledPin = 13; // led

double calibrationv;
float multiplier;

const int cal_holdTime = 2; // 2 sec button hold to calibration
const int mod_holdTime = 4; // 4 sec hold to po2 mod change
const int max_holdtime = 6; // 6 sec hold to reset max o2 result

long millis_held;    // How long the button was held (milliseconds)
long secs_held;      // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = HIGH;
unsigned long firstTime; // how long since the button was first pressed 
int active = 0;
double result_max = 0;

/*
 Calculate MOD (Maximum Operating Depth)
*/
float max_po1 = 1.30;
const float max_po2 = 1.60;
float cal_mod (float percentage, float ppo2 = 1.4) {
  return 10 * ( (ppo2/(percentage/100)) - 1 );
}

void beep(int x=1) { // make beep for x time
  //digitalWrite(ledPin, HIGH); // led blink disable for battery save
  for(int i=0; i<x; i++) {    
      tone(buzzer, 2800, 7000);
      delay(200);    
  }
  //digitalWrite(ledPin, LOW);
  noTone(buzzer);
}

void read_sensor(int adc=0) {  
  int16_t millivolts = 0;
  millivolts = ads.readADC_Differential_0_1();
  RA.addValue(millivolts);
}

void setup(void) {  

  //Serial.begin(9600);

  /* power saving stuff for battery power */
  // Disable ADC
  // ADCSRA = 0;
  // Disable the analog comparator by setting the ACD bit
  // (bit 7) of the ACSR register to one.
  // ACSR = B10000000;
  // Disable digital input buffers on all analog input pins
  // DIDR0 = DIDR0 | B00111111;

pinMode (7, INPUT);  // Viene del detector de sonido, salida digital
  pinMode (8, OUTPUT);  // Va al conector "sound detector"

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  ads.setGain(GAIN_TWO);
  multiplier = 0.059F; //original multiplier 0,0625
  ads.begin(); // ads1115 start

  pinMode(buttonPin,INPUT_PULLUP);  

  RA.clear();
  for(int cx=0; cx<= RA_SIZE; cx++) {
     read_sensor(0);
  }

  calibrationv = EEPROMReadInt(0);  
  if (calibrationv < 100) {
    calibrationv = calibrate(0);
  }

  beep(1);
}

void EEPROMWriteInt(int p_address, int p_value)
     {
     byte lowByte = ((p_value >> 0) & 0xFF);
     byte highByte = ((p_value >> 8) & 0xFF);

     EEPROM.write(p_address, lowByte);
     EEPROM.write(p_address + 1, highByte);
     }

unsigned int EEPROMReadInt(int p_address)
     {
     byte lowByte = EEPROM.read(p_address);
     byte highByte = EEPROM.read(p_address + 1);

     return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
     }

int calibrate(int x) {

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,0);  
  display.setTextSize(2);
  display.print(F("Calibrando"));
  display.display();

  //RA.clear();
  double result;  
  for(int cx=0; cx<= RA_SIZE; cx++) {
    read_sensor(0);
  }
  result = RA.getAverage();
  result = abs(result);
  EEPROMWriteInt(x, result); // write to eeprom

  beep(1);
  delay(1000);
  active = 0;
  return result;
}

void analysing(int x, int cal) {
  double currentmv=0;
  double result;
  double mv = 0.0;

  read_sensor(0);
  currentmv = RA.getAverage();
  currentmv = abs(currentmv);

  result = (currentmv / cal) * 20.9;
  if (result > 99.9) result = 99.9;
  mv = currentmv * multiplier;                                                                                                                                                                                                                                                            

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,0);

  if (mv < 0.02 || result <= 0) {
     display.setTextSize(2);
     display.println(F("Sensor"));
     display.print(F("Error!"));
  } else {
    display.setTextSize(4);
    display.print(result,1);
    display.println(F("%"));

    if (result >= result_max) {
      result_max = result;
    }

    display.setTextSize(3);
    display.setCursor(0,31);
    display.setTextColor(BLACK, WHITE);    
      
    //display.setCursor(75,31);
    display.print(mv,2);    
    display.print(F("mv"));

    if (active % 4) {
      display.setCursor(115,29);
      display.setTextColor(WHITE);
      display.print(F("."));
    }  

   
    // menu
    if (secs_held < 5 && active > 16) {
      display.setTextSize(2);
      display.setCursor(0,31);
      display.setTextColor(BLACK, WHITE);      
      if (secs_held >= cal_holdTime && secs_held < mod_holdTime) {
        display.print(F("   CAL    "));
      }
      if (secs_held >= mod_holdTime && secs_held < max_holdtime) {
        display.print(F("   PO2    "));
      }
      if (secs_held >= max_holdtime && secs_held < 10) {
        display.print(F("   MAX    "));
      }     
    }  

  }
  display.display();
}

void lock_screen(long pause = 5000) {
  beep(1);
  display.setTextSize(1);
  display.setCursor(0,31);  
  display.setTextColor(0xFFFF, 0);
  display.print(F("                "));
  display.setTextColor(BLACK, WHITE);
  display.setCursor(0,31);
  display.print(F("======= LOCK ======="));
  display.display();
  for (int i = 0; i < pause; ++i) {   
    while (digitalRead(buttonPin) == HIGH) {
      }
   }
   active = 0;
}

void po2_change() {  
  if (max_po1 == 1.3) max_po1 = 1.4;
  else if (max_po1 == 1.4) max_po1 = 1.5;
  else if (max_po1 == 1.5) max_po1 = 1.3;

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,0);  
  display.setTextSize(2);
  display.println(F("pO2 set"));
  display.print(max_po1);
  display.display();
  beep(1);   
  delay(1000);
  active = 0;  
}

void max_clear() {
  result_max = 0;
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,0);  
  display.setTextSize(2);
  display.println(F("Max result"));
  display.print(F("cleared"));
  display.display();
  beep(1);   
  delay(1000);
  active = 0;
}

void loop(void) {

  int current = digitalRead(buttonPin);

  if (current == LOW && previous == HIGH && (millis() - firstTime) > 200) {
    firstTime = millis();
    active = 17;
  }

  millis_held = (millis() - firstTime);
  secs_held = millis_held / 1000;

  if (millis_held > 2) {
    if (current == HIGH && previous == LOW) {
      if (secs_held <= 0) {
        lock_screen();
      }
      if (secs_held >= cal_holdTime && secs_held < mod_holdTime) {        
        calibrationv = calibrate(0);
      }
      if (secs_held >= mod_holdTime && secs_held < max_holdtime) {
        po2_change();
      }
      if (secs_held >= max_holdtime && secs_held < 10) {
        max_clear();
      }
    }
  }

  previous = current;
  prev_secs_held = secs_held;

  analysing(0,calibrationv);
  delay(200);

  active++;
  
  if (digitalRead (7) == HIGH)  {
  digitalWrite (8, HIGH); };  //Activa detector
delay(1000);
{
if (digitalRead (7) == LOW)
{digitalWrite (8,LOW); //Desactiva detector}
}
}
}

Y usas la misma librería

#include <Adafruit_SSD1306.h>

Si la usas no entiendo porque este código no funciona.
Coloca el link del código que probaste y funciona con la SSD1106

Muchísimas gracias por tu rápida respuesta.
A continuación pego el código usado con la pantalla de 1,3 pulgadas. Con este código consigo el mensaje "Hello World", pero no sé cómo hacer para que me dé las lecturas que necesito y que sí me da la pantalla pequeña usando el código anterior.

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_ADS1X15.h>
#include <EEPROM.h>
#include <RunningAverage.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

//Al incluir la librería debemos especificar qué pines estamos usando para SDA y SCL (4 y 5 en Uno).
//También debemos indicar que en este caso no usamos el pin de reset.
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0,4,5,U8X8_PIN_NONE);


#define RA_SIZE 20
RunningAverage RA(RA_SIZE);

Adafruit_ADS1015 ads;

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

const int buttonPin=2; // push button
const int buzzer = 9; // buzzer
const int ledPin = 13; // led

double calibrationv;
float multiplier;

const int cal_holdTime = 2; // 2 sec button hold to calibration
const int mod_holdTime = 4; // 4 sec hold to po2 mod change
const int max_holdtime = 6; // 6 sec hold to reset max o2 result

long millis_held;    // How long the button was held (milliseconds)
long secs_held;      // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = HIGH;
unsigned long firstTime; // how long since the button was first pressed 
int active = 0;
double result_max = 0;

/*
 Calculate MOD (Maximum Operating Depth)
*/
float max_po1 = 1.30;
const float max_po2 = 1.60;
float cal_mod (float percentage, float ppo2 = 1.4) {
  return 10 * ( (ppo2/(percentage/100)) - 1 );
}

void beep(int x=1) { // make beep for x time
  //digitalWrite(ledPin, HIGH); // led blink disable for battery save
  for(int i=0; i<x; i++) {    
      tone(buzzer, 2800, 7000);
      delay(200);    
  }
  //digitalWrite(ledPin, LOW);
  noTone(buzzer);
}

void read_sensor(int adc=0) {  
  int16_t millivolts = 0;
  millivolts = ads.readADC_Differential_0_1();
  RA.addValue(millivolts);
}

void setup(void) {  

  //Serial.begin(9600);

  /* power saving stuff for battery power */
  // Disable ADC
  // ADCSRA = 0;
  // Disable the analog comparator by setting the ACD bit
  // (bit 7) of the ACSR register to one.
  // ACSR = B10000000;
  // Disable digital input buffers on all analog input pins
  // DIDR0 = DIDR0 | B00111111;

  //Aquí establecemos la dirección i2c de nuestra pantalla, que siempre se debe multiplicar por 2.
u8g2_SetI2CAddress(u8g2.getU8g2(), 0x3C * 2);
u8g2.begin();

pinMode (7, INPUT);  // Viene del detector de sonido, salida digital
  pinMode (8, OUTPUT);  // Va al conector "sound detector"

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  ads.setGain(GAIN_TWO);
  multiplier = 0.059F; //original multiplier 0,0625
  ads.begin(); // ads1115 start

  pinMode(buttonPin,INPUT_PULLUP);  

  RA.clear();
  for(int cx=0; cx<= RA_SIZE; cx++) {
     read_sensor(0);
  }

  calibrationv = EEPROMReadInt(0);  
  if (calibrationv < 100) {
    calibrationv = calibrate(0);
  }

  beep(1);
}

void EEPROMWriteInt(int p_address, int p_value)
     {
     byte lowByte = ((p_value >> 0) & 0xFF);
     byte highByte = ((p_value >> 8) & 0xFF);

     EEPROM.write(p_address, lowByte);
     EEPROM.write(p_address + 1, highByte);
     }

unsigned int EEPROMReadInt(int p_address)
     {
     byte lowByte = EEPROM.read(p_address);
     byte highByte = EEPROM.read(p_address + 1);

     return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
     }

int calibrate(int x) {

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(10,10);  
  display.setTextSize(2);
  display.print(F("Calibrando"));
  display.display();

  //RA.clear();
  double result;  
  for(int cx=0; cx<= RA_SIZE; cx++) {
    read_sensor(0);
  }
  result = RA.getAverage();
  result = abs(result);
  EEPROMWriteInt(x, result); // write to eeprom

  beep(1);
  delay(1000);
  active = 0;
  return result;
}

void analysing(int x, int cal) {
  double currentmv=0;
  double result;
  double mv = 0.0;

  read_sensor(0);
  currentmv = RA.getAverage();
  currentmv = abs(currentmv);

  result = (currentmv / cal) * 20.9;
  if (result > 99.9) result = 99.9;
  mv = currentmv * multiplier;                                                                                                                                                                                                                                                            

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(10,10);

  if (mv < 0.02 || result <= 0) {
     display.setTextSize(2);
     display.println(F("Sensor"));
     display.print(F("Error!"));
  } else {
    display.setTextSize(4);
    display.print(result,1);
    display.println(F("%"));

    if (result >= result_max) {
      result_max = result;
    }

    display.setTextSize(3);
    display.setCursor(0,31);
    display.setTextColor(BLACK, WHITE);    
      
    //display.setCursor(75,31);
    display.print(mv,2);    
    display.print(F("mv"));

    if (active % 4) {
      display.setCursor(115,29);
      display.setTextColor(WHITE);
      display.print(F("."));
    }  

   
    // menu
    if (secs_held < 5 && active > 16) {
      display.setTextSize(2);
      display.setCursor(0,31);
      display.setTextColor(BLACK, WHITE);      
      if (secs_held >= cal_holdTime && secs_held < mod_holdTime) {
        display.print(F("   CAL    "));
      }
      if (secs_held >= mod_holdTime && secs_held < max_holdtime) {
        display.print(F("   PO2    "));
      }
      if (secs_held >= max_holdtime && secs_held < 10) {
        display.print(F("   MAX    "));
      }     
    }  

  }
  display.display();
}

void lock_screen(long pause = 5000) {
  beep(1);
  display.setTextSize(1);
  display.setCursor(0,31);  
  display.setTextColor(0xFFFF, 0);
  display.print(F("                "));
  display.setTextColor(BLACK, WHITE);
  display.setCursor(0,31);
  display.print(F("======= LOCK ======="));
  display.display();
  for (int i = 0; i < pause; ++i) {   
    while (digitalRead(buttonPin) == HIGH) {
      }
   }
   active = 0;
}

void po2_change() {  
  if (max_po1 == 1.3) max_po1 = 1.4;
  else if (max_po1 == 1.4) max_po1 = 1.5;
  else if (max_po1 == 1.5) max_po1 = 1.3;

  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(10,10);  
  display.setTextSize(2);
  display.println(F("pO2 set"));
  display.print(max_po1);
  display.display();
  beep(1);   
  delay(1000);
  active = 0;  
}

void max_clear() {
  result_max = 0;
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(10,10);  
  display.setTextSize(2);
  display.println(F("Max result"));
  display.print(F("cleared"));
  display.display();
  beep(1);   
  delay(1000);
  active = 0;
}

void loop(void) {

 int current = digitalRead(buttonPin);
  
u8g2.firstPage();
do {
//En este do while podemos dibujar o escribir lo que queramos mostrar en pantalla.
//Como ejemplo un Hello World.
u8g2.setFont(u8g2_font_ncenB08_tr);
u8g2.drawStr(10,10,"Hello World");
} while ( u8g2.nextPage() );
delay(1000);


  if (current == LOW && previous == HIGH && (millis() - firstTime) > 200) {
    firstTime = millis();
    active = 17;
  }

  millis_held = (millis() - firstTime);
  secs_held = millis_held / 1000;

  if (millis_held > 2) {
    if (current == HIGH && previous == LOW) {
      if (secs_held <= 0) {
        lock_screen();
      }
      if (secs_held >= cal_holdTime && secs_held < mod_holdTime) {        
        calibrationv = calibrate(0);
      }
      if (secs_held >= mod_holdTime && secs_held < max_holdtime) {
        po2_change();
      }
      if (secs_held >= max_holdtime && secs_held < 10) {
        max_clear();
      }
    }
  }

  previous = current;
  prev_secs_held = secs_held;

  analysing(0,calibrationv);
  delay(200);

  active++;
  
  if (digitalRead (7) == HIGH)  {
  digitalWrite (8, HIGH); };  //Activa detector
delay(1000);
{
if (digitalRead (7) == LOW)
{digitalWrite (8,LOW); //Desactiva detector}
}
}
}

Sigues intentando usar la librería para la pantalla con SSD1306 que ya no está.

La librería U8g2lib es la que debe quedar, elimina o comenta las líneas

#include <Adafruit_SSD1306.h>

y

Adafruit_SSD1306 display(OLED_RESET);

Luego cambia todas las ocurrencias de
display. por u8g2.

Seguramente haya algunas funciones que no existan o no tengan el mismo nombre en la librería U8g2lib y te generen error de compilación, busca sus equivalentes en la wiki de la librería, es muy completa la información.

Muchísimas gracias por tu respuesta.
Voy a implementarlo y, en cuanto lo tenga, te comento resultados.

Hola de nuevo MaximoEsfuerzo.
Muchas gracias por tus consejos Ya he hecho los cambios que me has indicado y empiezo a ver algo de luz :slight_smile:
El problema que me queda por resolver creo que va de las posiciones en la pantalla. Me explico... de las dos lecturas que me debería dar la pantalla sólo aparece la mitad de la lectura que debería aparecer ubicada arriba. Ésta aparece en la parte superior de la pantalla grande.
Para que resulte más gráfico adjunto una foto con la pantalla pequeña donde aparece lo que se debería leer y una de la pantalla grande.
No sé si la solución podría ser ir probando con las posiciones de setcursor. Pero es que si pongo (0,0) no me aparece nada, si pongo (10,10) me aparece lo de la foto y si pongo posiciones más allá de 10,10 no me aparece tampoco ninguna lectura.
Pego también el código que me ha quedado con la librería u8g2.

¿Alguna idea de cómo resolver esto?

Muchas gracias


#include <SPI.h>
#include <Wire.h>
// #include <Adafruit_SSD1306.h>
#include <Adafruit_ADS1X15.h>
#include <EEPROM.h>
#include <RunningAverage.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

//Al incluir la librería debemos especificar qué pines estamos usando para SDA y SCL (4 y 5 en Uno).
//También debemos indicar que en este caso no usamos el pin de reset.
//U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0,4,5,U8X8_PIN_NONE);
U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);


#define RA_SIZE 20
RunningAverage RA(RA_SIZE);

Adafruit_ADS1015 ads;

#define OLED_RESET 4
// Adafruit_SSD1306 display(OLED_RESET);

const int buttonPin=2; // push button
const int buzzer = 9; // buzzer
const int ledPin = 13; // led

double calibrationv;
float multiplier;

const int cal_holdTime = 2; // 2 sec button hold to calibration
const int mod_holdTime = 4; // 4 sec hold to po2 mod change
const int max_holdtime = 6; // 6 sec hold to reset max o2 result

long millis_held;    // How long the button was held (milliseconds)
long secs_held;      // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = HIGH;
unsigned long firstTime; // how long since the button was first pressed 
int active = 0;
double result_max = 0;

/*
 Calculate MOD (Maximum Operating Depth)
*/
float max_po1 = 1.30;
const float max_po2 = 1.60;
float cal_mod (float percentage, float ppo2 = 1.4) {
  return 10 * ( (ppo2/(percentage/100)) - 1 );
}

void beep(int x=1) { // make beep for x time
  //digitalWrite(ledPin, HIGH); // led blink disable for battery save
  for(int i=0; i<x; i++) {    
      tone(buzzer, 2800, 7000);
      delay(200);    
  }
  //digitalWrite(ledPin, LOW);
  noTone(buzzer);
}

void read_sensor(int adc=0) {  
  int16_t millivolts = 0;
  millivolts = ads.readADC_Differential_0_1();
  RA.addValue(millivolts);
}

void setup(void) {  

  //Serial.begin(9600);

  /* power saving stuff for battery power */
  // Disable ADC
  // ADCSRA = 0;
  // Disable the analog comparator by setting the ACD bit
  // (bit 7) of the ACSR register to one.
  // ACSR = B10000000;
  // Disable digital input buffers on all analog input pins
  // DIDR0 = DIDR0 | B00111111;

  //Aquí establecemos la dirección i2c de nuestra pantalla, que siempre se debe multiplicar por 2.
u8g2_SetI2CAddress(u8g2.getU8g2(), 0x3C * 2);
u8g2.begin();

pinMode (7, INPUT);  // Viene del detector de sonido, salida digital
  pinMode (8, OUTPUT);  // Va al conector "sound detector"

  //display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  ads.setGain(GAIN_TWO);
  multiplier = 0.059F; //original multiplier 0,0625
  ads.begin(); // ads1115 start

  pinMode(buttonPin,INPUT_PULLUP);  

  RA.clear();
  for(int cx=0; cx<= RA_SIZE; cx++) {
     read_sensor(0);
  }

  calibrationv = EEPROMReadInt(0);  
  if (calibrationv < 100) {
    calibrationv = calibrate(0);
  }

  beep(1);
}

void EEPROMWriteInt(int p_address, int p_value)
     {
     byte lowByte = ((p_value >> 0) & 0xFF);
     byte highByte = ((p_value >> 8) & 0xFF);

     EEPROM.write(p_address, lowByte);
     EEPROM.write(p_address + 1, highByte);
     }

unsigned int EEPROMReadInt(int p_address)
     {
     byte lowByte = EEPROM.read(p_address);
     byte highByte = EEPROM.read(p_address + 1);

     return ((lowByte << 0) & 0xFF) + ((highByte << 8) & 0xFF00);
     }

int calibrate(int x) {

  u8g2.clearDisplay();
  //u8g2.setTextColor(WHITE);
    u8g2.setDrawColor(1);

  
  

  u8g2.setCursor(5,5);  
u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.print(F("Calibrando"));
  u8g2.display();

  //RA.clear();
  double result;  
  for(int cx=0; cx<= RA_SIZE; cx++) {
    read_sensor(0);
  }
  result = RA.getAverage();
  result = abs(result);
  EEPROMWriteInt(x, result); // write to eeprom

  beep(1);
  delay(1000);
  active = 0;
  return result;
}

void analysing(int x, int cal) {
  double currentmv=0;
  double result;
  double mv = 0.0;

  read_sensor(0);
  currentmv = RA.getAverage();
  currentmv = abs(currentmv);

  result = (currentmv / cal) * 20.9;
  if (result > 99.9) result = 99.9;
  mv = currentmv * multiplier;                                                                                                                                                                                                                                                            

  u8g2.clearDisplay();
 u8g2.setDrawColor(1);
  u8g2.setCursor(5,5);

  if (mv < 0.02 || result <= 0) {
     u8g2.setFont(u8g2_font_6x10_tf);
     u8g2.println(F("Sensor"));
     u8g2.print(F("Error!"));
  } else {
    u8g2.setFont(u8g2_font_6x10_tf);
    u8g2.print(result,1);
    u8g2.println(F("%"));

    if (result >= result_max) {
      result_max = result;
    }

   u8g2.setFont(u8g2_font_6x10_tf);
    u8g2.setCursor(0,31);
  u8g2.setDrawColor(1);
  u8g2.setDrawColor(1);
   
      
    //display.setCursor(75,31);
    u8g2.print(mv,2);    
    u8g2.print(F("mv"));
    if (active % 4) {
      //u8g2.setCursor(115,29);
      u8g2.setCursor(115,29);
      u8g2.setDrawColor(1);
      u8g2.print(F("."));
    }  

   
    // menu
    if (secs_held < 5 && active > 16) {
      u8g2.setFont(u8g2_font_6x10_tf);
      u8g2.setCursor(0,31);
     u8g2.setDrawColor(1);
  u8g2.setDrawColor(1);    
      if (secs_held >= cal_holdTime && secs_held < mod_holdTime) {
        u8g2.print(F("   CAL    "));
      }
      if (secs_held >= mod_holdTime && secs_held < max_holdtime) {
        u8g2.print(F("   PO2    "));
      }
      if (secs_held >= max_holdtime && secs_held < 10) {
        u8g2.print(F("   MAX    "));
      }     
    }  

  }
  u8g2.display();
}

void lock_screen(long pause = 5000) {
  beep(1);
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setCursor(0,31);  
 u8g2.setDrawColor(1);
  u8g2.setDrawColor(1);
  u8g2.print(F("                "));
  u8g2.setDrawColor(1);
  u8g2.setDrawColor(1);
  u8g2.setCursor(0,31);
  u8g2.print(F("======= LOCK ======="));
  u8g2.display();
  for (int i = 0; i < pause; ++i) {   
    while (digitalRead(buttonPin) == HIGH) {
      }
   }
   active = 0;
}

void po2_change() {  
  if (max_po1 == 1.3) max_po1 = 1.4;
  else if (max_po1 == 1.4) max_po1 = 1.5;
  else if (max_po1 == 1.5) max_po1 = 1.3;

  u8g2.clearDisplay();
  u8g2.setDrawColor(1);
  u8g2.setCursor(5,5);  
  u8g2.setFont(u8g2_font_6x10_tf); 
  u8g2.println(F("pO2 set"));
  u8g2.print(max_po1);
  u8g2.display();
  beep(1);   
  delay(1000);
  active = 0;  
}

void max_clear() {
  result_max = 0;
  u8g2.clearDisplay();
  u8g2.setDrawColor(1);
  u8g2.setCursor(5,5);  
  u8g2.setFont(u8g2_font_6x10_tf); 
  u8g2.println(F("Max result"));
  u8g2.print(F("cleared"));
  u8g2.display();
  beep(1);   
  delay(1000);
  active = 0;
}

void loop(void) {

 int current = digitalRead(buttonPin);
  
//u8g2.firstPage();
do {
//En este do while podemos dibujar o escribir lo que queramos mostrar en pantalla.
//Como ejemplo un Hello World.
//u8g2.setFont(u8g2_font_ncenB08_tr);
//u8g2.drawStr(10,10,"Hello World");
} while ( u8g2.nextPage() );



  if (current == LOW && previous == HIGH && (millis() - firstTime) > 200) {
    firstTime = millis();
    active = 17;
  }

  millis_held = (millis() - firstTime);
  secs_held = millis_held / 1000;

  if (millis_held > 2) {
    if (current == HIGH && previous == LOW) {
      if (secs_held <= 0) {
        lock_screen();
      }
      if (secs_held >= cal_holdTime && secs_held < mod_holdTime) {        
        calibrationv = calibrate(0);
      }
      if (secs_held >= mod_holdTime && secs_held < max_holdtime) {
        po2_change();
      }
      if (secs_held >= max_holdtime && secs_held < 10) {
        max_clear();
      }
    }
  }

  previous = current;
  prev_secs_held = secs_held;

  analysing(0,calibrationv);
  delay(200);
while ( u8g2.nextPage() );
  active++;
  
  if (digitalRead (7) == HIGH)  {
  digitalWrite (8, HIGH); };  //Activa detector
delay(1000);
{
if (digitalRead (7) == LOW)
{digitalWrite (8,LOW); //Desactiva detector}
}
}
}

La posición del cursor indica donde se ubica el vértice inferior izquierdo del primer caracter a imprimir, por eso si indicas 0,0 no ves nada porque el texto estaría "por encima" de la pantalla.

Por lo que se ve en la foto, es probable que haya que definir un offset para "centrar" la pantalla.

Revisa la documentación de la librería:

Muchas gracias por tu respuesta.
He estado investigando y lo único que he encontrado que me ha podido ayudar en cierto modo es la variable u8g2.setCursor(x,y).

Centrándome en una sola variable, en este caso, la frase "SensorError", he estado haciendo pruebas de ensayo y error con dicha función y con diferentes tipos de fuente de la librería.
En general trabajando con valores de u8g2.setCursor(x,y) entre 1 y 9 he conseguido que casi todas las letras sean visibles, excepto la parte superior, que aparece cortada en mayor o menor medida.
Por encima de valores de 9 de setcursor ya no aparece nada en la pantalla y, en algunos casos, aparece la frase en la parte inferior de la pantalla y además, invertida (lo de abajo arriba).
La verdad es que ya no sé por donde tirar...
Alguna sugerencia?
Muchas gracias por la atención.

No he trabajado con esta librería ni pantalla pero lo que he hecho en su momento con otras, como para "amigarme" con ambas cosas, fue dibujar puntos en diferentes coordenadas (principalmente las esquinas) para ver como se comportaba. Luego hacer lo mismo con el texto. Por supuesto estudiar los ejemplos y leer la documentación. Finalmente insistir e insistir hasta que salió funcionando.

Fijate que la librería no es sencilla, tiene una gran cantidad de funciones, algunas que te ayudan a entender como va la cosa. Por ej. hay un par que te devuelven, una, la altura de un caracter desde la línea base (digamos, el renglón) y la otra, cuanto "baja" el caracter desde esa línea imaginaria (por ej. en el caso de la letra "g")
Es muy completa, solo hay que aprender a usarla para poder aprovecharla.

Incluso fijate si no te es más útil la U8x8, si solo vas a mostrar texto.

Más no puedo aportar.

Muchas gracias de nuevo,
Probaré con la librería que me comentas. No podré hasta dentro de unos días pero en cuanto tenga resultados te cuento.
Un afectuoso saludo

Muchas gracias por tu inestimable ayuda y disculpa el tiempo que he tardado en responder. Hasta ahora no he podido retomar este tema.
Finalmente he usado la librería U8glib. Con esta librería y unos cuantos aportes de ChatGPT ha ido de maravilla.
Un saludo a tod@s

Que te haya ido de maravilla con ChatGPT ya es todo un logro.

Saludos

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