Problem code with screen and leds and buttons

Hello, I'm trying to create a game with push-button leds and a screen. The goal is to light 9 leds with 5 buttons which then gives a password on the screen. All my code works except that I can't get the screen to light up when all 9 leds are on. Does anyone have any idea how to fix this problem? It would be great.

I am a beginner so the code must not be super pretty to see.

My code:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
 
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library. 
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 
#define NUMFLAKES     10 // Number of snowflakes in the animation example
 
#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000 };



boolean button1WasUp = true;
boolean button2WasUp = true;
boolean button3WasUp = true;
boolean button4WasUp = true;
boolean button5WasUp = true;


void setup() {
  
    Serial.begin(9600);
 
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever

  
  }

   pinMode(13, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);
   digitalWrite(13, LOW);
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
   digitalWrite(6, LOW);
   digitalWrite(7, LOW);
   digitalWrite(8, LOW);
   digitalWrite(9, LOW);
   pinMode(0, INPUT_PULLUP);
   pinMode(10, INPUT_PULLUP);
   pinMode(11, INPUT_PULLUP);
   pinMode(12, INPUT_PULLUP);
   pinMode(1, INPUT_PULLUP);

  
}
 
      
void loop() {
   boolean button1IsUp = digitalRead(0);
   boolean button2IsUp = digitalRead(10);
   boolean button3IsUp = digitalRead(11);
   boolean button4IsUp = digitalRead(12);
   boolean button5IsUp = digitalRead(1);
   boolean led1On = digitalRead(13);
   boolean led2On = digitalRead(2);
   boolean led3On = digitalRead(3);
   boolean led4On = digitalRead(4);
   boolean led5On = digitalRead(5);
   boolean led6On = digitalRead(6);
   boolean led7On = digitalRead(7);
   boolean led8On = digitalRead(8);
   boolean led9On = digitalRead(9);
   
  
   if (button1WasUp && !button1IsUp) {
      delay(10);
      button1IsUp = digitalRead(0);
      if (!button1IsUp) { digitalWrite(13, LOW); digitalWrite(2, LOW); digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(9, HIGH);  }
   }
   if (button2WasUp && !button2IsUp) {
      delay(10);
      button2IsUp = digitalRead(10);
      if (!button2IsUp) { digitalWrite(2, HIGH); digitalWrite(3,LOW); digitalWrite(4, HIGH); digitalWrite(9, LOW); }
   }
   if (button3WasUp && !button3IsUp) {
      delay(10);
      button3IsUp = digitalRead(11);
      if (!button3IsUp) { digitalWrite(13, HIGH); digitalWrite(3, HIGH); digitalWrite(5, HIGH); digitalWrite(7, HIGH); digitalWrite(9, HIGH); }
   }
   if (button4WasUp && !button4IsUp) {
      delay(10);
      button4IsUp = digitalRead(12);
      if (!button4IsUp) { digitalWrite(13, LOW); digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(5, LOW); digitalWrite(7, LOW); digitalWrite(8, HIGH); digitalWrite(9, LOW); }
   }
   if (button5WasUp && !button5IsUp) {
      delay(10);
      button5IsUp = digitalRead(1);
      if (!button5IsUp) { digitalWrite(13, LOW); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(5, HIGH); digitalWrite(7, LOW); digitalWrite(8, LOW);digitalWrite(9, LOW);  }
    }
// code for the screen to light up with the nine leds on but which does not work...

    if (led1On && led2On && led3On && led4On && led5On && led6On && led7On && led8On && led9On) {
      PrintText();
    }
 



  button1WasUp = button1IsUp;
  button2WasUp = button2IsUp;
  button3WasUp = button3IsUp;
  button4WasUp = button4IsUp;
  button5WasUp = button5IsUp;
}
// code for the screen to display the passwords
 void PrintText() {
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(0, 10);
      display.print("Code Bleu : 19837");
      display.display();

     }

Well, you could use analog pins A0 and A1 for the buttons instead of digial pins 0 and 1 which are, anyway, usually reserved for serial activity.
That way you could add Serial.print type debug messages to see how far the code has progressed.
Most likely one of these values is false

if (led1On && led2On && led3On && led4On && led5On && led6On && led7On && led8On && led9On) {
      PrintText();
    }

Alternatively, hack the code to force a call of PrintText() to check that the screen actually works.

1 Like

not forget to change 0 and 1 pins

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000
};



boolean button1WasUp = true;
boolean button2WasUp = true;
boolean button3WasUp = true;
boolean button4WasUp = true;
boolean button5WasUp = true;


void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever


  }

  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  pinMode(0, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);


}


