use push button to led on off
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_ADDR 0x3C
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// Constants
const int buttonPin = 2; // Digital input pin for push button
const int ledPin = 9; // Digital output pin for LED
// Variables
int buttonState = 0; // Variable to store the state of the button
void setup() {
pinMode(buttonPin, INPUT); // Set the button pin as an input
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
// Initialize the OLED display
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Button:");
display.display();
}
void loop() {
buttonState = digitalRead(buttonPin); // Read the button state (HIGH or LOW)
if (buttonState == HIGH) { // If the button is pressed
digitalWrite(ledPin, HIGH); // Turn on the LED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Button:");
display.setCursor(0, 30);
display.println("LED ON");
display.display();
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Button:");
display.setCursor(0, 30);
display.println("LED OFF");
display.display();
}
}
Comments
Post a Comment