#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_A 2
#define BUTTON_B 3
#define LED 13
int a_last = 0;
int a_curr = 0;
int b_last = 0;
int b_curr = 0;
unsigned int counter = 0;
void graphicsUpdate();
void setup() {
// put your setup code here, to run once:
// set up pin mode for both buttons
pinMode(BUTTON_A, INPUT);
pinMode(BUTTON_B, INPUT);
// set pin mode for LED
pinMode(LED, OUTPUT);
// some info will be sent to serial
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// set up for text
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.display();
}
void loop() {
// A button check
a_curr = digitalRead(BUTTON_A);
if (a_curr != a_last)
{
// if has changed, increment counter
if (a_curr == LOW) // button just went from off to on
{
counter++;
Serial.println(counter);
delay(50);
}
a_last = a_curr;
/*
if (counter % 4 == 0)
{
Serial.println("Pressed");
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
*/
}
// B button check
b_curr = digitalRead(BUTTON_B);
if (b_curr != b_last)
{
// if has changed, increment counter
if (b_curr == LOW) // button just went from off to on
{
counter--;
Serial.println(counter);
delay(50);
}
b_last = b_curr;
/*
if (counter % 4 == 0)
{
Serial.println("Pressed");
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
*/
}
graphicsUpdate();
}
void graphicsUpdate()
{
display.clearDisplay();
display.setCursor(0, 0);
display.print("DEC:");
display.println(counter);
display.print("BIN:");
display.println(String(counter, BIN));
display.print("HEX:");
display.println(String(counter, HEX));
display.print("OCT:");
display.println(String(counter, OCT));
display.display();
}