Example Code for Arduino-Point Counter

Last revision 2025/12/18

This guide offers step-by-step instructions and example code for creating a point counter using an Arduino, OLED display, and button modules, providing an excellent starting point for beginners in electronics.

Hardware Preparation

Software Preparation

Wiring Diagram

Connect the interrupt pins (INT) of the two button modules to the digital pins 2 and 3 of the UNO board, and the OLED display to another I2C interface on the UNO mainboard.

DFR0991-Point Counter wiring diagram

Sample Code

Dual RGB buttons interrupt count, OLED shows add/sub & difference

#include <DFRobot_RGBButton.h>
#include <U8g2lib.h>

DFRobot_RGBButton RGBButton_add(&Wire, /*I2CAddr*/ 0x23);
DFRobot_RGBButton RGBButton_sub(&Wire, /*I2CAddr*/ 0x24);
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(/* rotation=*/U8G2_R0, /* reset=*/ U8X8_PIN_NONE);  
/* Interrupt flag */
volatile bool flag_add = false, flag_sub = false;
int add_num=0,sub_num=0,total=0;
/* External interrupt flag */
void interrupt_add()
{
  if(digitalRead(2)==HIGH){
    flag_add = true;
    add_num+=1;
  }else{
    flag_add = false;
  }
}
void interrupt_sub()
{
  if(digitalRead(3)==HIGH){
    flag_sub = true;
    sub_num+=1;
  }else{
    flag_sub = false;
  }
}
void setup(void)
{
  Serial.begin(115200);
  Wire.begin();

  u8g2.begin();
  u8g2.enableUTF8Print();
  u8g2.setFont(u8g2_font_t0_17b_tr  );
  Wire.setClock(100000);
  /**
   * @brief Init function
   * @return bool type, true if successful, false if error
   */
  while( ! RGBButton_add.begin() ){
    Serial.println("1!");
    delay(3000);
  }
  while( ! RGBButton_sub.begin() ){
    Serial.println("2!");
    delay(3000);
  }
  Serial.println("Begin ok!\n");
  attachInterrupt(/*Interrupt No*/0, interrupt_add, CHANGE);   // Open the external interrupt 0, connect INT to the digital pin of the main control:
  attachInterrupt(/*Interrupt No*/1, interrupt_sub, CHANGE);   // Open the external interrupt 0, connect INT to the digital pin of the main control:

}

void loop()
{

  Wire.setClock(100000);
  if(flag_add){
    RGBButton_add.setRGBColor(RGBButton_add.eRed);
  }else{
    RGBButton_add.setRGBColor(RGBButton_add.eBlue);
  }
  if(flag_sub){ 
    RGBButton_sub.setRGBColor(RGBButton_sub.eGreen);
  }else{
    RGBButton_sub.setRGBColor(RGBButton_sub.eBlue);
  }
  String str_add="ADD:"+String(add_num);
  String str_sub="DED:"+String(sub_num);
  String str_total="TOTAL:"+String(add_num-sub_num);
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_t0_17b_tr  );  // choose a suitable font
  u8g2.drawStr(0,10,str_add.c_str());  // write something to the internal memory
  u8g2.drawStr(0,30,str_sub.c_str());  // write something to the internal memory
  u8g2.drawStr(0,50,str_total.c_str());  // write something to the internal memory
  u8g2.sendBuffer();
}

Result

After uploading the codes, the two buttons light up blue. We can attach labels to them, the green for plus and red for minus. When pressed, the plus button lights up green and the minus button lights up red, and the points change can be clearly seen on the display.

Was this article helpful?

TOP