0% found this document useful (0 votes)
11 views4 pages

Push Button

Uploaded by

power vision
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Push Button

Uploaded by

power vision
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <Arduino.

h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Define pin numbers for buttons and LEDs


const int pulseButtonPin = 4; // Pin for the PULSE button
const int lowButtonPin = 5; // Pin for the LOW button
const int mediumButtonPin = 6; // Pin for the MEDIUM button
const int highButtonPin = 7; // Pin for the HIGH button
const int ledPulse = 8; // LED for PULSE
const int ledLow = 9; // LED for LOW
const int ledMedium = 10; // LED for MEDIUM
const int ledHigh = 11; // LED for HIGH

// Variables to track button states


volatile bool pulseButtonState = HIGH;
volatile bool lastPulseButtonState = HIGH;
volatile bool lowButtonState = HIGH;
volatile bool lastLowButtonState = HIGH;
volatile bool mediumButtonState = HIGH;
volatile bool lastMediumButtonState = HIGH;
volatile bool highButtonState = HIGH;
volatile bool lastHighButtonState = HIGH;

// OLED display configuration


#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Function prototypes
void turnOffAllLEDs();
void displayCenteredText(const char* text, uint8_t textSize);
void displaySpeed(const char* speed);
void displayWelcomeMessage();
void displayReadyMessage();

void setup() {
// Initialize serial communication
Serial.begin(9600);

// Set button pins as input with internal pull-up resistors


pinMode(pulseButtonPin, INPUT_PULLUP);
pinMode(lowButtonPin, INPUT_PULLUP);
pinMode(mediumButtonPin, INPUT_PULLUP);
pinMode(highButtonPin, INPUT_PULLUP);

// Set LED pins as outputs


pinMode(ledPulse, OUTPUT);
pinMode(ledLow, OUTPUT);
pinMode(ledMedium, OUTPUT);
pinMode(ledHigh, OUTPUT);

// Initialize LEDs to LOW


digitalWrite(ledPulse, LOW);
digitalWrite(ledLow, LOW);
digitalWrite(ledMedium, LOW);
digitalWrite(ledHigh, LOW);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}

// Display "WELCOME" message on OLED


displayWelcomeMessage();

// Wait for 3 seconds


delay(3000);

// Display "READY" message on OLED


displayReadyMessage();

Serial.println(F("Setup complete"));
}

void loop() {
// Read the current state of the buttons
pulseButtonState = digitalRead(pulseButtonPin);
lowButtonState = digitalRead(lowButtonPin);
mediumButtonState = digitalRead(mediumButtonPin);
highButtonState = digitalRead(highButtonPin);

// Check if the PULSE button state has changed


if (pulseButtonState != lastPulseButtonState) {
if (pulseButtonState == LOW) { // Button is pressed
Serial.println("PULSE");
turnOffAllLEDs(); // Turn off all LEDs
digitalWrite(ledPulse, HIGH); // Turn on the PULSE LED

// Display PULSE on the OLED


displayCenteredText("PULSE", 4);
} else { // Button is released
digitalWrite(ledPulse, LOW); // Turn off the PULSE LED
displayReadyMessage();
}
// Debounce delay
delay(50);
}
lastPulseButtonState = pulseButtonState;

// Check if the LOW button state has changed


if (lowButtonState != lastLowButtonState) {
if (lowButtonState == LOW) { // Button is pressed
Serial.println("LOW");
if (digitalRead(ledLow) == LOW) { // If LOW LED is off, turn it on
turnOffAllLEDs();
digitalWrite(ledLow, HIGH);
displaySpeed(" LOW ");
} else { // If LOW LED is on, turn it off
digitalWrite(ledLow, LOW);
displayReadyMessage();
}
}
// Debounce delay
delay(50);
}
lastLowButtonState = lowButtonState;

// Check if the MEDIUM button state has changed


if (mediumButtonState != lastMediumButtonState) {
if (mediumButtonState == LOW) { // Button is pressed
Serial.println("MEDIUM");
if (digitalRead(ledMedium) == LOW) { // If MEDIUM LED is off, turn it on
turnOffAllLEDs();
digitalWrite(ledMedium, HIGH);
displaySpeed("MEDIUM");
} else { // If MEDIUM LED is on, turn it off
digitalWrite(ledMedium, LOW);
displayReadyMessage();
}
}
// Debounce delay
delay(50);
}
lastMediumButtonState = mediumButtonState;

// Check if the HIGH button state has changed


if (highButtonState != lastHighButtonState) {
if (highButtonState == LOW) { // Button is pressed
Serial.println("HIGH");
if (digitalRead(ledHigh) == LOW) { // If HIGH LED is off, turn it on
turnOffAllLEDs();
digitalWrite(ledHigh, HIGH);
displaySpeed("HIGH");
} else { // If HIGH LED is on, turn it off
digitalWrite(ledHigh, LOW);
displayReadyMessage();
}
}
// Debounce delay
delay(50);
}
lastHighButtonState = highButtonState;
}

// Function to turn off all LEDs


void turnOffAllLEDs() {
digitalWrite(ledPulse, LOW);
digitalWrite(ledLow, LOW);
digitalWrite(ledMedium, LOW);
digitalWrite(ledHigh, LOW);
}

// Function to display centered text on the OLED


void displayCenteredText(const char* text, uint8_t textSize) {
display.clearDisplay();
display.setTextSize(textSize);
display.setTextColor(SSD1306_WHITE);

// Adjust the cursor position to center the text


int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(text, 0, 0, &x1, &y1, &w, &h);
int16_t x = (SCREEN_WIDTH - w) / 2;
int16_t y = (SCREEN_HEIGHT - h) / 2;

display.setCursor(x, y);
display.println(text);
display.display();
}

// Function to display speed on the OLED


void displaySpeed(const char* speed) {
uint8_t textSize = (strcmp(speed, "MEDIUM") == 0) ? 3 : 4;
displayCenteredText(speed, textSize);
}

// Function to display "WELCOME" message on OLED


void displayWelcomeMessage() {
displayCenteredText("WELCOME", 3);
}

// Function to display "READY" message on OLED


void displayReadyMessage() {
displayCenteredText("READY", 4);
}

You might also like