Question: How can i get the code below to have one light remain on continuosly until the button is pressed and then the first led will

How can i get the code below to have one light remain on continuosly until the button is pressed and then the first led will shut off, and the second will turn on and with the button being pressed again it will do the opposite?

const int BUTTON_PIN = 2; // Pin number for the push button

const int LED_PIN_1 = 3; // Pin number for the first LED

const int LED_PIN_2 = 4; // Pin number for the second LED

void setup() {

// Set the button pin as input and enable the internal pull-up resistor

DDRD &= ~(1 << BUTTON_PIN);

PORTD |= (1 << BUTTON_PIN);

// Set the LED pins as outputs

DDRD |= (1 << LED_PIN_1) | (1 << LED_PIN_2);

// Turn on the first LED and turn off the second LED

PORTD |= (1 << LED_PIN_1);

PORTD &= ~(1 << LED_PIN_2);

}

void loop() {

// Check if the button is pressed

if (!(PIND & (1 << BUTTON_PIN))) {

// Toggle the state of the LEDs

PORTD ^= (1 << LED_PIN_1) | (1 << LED_PIN_2);

// Wait for a short period to debounce the button

delay(1000);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!