This circuit is designed to interface an Arduino UNO with a variety of components including a tactile switch, a 7-segment serial display, a DIP switch, an LED, a resistor, and a buzzer. The Arduino UNO serves as the central processing unit, running embedded code to control the display and read inputs from the tactile switch and DIP switch. The LED and buzzer provide visual and audible feedback, respectively. The circuit's functionality is centered around a timer system that can be set and started using the DIP switches and tactile switch, with the countdown displayed on the 7-segment display.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
// Define the pin connections
const int buttonPin = 2; // The push button is connected to pin D2
const int ledPin = 7; // The LED is connected to pin D7
const int buzzerPin = 8; // The buzzer is connected to pin D8
// Define the DIP switch pins
const int dipPins[4] = {3, 4, 5, 6}; // DIP switches connected to pins D3, D4, D5, D6
// Create display object
Adafruit_7segment display = Adafruit_7segment();
// Variables to hold the timer value and state
unsigned long timerValue = 0;
unsigned long previousMillis = 0;
bool timerRunning = false;
bool displayBlinkState = false;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set the button as an input with an internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set the LED as an output
pinMode(buzzerPin, OUTPUT); // Set the buzzer as an output
// Initialize the DIP switch pins
for (int i = 0; i < 4; i++) {
pinMode(dipPins[i], INPUT_PULLUP);
}
// Initialize the display
display.begin(0x70); // Pass in the address
display.clear(); // Clear the display
display.writeDisplay();
// Read the initial timer value from the DIP switches
timerValue = readDipSwitches();
displayTimer(timerValue, true); // Display the initial timer value
}
void loop() {
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
delay(200); // Debounce delay
if (!timerRunning) {
timerRunning = true;
timerValue = readDipSwitches(); // Update the timer value in case the DIP switches changed
previousMillis = millis(); // Reset the timer
displayTimer(timerValue, false); // Update display without blinking decimal
} else if (timerValue == 0) {
// Restart the timer if it has finished counting down
timerRunning = false;
display.clear();
display.writeDisplay();
}
}
// Timer logic
if (timerRunning && timerValue > 0) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
timerValue--;
displayTimer(timerValue, displayBlinkState);
displayBlinkState = !displayBlinkState; // Toggle the blink state
}
} else if (timerValue == 0 && timerRunning) {
// Timer has reached 0, blink the display, LED, and buzzer
blinkAll();
}
}
// Function to read the DIP switch settings and calculate the timer value
unsigned long readDipSwitches() {
unsigned long value = 0;
for (int i = 0; i < 4; i++) {
if (digitalRead(dipPins[i]) == LOW) { // Active LOW switches
value += (1 << (3 - i)) * (i == 3 ? 1000 : 1); // Correctly calculate the value based on switch position
}
}
return value;
}
// Function to display the timer value on the 7-segment display
void displayTimer(unsigned long value, bool showDecimal) {
display.clear();
// Print the value in decimal format with leading zeros
if (value < 10) {
display.print("000");
display.print(value);
} else if (value < 100) {
display.print("00");
display.print(value);
} else if (value < 1000) {
display.print("0");
display.print(value);
} else {
display.print(value);
}
display.writeDisplay();
// Blink the right-most decimal point
if (showDecimal) {
display.drawColon(false);
uint8_t led = display.displaybuffer[4]; // Get the current state of the last digit
display.writeDigitRaw(4, led | 0b10000000); // Turn on the decimal point
display.writeDisplay(); // Update the display with the decimal point on
}
}
// Function to blink "0000" on the display, the LED, and beep the buzzer
void blinkAll() {
displayBlinkState = !displayBlinkState;
if (displayBlinkState) {
// Turn on the LED and buzzer
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
// Display "0000" with no decimals
display.print("0000");
display.writeDisplay();
} else {
// Turn off the LED and buzzer
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
// Clear the display
display.clear();
display.writeDisplay();
}
}
This code is designed to run on the Arduino UNO microcontroller. It initializes the connected components, reads input from the tactile