greiman / SSD1306Ascii

Text only Arduino Library for SSD1306 OLED displays
MIT License
500 stars 122 forks source link

Extremly slow refresh time #51

Closed machmar closed 5 years ago

machmar commented 5 years ago

The display is really flickering. If you want my code, let me know. Thanks for response

krukhlis commented 5 years ago

Try to use i2c driver from greiman instead of slow buggy Wire. I mean run this example https://github.com/greiman/SSD1306Ascii/blob/master/examples/AvrI2c128x64/AvrI2c128x64.ino. Also, call the method to increase i2c frequency to 400 000 instead of default slow one.

DirtyEngineer commented 5 years ago

My guess is that you have a clear display call in your main loop. This will cause flickering.

machmar commented 5 years ago

I forgot to mention that I am using wire but all the demos work well. Not my code though. I was thinking if there is any way to write multiple lines at once. code here: `

include

include

include

include

include

include

include <Fonts/FreeSans12pt7b.h >

include <Fonts/FreeSansBold24pt7b.h >

Adafruit_SSD1306 display(-1); Rotary rotary = Rotary(PA2, PA3); OneButton coderButton(PA8, true);

int menuEncoderVar1 = 0; int menuEncoderVar2 = 0; int menuEncoderVar3 = 0; int menuEncoderVar4 = 0; int menuEncoderVar5 = 0; int menuEncoderVar6 = 0; int menuEncoderVar7 = 0; int menuEncoderVar8 = 0; int menuVar1 = 0; int normalClick = 0; int menuScrollVar1 = 0;

int brightness = 25; int brightnessVar = 0; int animationSpeed = 10; int animationSpeedVar = 0; int numberPixels = 0; int animationColorWheel = 0;

long currentmicros = 0; long previousmicros = 0; long currentmicros1 = 0; long previousmicros1 = 0; int topClock = 0; int topClockStop = 0; int floDirection = 0; int floNumber = 0;

void rotate() { unsigned char result = rotary.process(); if (menuVar1 == 0) { if (result == DIR_CW) menuEncoderVar1++; else if (result == DIR_CCW)menuEncoderVar1--; } if (menuVar1 == 2) { if (result == DIR_CW) menuEncoderVar2++; else if (result == DIR_CCW)menuEncoderVar2--; } if (menuVar1 == 4) { if (result == DIR_CW) menuEncoderVar3++; else if (result == DIR_CCW)menuEncoderVar3--; } if (menuVar1 == 6) { if (result == DIR_CW) menuEncoderVar4++; else if (result == DIR_CCW)menuEncoderVar4--; } if (menuVar1 == 8) { if (result == DIR_CW) menuEncoderVar5++; else if (result == DIR_CCW)menuEncoderVar5--; } if (menuVar1 == 10) { if (result == DIR_CW) brightness++; else if (result == DIR_CCW)brightness--; } if (menuVar1 == 12) { if (result == DIR_CW) brightness++; else if (result == DIR_CCW)brightness--; } if (menuVar1 == 31) { if (result == DIR_CW) menuEncoderVar6++; else if (result == DIR_CCW)menuEncoderVar6--; } if (menuVar1 == 12) { if (result == DIR_CW) menuEncoderVar7++; else if (result == DIR_CCW)menuEncoderVar7--; } if (menuVar1 == 29) { if (result == DIR_CW) animationSpeed++; else if (result == DIR_CCW)animationSpeed--; } }

void singleClick() { if (normalClick == 0) { normalClick = 1; return; } if (normalClick == 1) { normalClick = 0; return; } //Serial.println("YEET"); }

void setup() { display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); attachInterrupt(PA3, rotate, CHANGE); attachInterrupt(PA2, rotate, CHANGE); pinMode(PA2, INPUT_PULLUP); pinMode(PA3, INPUT_PULLUP); coderButton.attachClick(singleClick);

display.setFont(&FreeSans12pt7b); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 35); display.println("MachMar"); display.setFont(); display.setCursor(0, 0); display.setTextSize(1); display.println("github.com/machmar"); display.display(); delay(2000); display.clearDisplay(); if (EEPROM.read(194) != 125) { while (normalClick != 1) { display.setTextSize(2); display.setCursor(20, 0); display.println("Alert!"); display.setTextSize(1); display.setCursor(0, 20); display.println("Set the number of pixels in settings menu"); display.setCursor(0, 50); display.println("(Click)"); delay(1); coderButton.tick(); display.display(); } } normalClick = 0; //Serial.begin(2000000); }

void floatIco (int floSpeed, int floSteps) { unsigned long currentmicros = micros(); if (currentmicros - previousmicros1 >= 15) { previousmicros1 = currentmicros; if (floDirection == 0) floNumber++; if (floDirection == 1) floNumber--; } if (floNumber >= floSteps) floDirection != floDirection; }

void topRow() { unsigned long currentmicros = micros(); if (topClock == 0) { display.setTextSize(2); display.setCursor(20, 0); display.println("Neopixel"); } if (topClock == 1) { display.setTextSize(1); display.setCursor(20, 0); display.println("Code By Machmar"); display.setCursor(10, 8); display.println("github.com/machmar"); } if (currentmicros - previousmicros >= 1500) { previousmicros = currentmicros; topClock++; } if (topClock >= 2) { topClock = 0; topClockStop = 1; } }

void loop() {

animationSpeedVar = map(animationSpeed, 1, 400, 10000, 5); brightnessVar = map(brightness, 0, 100, 0, 255); display.clearDisplay(); coderButton.tick(); unsigned char result = rotary.process();

if (menuVar1 == 0) {//------------Main-menu-----------------

display.setTextSize(2);
display.setCursor(15, 0);
display.println("Neopixel");

if (result == DIR_CW) menuEncoderVar1++;
else if (result == DIR_CCW)menuEncoderVar1--;

if (menuEncoderVar1 <= 0) menuScrollVar1 = 0;
if (menuEncoderVar1 == 1 && menuScrollVar1 == 2) menuScrollVar1 = 1;
if (menuEncoderVar1 == 4 && menuScrollVar1 == 0) menuScrollVar1 = 1;
if (menuEncoderVar1 >= 5) menuScrollVar1 = 2;

if (menuScrollVar1 == 0) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(15, 18);
  display.println("Reset");
  display.setCursor(15, 28);
  display.println("Presets and Save");
  display.setCursor(15, 38);
  display.println("Animations");
  display.setCursor(15, 48);
  display.println("Stationary");
  display.setCursor(15, 58);
  display.print("Brightness  ");
  display.print(brightness);
  display.setCursor(floNumber, menuEncoderVar1 * 10 + 18);
  display.println(">");
}
if (menuScrollVar1 == 1) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(15, 16);
  display.println("Reset");
  display.setCursor(15, 26);
  display.println("Presets and Save");
  display.setCursor(15, 36);
  display.println("Animations");
  display.setCursor(15, 46);
  display.println("Stationary");
  display.setCursor(15, 56);
  display.print("Brightness  ");
  display.print(brightness);
  display.setCursor(floNumber, menuEncoderVar1 * 10 + 16);
  display.println(">");
}
if (menuScrollVar1 == 2) {
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(15, 16);
  display.println("Presets and Save");
  display.setCursor(15, 26);
  display.println("Animations");
  display.setCursor(15, 36);
  display.println("Stationary");
  display.setCursor(15, 46);
  display.print("Brightness  ");
  display.print(brightness);
  display.setCursor(15, 56);
  display.println("Settings");
  display.setCursor(floNumber, menuEncoderVar1 * 10 + 6);
  display.println(">");
}

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 1) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;

if (menuEncoderVar1 == 0 && normalClick == 1) menuVar1 = 1;
if (menuEncoderVar1 == 1 && normalClick == 1) menuVar1 = 3;
if (menuEncoderVar1 == 2 && normalClick == 1) menuVar1 = 5;
if (menuEncoderVar1 == 3 && normalClick == 1) menuVar1 = 7;
if (menuEncoderVar1 == 4 && normalClick == 1) menuVar1 = 9;
if (menuEncoderVar1 == 5 && normalClick == 1) menuVar1 = 11;

if (menuEncoderVar1 >= 6) menuEncoderVar1 = 5;
if (menuEncoderVar1 <= -1) menuEncoderVar1 = 0;

}

if (menuVar1 == 1) { normalClick = 0; menuVar1 = 2; menuEncoderVar2 = 0; } if (menuVar1 == 2) {//-------------------Reset-menu--------- if (result == DIR_CW) menuEncoderVar2++; else if (result == DIR_CCW)menuEncoderVar2--;

display.setTextSize(2);
display.setCursor(15, 0);
display.println("Reset");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 18);
display.println("Yes");
display.setCursor(15, 28);
display.println("Cancel");

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 20) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
display.setCursor(floNumber, menuEncoderVar2 * 10 + 18);
display.println(">");

if (menuEncoderVar2 == 0 && normalClick == 1) {
  menuVar1 = 1;
  normalClick = 0;
}
if (menuEncoderVar2 == 1 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar2 >= 2) menuEncoderVar2 = 1;
if (menuEncoderVar2 <= -1) menuEncoderVar2 = 0;

}

if (menuVar1 == 3) { normalClick = 0; menuVar1 = 4; menuEncoderVar3 = 0; } if (menuVar1 == 4) {//------------------Presets-menu------------- if (result == DIR_CW) menuEncoderVar3++; else if (result == DIR_CCW)menuEncoderVar3--;

display.setTextSize(2);
display.setCursor(15, 0);
display.println("Presets");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 18);
display.println("Save");
display.setCursor(15, 28);
display.println("Load");
display.setCursor(15, 38);
display.println("Back");

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 20) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
display.setCursor(floNumber, menuEncoderVar3 * 10 + 18);
display.println(">");

if (menuEncoderVar3 == 0 && normalClick == 1) {
  menuVar1 = 25;
  normalClick = 0;
}
if (menuEncoderVar3 == 1 && normalClick == 1) {
  menuVar1 = 27;
  normalClick = 0;
}
if (menuEncoderVar3 == 2 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar3 >= 3) menuEncoderVar3 = 2;
if (menuEncoderVar3 <= -1) menuEncoderVar3 = 0;

}

if (menuVar1 == 5) { normalClick = 0; menuVar1 = 6; menuEncoderVar4 = 0; } if (menuVar1 == 6) { //-----------------Animations-menu------------ if (result == DIR_CW) menuEncoderVar4++; else if (result == DIR_CCW)menuEncoderVar4--;

display.setTextSize(2);
display.setCursor(5, 0);
display.println("Animations");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 18);
display.print("Speed");
display.setCursor(55, 18);
display.println(animationSpeed);
display.setCursor(15, 28);
display.println("Colors");
display.setCursor(15, 38);
display.println("Back");

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 20) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
display.setCursor(floNumber, menuEncoderVar4 * 10 + 18);
display.println(">");

if (menuEncoderVar4 == 0 && normalClick == 1) {
  menuVar1 = 29;
  normalClick = 0;
}
if (menuEncoderVar4 == 1 && normalClick == 1) {
  menuVar1 = 31;
  normalClick = 0;
}
if (menuEncoderVar4 == 2 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar4 >= 3) menuEncoderVar4 = 2;
if (menuEncoderVar4 <= -1) menuEncoderVar4 = 0;

}

if (menuVar1 == 7) { normalClick = 0; menuVar1 = 8; menuEncoderVar4 = 0; } if (menuVar1 == 8) { //-------------------Stationary-menu------------- if (result == DIR_CW) menuEncoderVar5++; else if (result == DIR_CCW)menuEncoderVar5--;

display.setTextSize(2);
display.setCursor(5, 0);
display.println("Stationary");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 18);
display.println("Number of Colors");
display.setCursor(15, 28);
display.println("Colors");
display.setCursor(15, 38);
display.println("Back");

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 20) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
display.setCursor(floNumber, menuEncoderVar5 * 10 + 18);
display.println(">");

if (menuEncoderVar5 == 0 && normalClick == 1) {
  menuVar1 = 33;
  normalClick = 0;
}
if (menuEncoderVar5 == 1 && normalClick == 1) {
  menuVar1 = 35;
  normalClick = 0;
}
if (menuEncoderVar5 == 2 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar5 >= 3) menuEncoderVar5 = 2;
if (menuEncoderVar5 <= -1) menuEncoderVar5 = 0;

}

if (menuVar1 == 9) { normalClick = 0; menuVar1 = 10; } if (menuVar1 == 10) { //------------------Brightness-menu----------- display.setTextSize(2); display.setCursor(0, 0); display.println("Brightness");

display.setTextSize(1);
display.setCursor(15, 53);
display.setFont(&FreeSansBold24pt7b);
display.println(brightness);
display.setFont();

if (normalClick == 1) {
  normalClick = 0;
  menuVar1 = 0;
}

if (brightness >= 101) brightness = 100;
if (brightness <= -1) brightness = 0;

}

if (menuVar1 == 11) { normalClick = 0; menuVar1 = 2; menuEncoderVar2 = 0; } if (menuVar1 == 12) {//-------------------Settings-menu--------- if (result == DIR_CW) menuEncoderVar8++; else if (result == DIR_CCW)menuEncoderVar8--;

display.setTextSize(2);
display.setCursor(5, 0);
display.println("Settings");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 18);
display.println("Number of Pixels");
display.setCursor(15, 28);
display.println("Back");

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 20) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
display.setCursor(floNumber, menuEncoderVar8 * 10 + 18);
display.println(">");

if (menuEncoderVar8 == 0 && normalClick == 1) {
  menuVar1 = 1;
  normalClick = 0;
}
if (menuEncoderVar8 == 1 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar8 >= 2) menuEncoderVar8 = 1;
if (menuEncoderVar8 <= -1) menuEncoderVar8 = 0;

}

if (menuVar1 == 29) { //---------------Animations-Speed-submenu----------------

display.setTextSize(2);
display.setCursor(35, 0);
display.println("Speed");

display.setTextSize(1);
display.setCursor(15, 53);
display.setFont(&FreeSansBold24pt7b);
display.println(animationSpeed);
display.setFont();

if (normalClick == 1) {
  normalClick = 0;
  menuVar1 = 6;
}

if (animationSpeed >= 401) animationSpeed = 1;
if (animationSpeed <= 0) animationSpeed = 400;

}

if (menuVar1 == 31) { //----------------Animations-Colors-submenu----------- display.setTextSize(2); display.setCursor(25, 0); display.println("Colors");

display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(15, 18);
display.println("Color Wheel");
display.setCursor(15, 28);
display.println("Palette");
display.setCursor(15, 38);
display.println("Back");

unsigned long currentmicros = micros();
if (currentmicros - previousmicros1 >= 20) {
  previousmicros1 = currentmicros;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
display.setCursor(floNumber, menuEncoderVar6 * 10 + 18);
display.println(">");

if (menuEncoderVar6 == 0 && normalClick == 1) {
  animationColorWheel = 1;
  menuVar1 = 6;
  normalClick = 0;
}
if (menuEncoderVar6 == 1 && normalClick == 1) {
  menuVar1 = 100;
  animationColorWheel = 0;
  normalClick = 0;
}
if (menuEncoderVar6 == 2 && normalClick == 1) {
  menuVar1 = 6;
  normalClick = 0;
}
if (menuEncoderVar6 >= 3) menuEncoderVar6 = 2;
if (menuEncoderVar6 <= -1) menuEncoderVar6 = 0;

}

display.display(); }`

