2

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

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.clearDisplay();
   display.setTextColor(WHITE);
   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:
    pos[1]-=sp;
    break;

    case 2:
    pos[0] += sp;
    break;

    case 3:
    pos[1] += sp;
    break;

    case 4:
    pos[0] -= sp;
    break;
  }
  display.clearDisplay();

  display.setCursor(5,5);
  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
  display.display();
  }

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.

5
  • 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, 2019 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.
    – romkey
    Feb 7, 2019 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?
    – none none
    Feb 7, 2019 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? Feb 7, 2019 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.
    – none none
    Feb 7, 2019 at 20:18

1 Answer 1

2

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

EDIT: Why am I being downvoted?

1
  • Your question was down voted automatically by the "system" because there is very little content. I have up voted it to cancel the down vote. Please edit your answer to remove the EDIT and add just a little bit more information. Perhaps the version numbers of the libraries that didn't work and the version numbers that you are using now.
    – VE7JRO
    Dec 7, 2019 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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