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));
}