Hello,
I am working on my first Arduino project and could use a bit of guidance with a code issue I am having.
I have an Adafruit ESP32 Feather wired to an Adafruit 1.27" Color OLED Breakout Board.
I would like to use a simple push button to turn the screen off and on.
So I tried using code that would treat the button as a toggle switch.
So far, it can turn the screen off as long as I hold the button, but the screen comes back on when I let go.
(the code also toggles the onboard LED, just as a backup way to verify the button press. I also have bits of code to debounce the button).
// Screen dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 96 // Change this to 96 for 1.27" OLED.
// You can use any (4 or) 5 pins
#define SCLK_PIN 5
#define MOSI_PIN 18
#define DC_PIN 19
#define CS_PIN 17
#define RST_PIN 16
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1351.h>
#include <SPI.h>
//#include <FreeSans18pt7b.h>
const int buttonPin = 22;
const int ledPin = 13;
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// Option 1: use any pins but a little slower
Adafruit_SSD1351 tft = Adafruit_SSD1351(SCREEN_WIDTH, SCREEN_HEIGHT, CS_PIN, DC_PIN, MOSI_PIN, SCLK_PIN, RST_PIN);
float p = 3.1415926;
// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 200; // the debounce time; increase if the output flickers
void setup(void) {
Serial.begin(9600);
Serial.print("hello!");
tft.begin();
Serial.println("init");
//void fillScreen(uint16_t color=BLACK);
tft.fillRect(0, 0, 128, 128, BLACK);
//delay(100);
//tft.fillRect(10, 10, 118, 86, RED);
delay(500);
tft.setCursor(10, 30);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.println("LOW");
tft.setCursor(20, 65);
tft.setTextSize(2);
tft.println("mmol/L");
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
}
void loop() {
// put your main code here, to run repeatedly:
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
Serial.write("Button");
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}