Monday, October 30, 2017

Arduino : Intermediate - OLED I2C Counter using Pushbutton

Description
Demonstrates the use a 128x64 I2C OLED display to display counter number using pushbutton. The code include debounce function. You can download OLED I2C library at Rinky-Dink Electronic.

List Item
  1. Arduino Uno 
  2. 128x64 I2C OLED  
  3. Pushbutton
  4. Wire Jumper

Wiring Diagram

Video



Coding
#include <OLED_I2C.h>

OLED  myOLED(SDA, SCL, 8);

const int increaseButton = 9;

int buttonState;       
int lastButtonState = LOW;

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 10;

extern uint8_t BigNumbers[];

int value = 0;

void setup()
{
  myOLED.begin();
  myOLED.setFont(BigNumbers);
  pinMode(increaseButton,INPUT_PULLUP);
}

void loop()
{
  increaseParameter();
  myOLED.printNumI(value,CENTER,24);
  myOLED.update();
  myOLED.clrScr();
}

void increaseParameter()
{
  int reading = digitalRead(increaseButton);


  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (reading != buttonState) {
      buttonState = reading;

      if (buttonState == LOW) {
        value++;
      }
    }
  }
  lastButtonState = reading;
}

1 comment:

  1. Hey nice Video. Can you help me? I have the same libary, but there is one fail. When I write as code : " write myOLED.print("Hello",CENTER,12) " then the OLED Display gives some crazy fragments out like Chinese letters. What could that be ?

    ReplyDelete