Sketch is acting slow. Suggestions please.

This score keeping sketch is not very quick to respond to push button depressions. Seems it needs a .5 second or so pause between depressions to register a button depression; quick successive depressions don't register. I have 4 buttons to increment or decrement the score. The slow behavior is common to them all. They are, of course, momentary contact but they also have a 'snap' to them when depressed. The push buttons have a common ground to pull respective Arduino (Nano) I/O pins low on push button activation.

Please take a look at the sketch to see if you can spot anything. I can't detect anything that would slow it down. Perhaps you may have other suggestion to allow for quicker recognition of push button depressions.

Thanks - Scotty

//As of 6:30PM 1/11/2021 this sketch works in accepting button depresses, keeping score
//and displaying on scores oled.

#include <Arduino.h>
#include <U8g2lib.h>
//#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
//#endif
//#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
//#endif
#define uint8_t byte
const int blue_plus_pin = 2;
const int blue_minus_pin = 6;
const int red_plus_pin = 3;
const int red_minus_pin = 4;
const int reset_pin = 5;
byte blue_plus_state;
byte blue_minus_state;
byte red_plus_state;
byte red_minus_state;
byte reset_state;
byte blue_plus_previous_state = 0;
byte blue_minus_previous_state = 0;
byte red_plus_previous_state = 0;
byte red_minus_previous_state = 0;
byte reset_previous_state = 0;
int blue_score = 0;
int red_score = 0;
byte red = 0;;
byte blue = 0;
byte reset = 0;
bool enable_display = false;
long unsigned int reset_delay_start_time = 0;
#define byte uint8_t



U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI
//U8G2_SH1106_128X64_VCOMH0_1_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
//U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ 8);


void setup() {
  pinMode(blue_plus_pin, INPUT_PULLUP);
  pinMode(blue_minus_pin, INPUT_PULLUP);
  pinMode(red_plus_pin, INPUT_PULLUP);
  pinMode(red_minus_pin, INPUT_PULLUP);
  pinMode(reset_pin, INPUT_PULLUP);
  Serial.begin(9600);
  u8g2.begin();
}

void loop() {
  read_buttons();
  display();
  //button_score_debug();
}
void read_buttons() {
  // read the state of the pushbutton
  red_plus_state = digitalRead(red_plus_pin);
  red_minus_state = digitalRead(red_minus_pin);
  blue_plus_state = digitalRead(blue_plus_pin);
  blue_minus_state = digitalRead(blue_minus_pin);
  reset_state = digitalRead(reset_pin);

  if ((red_plus_state != red_plus_previous_state) && (red_plus_state == 0) && red_score < 21) {
    red_score++;
    enable_display = true;
  }
  if ((red_minus_state != red_minus_previous_state) && (red_minus_state == 0) && red_score >= 1) {
    red_score--;
    enable_display = true;
  }
  if ((blue_plus_state != blue_plus_previous_state) && (blue_plus_state == 0) && blue_score < 21) {
    blue_score++;
    enable_display = true;
  }
  if ((blue_minus_state != blue_minus_previous_state) && (blue_minus_state == 0) && blue_score >= 1) {
    blue_score--;
    enable_display = true;
  }
  if ((reset_state != reset_previous_state) && reset_state == 0) {
    reset_delay_start_time = millis();
  }
  //reset_previous_state = reset_state;
  if ((millis() - reset_delay_start_time >= 5000) && reset_state == 0) {
    red_score = 0;
    blue_score = 0;
    enable_display = true;
  }
  //delay(75);
  red_plus_previous_state = red_plus_state;
  red_minus_previous_state = red_minus_state;
  blue_plus_previous_state = blue_plus_state;
  blue_minus_previous_state = blue_minus_state;
  reset_previous_state = reset_state;
}

void display() {
  if (enable_display) {
    u8g2.clearBuffer();          // clear the internal memory
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(5, 15, "RED   BLUE"); // write something to the internal memory
    u8g2.setCursor(12, 60);
    u8g2.setFont(u8g2_font_logisoso24_tr);
    u8g2.print(red_score);
    u8g2.setCursor(74, 60);
    u8g2.print(blue_score);
    u8g2.sendBuffer();          // transfer internal memory to the display
    delay(500);
    enable_display = false;
  }
}

void button_score_debug() {
  //Serial.print(red_score);
  //Serial.print("              ");
  //Serial.println(blue_score);
  //Serial.print("              ");
  //Serial.print (reset_delay_start_time);
  //Serial.print("              ");
  //Serial.println(millis());
  //Serial.println("           ");
  //Serial.println(blue_score);
  //Serial.print(red_plus_state);
  //Serial.print("           ");
  //Serial.print(red_plus_previous_state);
  //Serial.print("                               ");
  //Serial.println(digitalRead(red_plus_pin));
}

You have a delay(500) in your code. That is .5 seconds. Coincidence? Probably not. :slight_smile: Replace it with the millis() technique you used in the read buttons function.

The delay() function makes your code slow. it can be solved by using millis() function. See How to use millis() instead of delay()

Moreover, using delay() can make your code miss some button press events

The 'delay(500)' appears to have no actual use. Edit - oh prevent screen update flicker. Why not simply just remove it? At the least, you will gain a further understanding from how it behaves when you do. You could test the switches too. To prevent screen flicker you will have to use millis() timing. The positioning of millis() timing statements is fairly standard and you can almost copy and paste. But you have to recognize and know how to mark the beginning and end of the code that you want done periodically (previously done with delay() ). Whereas with delay, the beginning and ending are obvious, just what comes before and after the delay() function call. That, apart from the lack of need of supporting variables, is what makes it more beginner friendly. I think you can see that the LCD code would be the section to enclose in a timing loop.

Klaus_K:
You have a delay(500) in your code. That is .5 seconds. Coincidence?

that’s actually "only" half a second but indeed not great.

The display is being updated in every iteration of loop - that is likely to be a big part of the sloth. loop() should be iteration maybe 100 times (or more) per second and there is no need to update a display that often. Two times per second is probably enough for the human viewer.

Also it will slow things down if the complete display is updated all at once.

...R

My apologies for wasting your time gentlemen and thank you for responding. I did see the delay but mistakenly thought it shouldn't matter since it could only happen when a button was depressed. Of course the sketch is running slow. I'm making it slow by depressing a button and ignoring anything else that happens for .5 seconds. Anyhow, the delay is gone and the problem went with it. - Scotty

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