I'm trying to create a small handheld console with 6 buttons and an I2C 0.96" OLED display: https://imgur.com/a/AKiRMWN

The directional buttons use the analog pin and different resistors technique to save space while the two remaining buttons use a digital pin each. However, despite using the same libraries as other projects, the sketch seems to run very slowly, resulting in a lowered screen refresh rate.

Here is the code:

#define wid 64  // Defines half of the width of the screen for graphic reference
#define hei 32

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display;  // Create display

int av;
const float sp = 3;
float pos[2]={0,0};  // Cursor position

void setup() {
   delay(100);  // Gives the screen time to setup
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize display with the I2C address of 0x3C
   display.display();   // Clears the screen of any remaining images

void loop() {
  av=analogRead(A0);  // Reads the directional input
  av=inputRefine(av); // Assigns it to a direction
  switch (av)         // Changes the cursor position accordingly
    case 1:

    case 2:
    pos[0] += sp;

    case 3:
    pos[1] += sp;

    case 4:
    pos[0] -= sp;

  display.print(millis());  // Prints the system time on the corner of the screen

  display.drawCircle(wid + pos[0], hei + pos[1], 5, WHITE);  // Draws a circle on the cursor position

int inputRefine(int av)
  int a=0;
  if (av>500&&av<600) a=2;
  else if(av>1000) a=3;
  else if(av>920&&av<950) a=4;
  else if(av>680&&av<720) a=1;

  return a;

What is slowing the execution down?

EDIT: Even commenting out any unnecessary line doesn't remove the lag EDIT 2: The problem might reside in the screen itself. Unfortunately, I don't have a backup one. I'll order one as soon as possible and test this theory.

  • 1
    I'd suggest using int instead of float for float pos[2]={0,0};. I'd suggest adding some code that measures how many ms each part of your code uses, and prints it to the serial console. – Gerben Feb 7 at 13:18
  • Here's a great way to debug this kind of thing, especially since the sketch is pretty simple. Comment out everything but the lines which update the display. Leave them in using dummy values for pos. Now you know how fast it can update. Then add things back, one by one, and when it slows down you'll know where the problem is. Also, I second @Gerben's comment about floats - never use them on this kind of processor unless you absolutely have to; they're very very slow compared to integers. – John Romkey Feb 7 at 16:09
  • I edited out any unnecessary lines and only left the display.print(millis()); graphical command, but the lag still remains. Could it be a hardware issue? – Marco Giacomin Feb 7 at 18:23
  • Did you calculate the possible update frequency? Check what happens on "display.display();", github.com/adafruit/Adafruit_SSD1306/blob/master/…. How many bytes are transferred? How long time will this take? – Mikael Patel Feb 7 at 20:11
  • I'm terribly inexperienced in how library source code is handled, but i did try to swap every piece of equipment involved in the console (the only piece i don't own at least two copies of is the screen) and the lag didn't cease. I think the screen could be the problem. – Marco Giacomin Feb 7 at 20:18

Solved by updating the Adafruit sdd1306 library and the Adafruit GFX library

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.