Oled text delay affects other function, any other function or proper code?

im building a 11 switches dumbpad with rotary encoder and oled display,
im about to develop more whatt i need to display to the oled but im a bit confused with the delay that i need to display into oled. so theres a main display showing idle screen text, when i update function its update a text into my oled, like play and pause, volume up or down, i want the text show in oled in certain time wich 1sec, but its also need to wait for a sec in order to make any function work.

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Rotary.h>
#include <Keypad.h>
#include "HID-Project.h"
Rotary r = Rotary(14, 16); // digital Pin attached to the rotary
Adafruit_SSD1306 display(128, 64, &Wire); //  oled library


int pulse = 0;
#define SW 15
int buttonState;

unsigned long timePress = 0;
unsigned long timePressLimit = 0;
unsigned long lastButtonPress = 0;

int clicks = 0;
// kypad

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte pin_rows[ROW_NUM] = {7, 6, 5, 4}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {8, 9, 10}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// kypad


void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  r.begin(true);
  maindisp();

}

void loop() {

  // kypad
char key = keypad.getKey();

  if (key){
    Serial.println(key);
  }

  switch(key){
    case '1':
      Keyboard.print('1');
      break;
    case '2':
      Keyboard.print('2');
      break;
    case '3':
      Keyboard.print('3');
      break;
    case '4':
      Keyboard.print('4');
      break;
    case '5':
      Keyboard.print('5');
      break;
     case '6':
      Keyboard.print('6');
      break;   
    case '7':
      Keyboard.print('7');
      break;    
    case '8':
      Keyboard.print('8');
      break;
    case '9':
      Keyboard.print('9');
      break;
    case '0':
      Keyboard.print('0');
      break;
    case '*':
      Keyboard.print('*');
      break;
  }
// kypad
  

  unsigned char result = r.process();
 
  if (result == DIR_CCW){
Consumer.write(MEDIA_VOLUME_DOWN);
Consumer.write(MEDIA_VOLUME_DOWN);
      display.clearDisplay();
      display.setCursor(5, 20);
      display.setTextColor(WHITE);
      display.setTextSize(2);
      display.setCursor(0, 20);
      display.print("vol down");
      display.display();
      maindisp();
         
}

  if (result == DIR_CW)
  {
Consumer.write(MEDIA_VOLUME_UP);

    
      display.clearDisplay();
      display.setCursor(5, 20);
      display.setTextColor(WHITE);
      display.setTextSize(2);
      display.setCursor(0, 20);
      display.print("vol up");
      display.display();
      maindisp(); 
      
  }

        
	int btnState = digitalRead(SW);
if (btnState == LOW) {
		//if 50ms have passed since last LOW pulse, it means that the
		//button has been pressed, released and pressed again
		if (millis() - lastButtonPress > 50) {
			Serial.println("play/pause");
      Consumer.write(MEDIA_PLAY_PAUSE);
      display.clearDisplay();
       display.setTextSize(2);
 display.setTextColor(WHITE);

 display.setCursor(0, 20);
 display.print("play/pause");
display.display(); 
 delay(100);
 display.clearDisplay();
maindisp();
      
      
		}

		// Remember last button press event
		lastButtonPress = millis();
}

delay(1);
    
	}
void maindisp(){
   delay(1000);
display.clearDisplay();
display.setTextSize(2);
 display.setTextColor(WHITE);
 display.setCursor(0, 20);
 display.print("DUMBPAD");
  display.display();
}

Using delay(1000) will cause the code to stop doing anything for 1 second. If you want the code to do something during the delay() then you need to use non blocking timing. See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Nothing to do with timing, but why use switch/case to send the keystrokes when you can simply send the value of the key variable ?

Why not just

Keyboard.print(key);

instead of all this:

i've tried this but it seems just give me a blink (less than 500ms kinda) instead a second. sometimes it gets stuck and not do the code unless i give a little rotation. did i write it wrong?

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Rotary.h>
#include <Keypad.h>
#include "HID-Project.h"
Rotary r = Rotary(14, 16); // digital Pin attached to the rotary
Adafruit_SSD1306 display(128, 64, &Wire); //  oled library

const long period = 1000;
unsigned long previousMillis = 0;


int pulse = 0;
#define SW 15
int buttonState;

unsigned long timePress = 0;
unsigned long timePressLimit = 0;
unsigned long lastButtonPress = 0;

int clicks = 0;
// kypad

const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte pin_rows[ROW_NUM] = {7, 6, 5, 4}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {8, 9, 10}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
// kypad


void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x64)
  r.begin(true);

}

void loop() {
  unsigned long currentMillis = millis();

  // kypad
char key = keypad.getKey();

  if (key){
    Serial.println(key);
  }

  switch(key){
    case '1':
      Keyboard.print('1');
      break;
    case '2':
      Keyboard.print('2');
      break;
    case '3':
      Keyboard.print('3');
      break;
    case '4':
      Keyboard.print('4');
      break;
    case '5':
      Keyboard.print('5');
      break;
     case '6':
      Keyboard.print('6');
      break;   
    case '7':
      Keyboard.print('7');
      break;    
    case '8':
      Keyboard.print('8');
      break;
    case '9':
      Keyboard.print('9');
      break;
    case '0':
      Keyboard.print('0');
      break;
    case '*':
      Keyboard.print('*');
      break;
  }
// kypad
  

  unsigned char result = r.process();
 
  if (result == DIR_CCW){
Consumer.write(MEDIA_VOLUME_DOWN);
Consumer.write(MEDIA_VOLUME_DOWN);

      display.clearDisplay();
      display.setCursor(5, 20);
      display.setTextColor(WHITE);
      display.setTextSize(2);
      display.setCursor(0, 20);
      display.print("vol down");
      display.display();
       if (currentMillis - previousMillis >= period){
       
      display.clearDisplay();
display.setTextSize(2);
 display.setTextColor(WHITE);
 display.setCursor(0, 20);
 display.print("DUMBPAD");
  display.display();
  previousMillis += currentMillis;
}
         
}

  if (result == DIR_CW)
  {
Consumer.write(MEDIA_VOLUME_UP);

    
      display.clearDisplay();
      display.setCursor(5, 20);
      display.setTextColor(WHITE);
      display.setTextSize(2);
      display.setCursor(0, 20);
      display.print("vol up");
      display.display();
     
      if (currentMillis - previousMillis >= period){
       
      display.clearDisplay();
display.setTextSize(2);
 display.setTextColor(WHITE);
 display.setCursor(0, 20);
 display.print("DUMBPAD");
  display.display();

  previousMillis += currentMillis;
}
  }
        
	int btnState = digitalRead(SW);
if (btnState == LOW) {
		//if 50ms have passed since last LOW pulse, it means that the
		//button has been pressed, released and pressed again
		if (millis() - lastButtonPress > 50) {
			Serial.println("play/pause");
      Consumer.write(MEDIA_PLAY_PAUSE);
      display.clearDisplay();
       display.setTextSize(2);
 display.setTextColor(WHITE);

 display.setCursor(0, 20);
 display.print("play/pause");
display.display(); 
if (millis() - previousMillis > 1000){
       
      display.clearDisplay();
display.setTextSize(2);
 display.setTextColor(WHITE);
 display.setCursor(0, 20);
 display.print("DUMBPAD");
  display.display();
 previousMillis = period;
}
      
      
		}

		// Remember last button press event
		lastButtonPress = millis();
}

delay(1);
    
	}

i manage it later to make another layout so i can make it another function in some apps, my plan is to show current state layout in my oled screen.

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