// Define the pin numbers
const int buttonPin1 = 2; // the number of the first pushbutton pin
const int buttonPin2 = 3; // the number of the second pushbutton pin
const int relayPin1 = 4; // the number of the first relay control pin
const int relayPin2 = 5; // the number of the second relay control pin
// Variables to store the current and previous button states
int buttonState1 = 0;
int lastButtonState1 = 0;
int buttonState2 = 0;
int lastButtonState2 = 0;
int activeRelay = 0; // 0 indicates no relay is active, 1 indicates relay1 is active, 2 indicates relay2 is active
//Screen Setup
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
//End Screen Setup
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define BUTTON_PIN 2 // Pin connected to the push button
unsigned long previousMillis = 0; // will store last time the timer was updated
unsigned long interval = 1000; // interval at which to update the timer (1 second)
int minutes = 0;
int seconds = 0;
bool buttonPressed = false;
void setup() {
//Timer_Screen Setup
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the buffer
display.clearDisplay();
// Draw initial timer display
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Time In Area:");
display.setTextSize(4);
display.setCursor(0,15);
display.print("00:00");
display.display();
// Set up button pin
// Initialize the button and relay pins as inputs/outputs
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
// Initially turn off the relays
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
}
void loop() {
// Read the state of the first pushbutton
buttonState1 = digitalRead(buttonPin1);
// Check if the button state has changed
if (buttonState1 != lastButtonState1) {
// If the button is pressed (HIGH)
{
if (buttonState1 == HIGH) {
// Activate the first relay
digitalWrite(relayPin1, HIGH);
activeRelay = 1; // Set the active relay
buttonPressed = true;
}
else {
buttonPressed = false;
}
if (activeRelay == 2) {
digitalWrite(relayPin1, LOW);
} else if (activeRelay == 1) {
digitalWrite(relayPin2, LOW);
}
}
// Update lastButtonState1
lastButtonState1 = buttonState1;
}
// Read the state of the second pushbutton
buttonState2 = digitalRead(buttonPin2);
// Check if the button state has changed
if (buttonState2 != lastButtonState2) {
// If the button is pressed (HIGH)
if (buttonState2 == HIGH) {
buttonPressed = true;
} else {
buttonPressed = false;
}
// Deactivate the active relay
if (activeRelay == 1) {
digitalWrite(relayPin1, LOW);
} else if (activeRelay == 2) {
digitalWrite(relayPin2, LOW);
}
activeRelay = 2; // Set the active relay
// Activate the second relay
digitalWrite(relayPin2, HIGH);
}
// Update lastButtonState2
lastButtonState2 = buttonState2;
// Update timer
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (!buttonPressed) {
updateTimer();
} else {
resetTimer();
}
}
}
void updateTimer() {
seconds++;
if (seconds == 60) {
minutes++;
seconds = 0;
}
displayTimer();
}
void resetTimer() {
minutes = 0;
seconds = 0;
displayTimer();
}
void displayTimer() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Time In Area:");
display.setTextSize(4);
display.setCursor(0,15);
if (minutes < 10) {
display.print("0");
}
display.print(minutes);
display.print(":");
if (seconds < 10) {
display.print("0");
}
display.println(seconds);
display.display();
}