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();
}