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.
int
instead offloat
forfloat 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.display.print(millis());
graphical command, but the lag still remains. Could it be a hardware issue?