Butto count OLED

I was wondering if anyone could help me fix this code i'm new to coding and have no clue what to do. Basically i'm trying to make a button count to oled display but the code i found uses code for a SSD1306 OLED display and I have a SH1106 OLED display so i'm wondering if anyone could change the code so it works for SH1106.

Thank you

The code:

/*

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
ezButton button(7); // create ezButton object that attach to pin 7;
unsigned long lastCount = 0;

void setup() {
Serial.begin(9600);

// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}

delay(2000); // wait for initializing
oled.clearDisplay(); // clear display

oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display

button.setDebounceTime(50); // set debounce time to 50 milliseconds
button.setCountMode(COUNT_FALLING);
}

void loop() {
button.loop(); // MUST call the loop() function first

unsigned long count = button.getCount();
if (lastCount != count) {
Serial.println(count); // print count to Serial Monitor
oled.clearDisplay(); // clear display
oled.println(count); // display count
oled.display(); // show on OLED
lastCount != count;
}
}

Format your code...

If you installed the SH1106 library, and choose the example, SH_1106_128x64_I2C.ino you will see it uses the same library calls as your code...

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

If your OLED is not working, perhaps your connections are not good. Show a picture.

1 Like

Please format the code as code. Highlight it, and click on CODE

Hi,
Look at this link and follow the link to the documentation.

Use the library examples to write your code.

Tom.... :smiley: :+1: :coffee: :australia:

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-count-oled
 */

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

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
ezButton button(7);  // create ezButton object that attach to pin 7;
unsigned long lastCount = 0;

void setup() {
  Serial.begin(9600);

  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display

  oled.setTextSize(2);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display

  button.setDebounceTime(50); // set debounce time to 50 milliseconds
  button.setCountMode(COUNT_FALLING);
}

void loop() {
  button.loop(); // MUST call the loop() function first

  unsigned long count = button.getCount();
  if (lastCount != count) {
    Serial.println(count); // print count to Serial Monitor
    oled.clearDisplay(); // clear display
    oled.println(count); // display count
    oled.display();      // show on OLED
    lastCount != count;
  }
}

1 Like

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