Issues with setting up a 2 button counter with Oled display

Hi,

I am on here looking for some help regarding some programming that I am trying to achieve. I am trying to achieve 2 buttons (1 for hours, 1 for minutes) that can be set at a desired time, once set both buttons are to be pressed together to begin the timer. This is all connected to the Oled display that will display the numbers and time in a "00:00" format. This is my current code for one button but I am unsure where to go from here or if this is the best way to have done this. Help with this and how to write it for the outcome I am trying to achieve would be greatly appreciated. I have copied what I have done so far below.

Many thanks to whoever helps!

#include <Wire.h>
#include <U8x8lib.h>

U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

const int  Up_buttonPinHR   = 2;   


// Variables will change:

int buttonPushCounter = 0;  

int up_buttonState = 0;        

int up_lastButtonState = 0;     

bool bPress = false;



void setup()

{
  Serial.begin(9600);

  pinMode( Up_buttonPinHR , INPUT_PULLUP);

  u8x8.begin();

}

void loop()

{

  u8x8.setFont(u8x8_font_chroma48medium8_r);
  
  checkUp();
  
  if( bPress){

       bPress = false;

      u8x8.setCursor(1, 1);
      u8x8.print("00:00");
      u8x8.setCursor(2, 1);
      u8x8.print(buttonPushCounter);
   }

}

void checkUp()

{

  up_buttonState = digitalRead(Up_buttonPinHR);


  if (up_buttonState != up_lastButtonState) {


    if (up_buttonState == LOW) {

        bPress = true;


      buttonPushCounter++;

      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);

    } else {

      Serial.println("off");

    }
    
    delay(50);

  }

  up_lastButtonState = up_buttonState;

}

Below is a start. You can code the function that tests to see whether a button has been pressed to return a boolean value. It's nice because it makes the code more readable and you can store that value in order to check to see if one or both buttons are pressed at the same time. Just duplicate for hour button and minute button.

#include <Wire.h>
#include <U8x8lib.h>

U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

const int  Up_buttonPinHR   = 2;   
byte up_lastButtonState;     

void setup()
{
  Serial.begin(9600);
  pinMode( Up_buttonPinHR , INPUT_PULLUP);
  up_lastButtonState = digitalRead(Up_buttonPinHR);
  u8x8.begin();
}

void loop()
{
  u8x8.setFont(u8x8_font_chroma48medium8_r);
  if( upButtonPressed()){
    u8x8.setCursor(1, 1);
    u8x8.print("00:00");
    u8x8.setCursor(2, 1);
  }
}

bool upButtonPressed()
{
  byte up_buttonState = digitalRead(Up_buttonPinHR);
  if (up_buttonState != up_lastButtonState) {
    up_lastButtonState = up_buttonState;
    delay(50);
    if (up_buttonState == LOW) {
      return true;
    }
  }
  return false;
}

Here is a better start:

#include <Wire.h>
#include <U8x8lib.h>

U8X8_SSD1306_128X64_ALT0_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

const int hourButtonPin = 2;   
const int minuteButtonPin = 3;   

byte hourButtonLast;     
byte minuteButtonLast;     

void setup()
{
  Serial.begin(9600);
  pinMode( hourButtonPin , INPUT_PULLUP);
  hourButtonLast = digitalRead(hourButtonPin);
  u8x8.begin();
}

void loop()
{
  bool hourPressed = hourButtonPressed();
  bool minutePressed = minuteButtonPressed();

  u8x8.setFont(u8x8_font_chroma48medium8_r);
  
  if (hourPressed && minutePressed)
  {
    // start clock
  }
  else if (hourPressed)
  {
    // change hour
  }
  else if (minutePressed)
  {
    // change minute
  }
}

bool hourButtonPressed()
{
  byte hourButtonState = digitalRead(hourButtonPin);
  if (hourButtonState != hourButtonLast) {
    hourButtonLast = hourButtonState;
    delay(50);
    if (hourButtonState == LOW) {
      return true;
    }
  }
  return false;
}

bool minuteButtonPressed()
{
  byte minuteButtonState = digitalRead(minuteButtonPin);
  if (minuteButtonState != minuteButtonLast) {
    minuteButtonLast = minuteButtonState;
    delay(50);
    if (minuteButtonState == LOW) {
      return true;
    }
  }
  return false;
}

It will be simpler if you use the button library.

#include <ezButton.h>

ezButton button1(6);  // create Button object that attach to pin 6;
ezButton button2(7);  // create Button object that attach to pin 7;

void setup() {
   Serial.begin(9600);
   button1.setDebounceTime(50); // set debounce time to 50 milliseconds
   button2.setDebounceTime(50); // set debounce time to 50 milliseconds
   button1.setCountMode(COUNT_FALLING);
   button2.setCountMode(COUNT_FALLING);
}

void loop() {
   button1.loop(); // MUST call the loop() function first
   button2.loop(); // MUST call the loop() function first

   unsigned long btn1Count = button1.getCount();
   unsigned long btn2Count = button2.getCount();
   Serial.print("button 1 count: ");
   Serial.println(btn1Count);
   Serial.print("button 2 count: ");
   Serial.println(btn2Count);
}

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