Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Lois
Published © CERN-OHL2

Counter by OLED Display

The initial value is 00, press switch 1, add 1 to the tens, press switch 2, add 1 to the units.

BeginnerProtip2 hours117

Things used in this project

Story

Read more

Schematics

Counter by OLED Display

The initial value is 00, press switch 1, add 1 to the tens, press switch 2, add 1 to the units

Code

Counter by OLED Display

Arduino
The initial value is 00, press switch 1, add 1 to the tens, press switch 2, add 1 to the units
#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

#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int switch1Pin = 2; // Pin for switch 1
const int switch2Pin = 3; // Pin for switch 2

int tens = 0; // Tens place digit
int units = 0; // Units place digit

void setup() {
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // 0x3C is the I2C address
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}

display.clearDisplay();
updateDisplay();
}

void loop() {
// Check first switch
if (digitalRead(switch1Pin) == LOW) {
delay(50);
if (digitalRead(switch1Pin) == LOW) {
tens = (tens + 1) % 10;
updateDisplay();
while (digitalRead(switch1Pin) == LOW); // Wait for release
}
}

// Check second switch
if (digitalRead(switch2Pin) == LOW) {
delay(50);
if (digitalRead(switch2Pin) == LOW) {
units = (units + 1) % 10;
updateDisplay();
while (digitalRead(switch2Pin) == LOW); // Wait for release
}
}
}

void updateDisplay() {
display.clearDisplay();
display.setTextSize(2); // Size 2 Text
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print(tens);
display.print(units);
display.display();
}

Credits

Lois
13 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.