/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-count-oled
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ezButton.h>
#define LEFT_BTN_PIN 3
#define RIGHT_BTN_PIN 4
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define I2C_SDA 6
#define I2C_SCL 7
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C
ezButton button(LEFT_BTN_PIN); // create ezButton object that attach to pin 7;
unsigned long lastCount = 0;
void setup() {
Serial.begin(9600);
Wire.setPins(I2C_SDA, I2C_SCL);
// initialize OLED display with address 0x3C for 128x64
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000); // wait for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // text size
oled.setTextColor(WHITE); // text color
oled.setCursor(0, 10); // position to display
oled.println("Hello World!");
oled.display();
button.setDebounceTime(50); // set debounce time to 50 milliseconds
button.setCountMode(COUNT_FALLING);
}
void loop() {
button.loop(); // MUST call the loop() function first
unsigned long count = button.getCount();
if (lastCount != count) {
Serial.println(count); // print count to Serial Monitor
oled.clearDisplay(); // clear display
oled.println(count); // display count
oled.display(); // show on OLED
lastCount != count;
}
}
#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)
#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 CURSORS_X 30
#define CURSORS_Y 10
#define I2C_SDA 6
#define I2C_SCL 7
#define BTN_LEFT 1 // button left pin
#define BTN_RIGHT 2 // button right pin
// variable for reading the pushbutton status
int buttonStateLeft = 0;
int buttonStateRight = 0;
void setup() {
Serial.begin(9600);
Wire.setPins(I2C_SDA, I2C_SCL);
delay(1000);
pinMode(BTN_LEFT, INPUT);
pinMode(BTN_RIGHT, INPUT);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
}
display.setTextSize(2); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(CURSORS_X, CURSORS_Y); // Set the Start
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
}
void loop() {
// read the state of the pushbutton value:
buttonStateLeft = digitalRead(BTN_LEFT);
buttonStateRight = digitalRead(BTN_RIGHT);
// check if any of the pushbuttons are pressed. If it is, the buttonState is HIGH:
if (buttonStateLeft == HIGH || buttonStateRight == HIGH) {
display.clearDisplay();
display.setCursor(CURSORS_X, CURSORS_Y);
display.print(F("SIGNAL"));
display.setCursor(CURSORS_X+15, CURSORS_Y+30);
display.print(F("SENT"));
display.display();
delay(1000);
}
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1