void loop() {
  boolean button1IsUp = digitalRead(0);
  boolean button2IsUp = digitalRead(10);
  boolean button3IsUp = digitalRead(11);
  boolean button4IsUp = digitalRead(12);
  boolean button5IsUp = digitalRead(1);
  boolean led1On = digitalRead(13);
  boolean led2On = digitalRead(2);
  boolean led3On = digitalRead(3);
  boolean led4On = digitalRead(4);
  boolean led5On = digitalRead(5);
  boolean led6On = digitalRead(6);
  boolean led7On = digitalRead(7);
  boolean led8On = digitalRead(8);
  boolean led9On = digitalRead(9);


  if (button1WasUp && !button1IsUp) {
    delay(10);
    button1IsUp = digitalRead(0);
    if (!button1IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button2WasUp && !button2IsUp) {
    delay(10);
    button2IsUp = digitalRead(10);
    if (!button2IsUp) {
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button3WasUp && !button3IsUp) {
    delay(10);
    button3IsUp = digitalRead(11);
    if (!button3IsUp) {
      digitalWrite(13, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button4WasUp && !button4IsUp) {
    delay(10);
    button4IsUp = digitalRead(12);
    if (!button4IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button5WasUp && !button5IsUp) {
    delay(10);
    button5IsUp = digitalRead(1);
    if (!button5IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }
  }
  // code for the screen to light up with the nine leds on but which does not work...
  if (led1On && led2On && led3On && led4On && led5On && led6On && led7On && led8On && led9On) {
    PrintText();
  }
  button1WasUp = button1IsUp;
  button2WasUp = button2IsUp;
  button3WasUp = button3IsUp;
  button4WasUp = button4IsUp;
  button5WasUp = button5IsUp;
}
// code for the screen to display the passwords
void PrintText() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.print("Code Bleu : 19837");
  display.display();
}
1 Like

However I have already tested the code on my arduino and everything works, the leds and the screen. Except that the screen lights up from the start and not after the nine leds are lit.

Can you explain to me why I had to change them

answer:

you allow to use A0,A1,A2,A3 as digital I/O, (A4/A5 are used by SSD1306 I2C connection)

so in conclusion I have to change pins 0 and 1 and put them in A0 and A1.

or don't use Serial. comment out Serial.begin(9600);

and do you know how to make the screen light up after all nine leds are on

What is the status now? Is the screen still lit up from the start or what ?

Light up? this is OLED display, it has no backlight. or do you mean whole screen should fill white pixels?

I changed as you advised me the pins of the buttons on A0 and A1 but now they no longer work. Do I need to change a few other things to make it work like digitalWrite?

thanks for the help

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000
};



boolean button1WasUp = true;
boolean button2WasUp = true;
boolean button3WasUp = true;
boolean button4WasUp = true;
boolean button5WasUp = true;


void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever


  }

  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  digitalWrite(13, LOW);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  pinMode(A0, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);


}


void loop() {
  boolean button1IsUp = digitalRead(A0);
  boolean button2IsUp = digitalRead(10);
  boolean button3IsUp = digitalRead(11);
  boolean button4IsUp = digitalRead(12);
  boolean button5IsUp = digitalRead(A1);
  boolean led1On = digitalRead(13);
  boolean led2On = digitalRead(2);
  boolean led3On = digitalRead(3);
  boolean led4On = digitalRead(4);
  boolean led5On = digitalRead(5);
  boolean led6On = digitalRead(6);
  boolean led7On = digitalRead(7);
  boolean led8On = digitalRead(8);
  boolean led9On = digitalRead(9);


  if (button1WasUp && !button1IsUp) {
    delay(10);
    button1IsUp = digitalRead(A0);
    if (!button1IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button2WasUp && !button2IsUp) {
    delay(10);
    button2IsUp = digitalRead(10);
    if (!button2IsUp) {
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button3WasUp && !button3IsUp) {
    delay(10);
    button3IsUp = digitalRead(11);
    if (!button3IsUp) {
      digitalWrite(13, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button4WasUp && !button4IsUp) {
    delay(10);
    button4IsUp = digitalRead(12);
    if (!button4IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button5WasUp && !button5IsUp) {
    delay(10);
    button5IsUp = digitalRead(A1);
    if (!button5IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }
  }
  // code for the screen to light up with the nine leds on but which does not work...
  if (led1On && led2On && led3On && led4On && led5On && led6On && led7On && led8On && led9On) {
    PrintText();
  }
  button1WasUp = button1IsUp;
  button2WasUp = button2IsUp;
  button3WasUp = button3IsUp;
  button4WasUp = button4IsUp;
  button5WasUp = button5IsUp;
}
// code for the screen to display the passwords
void PrintText() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.print("Code Bleu : 19837");
  display.display();
}

no that's not it the screen display works it displays the code, it's just when it should light up that I can't adjust. My goal is that when the nine leds are lit after pressing the buttons, the screen lights up and displays a code

To me it looks like you have successfully updated the sketch to use A0 instead of digital pin 0 and similarly updated it to use pin A1 instead of digital pin 1. Therefore I cannot understand why, if you have also made the corresponding hardware changes, the behaviour has otherwise changed.
Pin A0 is your button 1 and pin A1 is your button 5.

1 Like

try

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000
};

boolean button1WasUp = true;
boolean button2WasUp = true;
boolean button3WasUp = true;
boolean button4WasUp = true;
boolean button5WasUp = true;


void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while(true); // Don't proceed, loop forever
  }

  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  
  pinMode(A0, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
}


void loop() {
  boolean button1IsUp = digitalRead(A0)==HIGH;
  boolean button2IsUp = digitalRead(10)==HIGH;
  boolean button3IsUp = digitalRead(11)==HIGH;
  boolean button4IsUp = digitalRead(12)==HIGH;
  boolean button5IsUp = digitalRead(A1)==HIGH;

  if (button1WasUp && !button1IsUp) {
    delay(10);
    button1IsUp = digitalRead(A0);
    if (!button1IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button2WasUp && !button2IsUp) {
    delay(10);
    button2IsUp = digitalRead(10);
    if (!button2IsUp) {
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button3WasUp && !button3IsUp) {
    delay(10);
    button3IsUp = digitalRead(11);
    if (!button3IsUp) {
      digitalWrite(13, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button4WasUp && !button4IsUp) {
    delay(10);
    button4IsUp = digitalRead(12);
    if (!button4IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button5WasUp && !button5IsUp) {
    delay(10);
    button5IsUp = digitalRead(A1);
    if (!button5IsUp) {
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }
  }
  boolean led1On = digitalRead(13);
  boolean led2On = digitalRead(2);
  boolean led3On = digitalRead(3);
  boolean led4On = digitalRead(4);
  boolean led5On = digitalRead(5);
  boolean led6On = digitalRead(6);
  boolean led7On = digitalRead(7);
  boolean led8On = digitalRead(8);
  boolean led9On = digitalRead(9);
  
  // code for the screen to light up with the nine leds on but which does not work...
  if (led1On && led2On && led3On && led4On && led5On && led6On && led7On && led8On && led9On) {
    PrintText();
    
  }
  button1WasUp = button1IsUp;
  button2WasUp = button2IsUp;
  button3WasUp = button3IsUp;
  button4WasUp = button4IsUp;
  button5WasUp = button5IsUp;
}
// code for the screen to display the passwords
void PrintText() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.print("Code Bleu : 19837");
  display.display();
}

yes indeed it's weird I try a bit of everything but the two buttons don't work

I tried but still the same problem the two buttons do not work

then it is not display problem but your code is not allow to secure manage the button states, without any unwanted behavior.

look Serial monitor for button presses.

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000
};

boolean button1WasUp = true;
boolean button2WasUp = true;
boolean button3WasUp = true;
boolean button4WasUp = true;
boolean button5WasUp = true;


void setup() {

  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true); // Don't proceed, loop forever
  }

  pinMode(13, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  pinMode(A0, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(A1, INPUT_PULLUP);
}


void loop() {
  boolean button1IsUp = digitalRead(A0) == HIGH;
  boolean button2IsUp = digitalRead(10) == HIGH;
  boolean button3IsUp = digitalRead(11) == HIGH;
  boolean button4IsUp = digitalRead(12) == HIGH;
  boolean button5IsUp = digitalRead(A1) == HIGH;
  Serial.println(button1IsUp);
  Serial.println(button2IsUp);
  Serial.println(button3IsUp);
  Serial.println(button4IsUp);
  Serial.println(button5IsUp);

  if (button1WasUp && !button1IsUp) {
    delay(20);
   
    if (!button1IsUp) {
      Serial.println("button1");
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button2WasUp && !button2IsUp) {
    delay(20);
    
    if (!button2IsUp) {
      Serial.println("button2");
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(4, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button3WasUp && !button3IsUp) {
    delay(20);
    
    if (!button3IsUp) {
      Serial.println("button3");
      digitalWrite(13, HIGH);
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(7, HIGH);
      digitalWrite(9, HIGH);
    }
  }
  if (button4WasUp && !button4IsUp) {
    delay(20);
  
    if (!button4IsUp) {
      Serial.println("button4");
      digitalWrite(13, LOW);
      digitalWrite(2, HIGH);
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(7, LOW);
      digitalWrite(8, HIGH);
      digitalWrite(9, LOW);
    }
  }
  if (button5WasUp && !button5IsUp) {
    delay(20);

    if (!button5IsUp) {
      Serial.println("button5");
      digitalWrite(13, LOW);
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      digitalWrite(5, HIGH);
      digitalWrite(7, LOW);
      digitalWrite(8, LOW);
      digitalWrite(9, LOW);
    }
  }
  boolean led1On = digitalRead(13);
  boolean led2On = digitalRead(2);
  boolean led3On = digitalRead(3);
  boolean led4On = digitalRead(4);
  boolean led5On = digitalRead(5);
  boolean led6On = digitalRead(6);
  boolean led7On = digitalRead(7);
  boolean led8On = digitalRead(8);
  boolean led9On = digitalRead(9);

  // code for the screen to light up with the nine leds on but which does not work...
  if (led1On && led2On && led3On && led4On && led5On && led6On && led7On && led8On && led9On) {
    PrintText();

  }
  button1WasUp = button1IsUp;
  button2WasUp = button2IsUp;
  button3WasUp = button3IsUp;
  button4WasUp = button4IsUp;
  button5WasUp = button5IsUp;
}
// code for the screen to display the passwords
void PrintText() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.print("Code Bleu : 19837");
  display.display();
}
1 Like

yes thank you thanks to that the buttons work again. The problem is that the screen always stays on from the start.

use to make screen empty

display.clearDisplay();