/* Multiple SSD1327 1.5 Inch Oled with Arduino Mega
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include <U8g2lib.h> // U8g2 Library for Oled https://github.com/olikraus/u8g2
#define switch_pin1 2 // Tact Switch for 1st OLED connected to pin 8
#define switch_pin2 3 // Tact Switch for 2nd OLED connected to pin 9
int counter1 = 0; // Score counter for Player 1
int counter2 = 0; // Score counter for Player 2
int center1 = 47; // Used to Center value for counter display on OLED 1
int center2 = 47; // Used to Center value for counter display on OLED 2
// OLED 1 init
U8G2_SSD1327_MIDAS_128X128_F_4W_HW_SPI OLED1(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
// OLED 2 init
U8G2_SSD1327_MIDAS_128X128_F_4W_HW_SPI OLED2(U8G2_R0, /* cs=*/ 11, /* dc=*/ 9, /* reset=*/ 7);
void setup(void) {
pinMode(switch_pin1, INPUT_PULLUP); // using Input_Pullup resistor of the Arduino
pinMode(switch_pin2, INPUT_PULLUP); // using Input_Pullup resistor of the Arduino
OLED1.begin(); // Start OLED 1
OLED1.setContrast(200); // Brightness setting from 0 to 255
OLED2.begin(); // Start OLED 2
OLED2.setContrast(50); // Brightness setting from 0 to 255
// Choose small font for Player 1 text display
OLED1.setFont(u8g2_font_fub17_tr);
OLED1.clearBuffer(); // Clear OLED 1
OLED1.setCursor(10, 32);
OLED1.print("PLAYER 1");
OLED1.drawRFrame(0, 7, 128, 32, 7); // Draw frame around Player 1 text
OLED1.setFont(u8g2_font_fub42_tr); // Choose bigger font for score display
OLED1.setCursor(center1, 117);
OLED1.print(counter1); // Display counter 1 value
OLED1.drawRFrame(0, 65, 128, 63, 7);
OLED1.sendBuffer(); // Send to OLED 1
// Same for OLED 2
OLED2.setFont(u8g2_font_fub17_tr);
OLED2.clearBuffer();
OLED2.setCursor(10, 32);
OLED2.print("PLAYER 2");
OLED2.drawRFrame(0, 7, 128, 32, 7);
OLED2.setFont(u8g2_font_fub42_tr);
OLED2.setCursor(center2, 117);
OLED2.print(counter2);
OLED2.drawRFrame(0, 65, 128, 63, 7);
OLED2.sendBuffer();
}
void loop() {
// If tact switch 1 is clicked
if (digitalRead(switch_pin1) == LOW) {
counter1++; // increase counter
switch (counter1) {
case 0 ... 9: // if counter < 10
center1 = 47; // used to center score in OLED display
break;
case 10 ... 99: // if counter between 10 and 99
center1 = 32;
break;
case 100 ... 999: // if counter > 100
center1 = 12;
break;
}
OLED1.clearBuffer();
OLED1.setFont(u8g2_font_fub17_tr);
OLED1.setCursor(10, 32);
OLED1.print("PLAYER 1");
OLED1.drawRFrame(0, 7, 128, 32, 7);
OLED1.setFont(u8g2_font_fub42_tr);
OLED1.setCursor(center1, 117);
OLED1.print(counter1);
OLED1.drawRFrame(0, 65, 128, 63, 7);
OLED1.sendBuffer();
delay(10);
}
if (digitalRead(switch_pin2) == LOW) {
counter2++;
switch (counter2) {
case 0 ... 9:
center2 = 47;
break;
case 10 ... 99:
center2 = 32;
break;
case 100 ... 999:
center2 = 12;
break;
}
OLED2.clearBuffer();
OLED2.setCursor(10, 32);
OLED2.setFont(u8g2_font_fub17_tr);
OLED2.setFontMode(0);
OLED2.setDrawColor(1);
OLED2.print("PLAYER 2");
OLED2.drawRFrame(0, 7, 128, 32, 7);
OLED2.setFont(u8g2_font_fub42_tr);
OLED2.setCursor(center2, 117);
OLED2.print(counter2);
OLED2.drawRFrame(0, 65, 128, 63, 7);
OLED2.sendBuffer();
delay(10);
}
}