DirtyEngineer commented 5 years ago
void loop() {

..
..
display.clearDisplay();
..
..

You're clearing the display each time through your loop. Remember that there is no buffer with this library.

-Edit-

You're not even using this library in your code? You're using an Adafruit one.

greiman commented 5 years ago

Closed - not this library.

#include <Adafruit_SSD1306_STM32.h>
#include <Adafruit_GFX.h>
machmar commented 5 years ago

sorry I sent the wrong code, please reopen this issue

machmar commented 5 years ago

`#include

include

include

include

include

include

SSD1306AsciiWire oled; Rotary rotary = Rotary(PA2, PA3); OneButton coderButton(PA8, true);

int menuEncoderVar1 = 0; int menuEncoderVar2 = 0; int menuEncoderVar3 = 0; int menuEncoderVar4 = 0; int menuEncoderVar5 = 0; int menuEncoderVar6 = 0; int menuEncoderVar7 = 0; int menuEncoderVar8 = 0; int menuVar1 = 0; int normalClick = 0; int menuScrollVar1 = 0;

int brightness = 25; int brightnessVar = 0; int animationSpeed = 10; int animationSpeedVar = 0; int numberPixels = 0; int animationColorWheel = 0;

long currentmillis = 0; long previousmillis = 0; long currentmillis1 = 0; long previousmillis1 = 0; int topClock = 0; int topClockStop = 0; int floDirection = 0; int floNumber = 0;

void rotate() { unsigned char result = rotary.process(); if (menuVar1 == 0) { if (result == DIR_CW) menuEncoderVar1++; else if (result == DIR_CCW)menuEncoderVar1--; } if (menuVar1 == 2) { if (result == DIR_CW) menuEncoderVar2++; else if (result == DIR_CCW)menuEncoderVar2--; } if (menuVar1 == 4) { if (result == DIR_CW) menuEncoderVar3++; else if (result == DIR_CCW)menuEncoderVar3--; } if (menuVar1 == 6) { if (result == DIR_CW) menuEncoderVar4++; else if (result == DIR_CCW)menuEncoderVar4--; } if (menuVar1 == 8) { if (result == DIR_CW) menuEncoderVar5++; else if (result == DIR_CCW)menuEncoderVar5--; } if (menuVar1 == 10) { if (result == DIR_CW) brightness++; else if (result == DIR_CCW)brightness--; } if (menuVar1 == 12) { if (result == DIR_CW) brightness++; else if (result == DIR_CCW)brightness--; } if (menuVar1 == 31) { if (result == DIR_CW) menuEncoderVar6++; else if (result == DIR_CCW)menuEncoderVar6--; } if (menuVar1 == 12) { if (result == DIR_CW) menuEncoderVar7++; else if (result == DIR_CCW)menuEncoderVar7--; } if (menuVar1 == 29) { if (result == DIR_CW) animationSpeed++; else if (result == DIR_CCW)animationSpeed--; } }

void singleClick() { if (normalClick == 0) { normalClick = 1; return; } if (normalClick == 1) { normalClick = 0; return; } //Serial.println("YEET"); }

void setup() { Wire.begin(); Wire.setClock(400000); oled.begin(&Adafruit128x64, 0x3C); attachInterrupt(PA3, rotate, CHANGE); attachInterrupt(PA2, rotate, CHANGE); pinMode(PA2, INPUT_PULLUP); pinMode(PA3, INPUT_PULLUP); coderButton.attachClick(singleClick);

oled.setFont(Cooper19); oled.setCursor(10, 0); oled.println("MachMar"); oled.setFont(Verdana12_bold); oled.setCursor(0, 2); oled.println("github.com/machmar"); delay(2000); oled.clear(); if (EEPROM.read(194) != 125) { while (normalClick != 1) { oled.setFont(Cooper19); oled.setCursor(30, 0); oled.println("Alert !"); oled.setFont(Adafruit5x7); oled.setCursor(0, 20); oled.println("Set the number of pixels in settings menu"); oled.setCursor(0, 50); oled.println("(Click)"); delay(1); coderButton.tick();
} } normalClick = 0; }

void floatIco (int floSpeed, int floSteps) { unsigned long currentmillis = millis(); if (currentmillis - previousmillis1 >= 15) { previousmillis1 = currentmillis; if (floDirection == 0) floNumber++; if (floDirection == 1) floNumber--; } if (floNumber >= floSteps) floDirection != floDirection; }

void topRow() { unsigned long currentmillis = millis(); if (topClock == 0) { oled.setFont(Cooper19); oled.setCursor(20, 0); oled.println("Neopixel"); } if (topClock == 1) { oled.setFont(Adafruit5x7); oled.setCursor(20, 0); oled.println("Code By Machmar"); oled.setCursor(10, 1); oled.println("github.com/machmar"); } if (currentmillis - previousmillis >= 1500) { previousmillis = currentmillis; topClock++; } if (topClock >= 2) { topClock = 0; topClockStop = 1; } }

void loop() { oled.clear(); animationSpeedVar = map(animationSpeed, 1, 400, 10000, 5); brightnessVar = map(brightness, 0, 100, 0, 255); coderButton.tick(); unsigned char result = rotary.process();

if (menuVar1 == 0) {//------------Main-menu-----------------

oled.setFont(Cooper19);
oled.setCursor(15, 0);
oled.println("Neopixel");
oled.clear();

if (result == DIR_CW) menuEncoderVar1++;
else if (result == DIR_CCW)menuEncoderVar1--;

//if (menuEncoderVar1 >= 0 && menuEncoderVar1 <= 3) oled.scrollDisplay(1);
//if (menuEncoderVar1 == 4 && menuScrollVar1 == 0) oled.scrollDisplay(1);

  oled.setFont(Adafruit5x7);
  oled.setCursor(15, 2);
  oled.println("Reset");
  oled.setCursor(15, 3);
  oled.println("Presets and Save");
  oled.setCursor(15, 4);
  oled.println("Animations");
  oled.setCursor(15, 5);
  oled.println("Stationary");
  oled.setCursor(15, 6);
  oled.print("Brightness  ");
  oled.print(brightness);
  oled.setCursor(15,7);
  oled.print("Settings");
  oled.setCursor(floNumber, menuEncoderVar1 + 2);
  oled.println(">");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 1) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;

if (menuEncoderVar1 == 0 && normalClick == 1) menuVar1 = 1;
if (menuEncoderVar1 == 1 && normalClick == 1) menuVar1 = 3;
if (menuEncoderVar1 == 2 && normalClick == 1) menuVar1 = 5;
if (menuEncoderVar1 == 3 && normalClick == 1) menuVar1 = 7;
if (menuEncoderVar1 == 4 && normalClick == 1) menuVar1 = 9;
if (menuEncoderVar1 == 5 && normalClick == 1) menuVar1 = 11;

if (menuEncoderVar1 >= 6) menuEncoderVar1 = 5;
if (menuEncoderVar1 <= -1) menuEncoderVar1 = 0;

}

if (menuVar1 == 1) { normalClick = 0; menuVar1 = 2; menuEncoderVar2 = 0; } if (menuVar1 == 2) {//-------------------Reset-menu--------- if (result == DIR_CW) menuEncoderVar2++; else if (result == DIR_CCW)menuEncoderVar2--;

oled.setFont(Cooper19);
oled.setCursor(15, 0);
oled.println("Reset");

oled.setFont(Adafruit5x7);
oled.setCursor(15, 2);
oled.println("Yes");
oled.setCursor(15, 3);
oled.println("Cancel");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 20) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
oled.setCursor(floNumber, menuEncoderVar2 + 2);
oled.println(">");

if (menuEncoderVar2 == 0 && normalClick == 1) {
  menuVar1 = 1;
  normalClick = 0;
}
if (menuEncoderVar2 == 1 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar2 >= 2) menuEncoderVar2 = 1;
if (menuEncoderVar2 <= -1) menuEncoderVar2 = 0;

}

if (menuVar1 == 3) { normalClick = 0; menuVar1 = 4; menuEncoderVar3 = 0; } if (menuVar1 == 4) {//------------------Presets-menu------------- if (result == DIR_CW) menuEncoderVar3++; else if (result == DIR_CCW)menuEncoderVar3--;

oled.setFont(Cooper19);
oled.setCursor(15, 0);
oled.println("Presets");

oled.setFont(Adafruit5x7);
oled.setCursor(15, 2);
oled.println("Save");
oled.setCursor(15, 3);
oled.println("Load");
oled.setCursor(15, 4);
oled.println("Back");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 20) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
oled.setCursor(floNumber, menuEncoderVar3 + 2);
oled.println(">");

if (menuEncoderVar3 == 0 && normalClick == 1) {
  menuVar1 = 25;
  normalClick = 0;
}
if (menuEncoderVar3 == 1 && normalClick == 1) {
  menuVar1 = 27;
  normalClick = 0;
}
if (menuEncoderVar3 == 2 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar3 >= 3) menuEncoderVar3 = 2;
if (menuEncoderVar3 <= -1) menuEncoderVar3 = 0;

}

if (menuVar1 == 5) { normalClick = 0; menuVar1 = 6; menuEncoderVar4 = 0; } if (menuVar1 == 6) { //-----------------Animations-menu------------ if (result == DIR_CW) menuEncoderVar4++; else if (result == DIR_CCW)menuEncoderVar4--;

oled.setFont(Cooper19);
oled.setCursor(5, 0);
oled.println("Animations");

oled.setFont(Adafruit5x7);
oled.setCursor(15, 2);
oled.print("Speed");
oled.setCursor(55, 2);
oled.println(animationSpeed);
oled.setCursor(15, 3);
oled.println("Colors");
oled.setCursor(15, 4);
oled.println("Back");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 20) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
oled.setCursor(floNumber, menuEncoderVar4 + 2);
oled.println(">");

if (menuEncoderVar4 == 0 && normalClick == 1) {
  menuVar1 = 29;
  normalClick = 0;
}
if (menuEncoderVar4 == 1 && normalClick == 1) {
  menuVar1 = 31;
  normalClick = 0;
}
if (menuEncoderVar4 == 2 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar4 >= 3) menuEncoderVar4 = 2;
if (menuEncoderVar4 <= -1) menuEncoderVar4 = 0;

}

if (menuVar1 == 7) { normalClick = 0; menuVar1 = 8; menuEncoderVar4 = 0; } if (menuVar1 == 8) { //-------------------Stationary-menu------------- if (result == DIR_CW) menuEncoderVar5++; else if (result == DIR_CCW)menuEncoderVar5--;

oled.setFont(Cooper19);
oled.setCursor(5, 0);
oled.println("Stationary");

oled.setFont(Adafruit5x7);
oled.setCursor(15, 2);
oled.println("Number of Colors");
oled.setCursor(15, 3);
oled.println("Colors");
oled.setCursor(15, 4);
oled.println("Back");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 20) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
oled.setCursor(floNumber, menuEncoderVar5 + 2);
oled.println(">");

if (menuEncoderVar5 == 0 && normalClick == 1) {
  menuVar1 = 33;
  normalClick = 0;
}
if (menuEncoderVar5 == 1 && normalClick == 1) {
  menuVar1 = 35;
  normalClick = 0;
}
if (menuEncoderVar5 == 2 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar5 >= 3) menuEncoderVar5 = 2;
if (menuEncoderVar5 <= -1) menuEncoderVar5 = 0;

}

if (menuVar1 == 9) { normalClick = 0; menuVar1 = 10; } if (menuVar1 == 10) { //------------------Brightness-menu-----------------------HERE-HERE-HERE-HERE---------------- oled.setFont(Cooper19); oled.setCursor(0, 0); oled.println("Brightness"); oled.setCursor(15, 2); oled.setFont(fixednums15x31); oled.println(brightness); oled.setFont(Adafruit5x7);

if (normalClick == 1) {
  normalClick = 0;
  menuVar1 = 0;
}

if (brightness >= 101) brightness = 100;
if (brightness <= -1) brightness = 0;

}

if (menuVar1 == 11) { normalClick = 0; menuVar1 = 2; menuEncoderVar2 = 0; } if (menuVar1 == 12) {//-------------------Settings-menu--------- if (result == DIR_CW) menuEncoderVar8++; else if (result == DIR_CCW)menuEncoderVar8--;

oled.setFont(Cooper19);
oled.setCursor(5, 0);
oled.println("Settings");

oled.setFont(Adafruit5x7);
oled.setCursor(15, 2);
oled.println("Number of Pixels");
oled.setCursor(15, 3);
oled.println("Back");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 20) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
oled.setCursor(floNumber, menuEncoderVar8 + 2);
oled.println(">");

if (menuEncoderVar8 == 0 && normalClick == 1) {
  menuVar1 = 1;
  normalClick = 0;
}
if (menuEncoderVar8 == 1 && normalClick == 1) {
  menuVar1 = 0;
  normalClick = 0;
}
if (menuEncoderVar8 >= 2) menuEncoderVar8 = 1;
if (menuEncoderVar8 <= -1) menuEncoderVar8 = 0;

}

if (menuVar1 == 29) { //---------------Animations-Speed-submenu--------------------HERE-HERE-HERE-HERE--------

oled.setFont(Cooper19);
oled.setCursor(35, 0);
oled.println("Speed");
oled.setCursor(15, 2);
oled.setFont(fixednums15x31);
oled.println(animationSpeed);
oled.setFont(Adafruit5x7);

if (normalClick == 1) {
  normalClick = 0;
  menuVar1 = 6;
}

if (animationSpeed >= 401) animationSpeed = 1;
if (animationSpeed <= 0) animationSpeed = 400;

}

if (menuVar1 == 31) { //----------------Animations-Colors-submenu----------- oled.setFont(Cooper19); oled.setCursor(25, 0); oled.println("Colors");

oled.setFont(Adafruit5x7);

oled.setCursor(15, 2);
oled.println("Color Wheel");
oled.setCursor(15, 3);
oled.println("Palette");
oled.setCursor(15, 4);
oled.println("Back");

unsigned long currentmillis = millis();
if (currentmillis - previousmillis1 >= 20) {
  previousmillis1 = currentmillis;
  if (floDirection == 0) floNumber++;
  if (floDirection == 1) floNumber--;
}
if (floNumber >= 5) floDirection = !floDirection;
if (floNumber <= 0) floDirection = !floDirection;
oled.setCursor(floNumber, menuEncoderVar6 + 2);
oled.println(">");

if (menuEncoderVar6 == 0 && normalClick == 1) {
  animationColorWheel = 1;
  menuVar1 = 6;
  normalClick = 0;
}
if (menuEncoderVar6 == 1 && normalClick == 1) {
  menuVar1 = 100;
  animationColorWheel = 0;
  normalClick = 0;
}
if (menuEncoderVar6 == 2 && normalClick == 1) {
  menuVar1 = 6;
  normalClick = 0;
}
if (menuEncoderVar6 >= 3) menuEncoderVar6 = 2;
if (menuEncoderVar6 <= -1) menuEncoderVar6 = 0;

} }`

greiman commented 5 years ago

Looks like you clear the screen each time you enter loop so it will flicker.

Clearing the screen requires writing 1024 bytes of zero to the display memory.

oled,clear() takes about 46 ms.

Run this code in loop. It flickers.

void loop() {
  uint32_t m = micros();
  oled.clear();
  oled.print("\nmicros: ");
  oled.print(micros() - m);
  }
noeldum commented 2 weeks ago

I am facing the same issue as the OP here.

With the Adafruit lib I do clear the display in a loop and it does not flicker.

Using the same code ported to SSD1306Ascii it does flicker.

What is the reason for this? Is it because one is using buffer and the other not?

What are the options to avoid this? Should I use clearField() to just clear the area of interest. The problem with that is that I need to know the length of what I am displaying.

greiman commented 2 weeks ago

What is the reason for this? Is it because one is using buffer and the other not?

This is likely the reason. SSD1306Ascii does not use a buffer image of the screen.

It will be much faster if you only clear areas that you are not rewriting.

You can use this function to find the length:

size_t strWidth(const char* str) const;

noeldum commented 2 weeks ago

Thank you. This explains that then.

I found to avoid the flicker is not to clear anything at all and replace the text I want to update. The issue here is that I need to control every length of text I am sending. Hence filling with blank space if the string gets shorter For example if I send "12.58V" then update with "8.23V" it would come as "8.23VV" if I don't fill with space. I found the dtostrf command to be the rescue here (yes because on top of that arduino does no process sprintf with floats arf!).

Well the no buffer story is a good news for me. I am actually switching to this lib because I was facing low RAM with the adafruit lib. So I guess now I don't need to keep about 1k of free ram to be able to run my code with the oled now?