Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

Identify some of the global differences when negotiating.

Answered: 1 week ago