This circuit is designed to control an LED using a push button and a rocker switch. The Arduino Nano microcontroller is used to manage the input from the push button and rocker switch and control the LED accordingly. When the rocker switch is active, pressing the push button will turn on the LED for a specified interval (10 seconds). If the rocker switch is inactive, the LED remains off regardless of the push button state.
Arduino Nano
2Pin Push Switch
LED: Two Pin (red)
Resistor
Rocker Switch
const int buttonPin = 3; // Pin connected to the push button
const int ledPin = 4; // Pin connected to the LED
const int rockerPin = 9; // Pin connected to the rocker switch
unsigned long previousMillis = 0; // Stores the last time the LED was updated
const long interval = 10000; // Interval for the LED to stay on (10 seconds)
bool ledState = LOW; // Current state of the LED
bool circuitActive = false; // State of the circuit (active/inactive)
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(rockerPin, INPUT); // Set rocker switch pin as input
digitalWrite(ledPin, LOW); // Ensure LED is off initially
}
void loop() {
// Check the state of the rocker switch
circuitActive = digitalRead(rockerPin);
if (circuitActive) {
// If the circuit is active, check the button state
if (digitalRead(buttonPin) == LOW) {
// If the button is pressed (active low), reset the timer and turn on the LED
previousMillis = millis();
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
// Check if the LED should be turned off
if (ledState == HIGH && (millis() - previousMillis >= interval)) {
ledState = LOW;
digitalWrite(ledPin, ledState);
}
} else {
// If the circuit is inactive, ensure the LED is off
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
This code initializes the pins for the push button, LED, and rocker switch. It continuously checks the state of the rocker switch and push button. If the rocker switch is active and the push button is pressed, the LED is turned on for 10 seconds. If the rocker switch is inactive, the LED remains off.