2.1 Lights and Buttons

In this challenge, we’ll involve some electronic devices that are connected to our computer. We’ll try to build something fun out of an LED light and a button that can also shine in any color.

Make a copy of the application template for each of the tasks below and write the necessary code with JavaScript and HTML to solve the tasks below. Make sure you connected the hardware kit to your computer's USB port, and you tested the correct functionality of the devices with the Brick Viewer.

a) Light up the LED

We start easy! When the user presses the button on the website, make the LED light up in a green color! Next, change the program, so it lights the LED in your favorite color!

Solution:

Now, make the LED blink red when the user presses the button on the website. Let the user enter the frequency in milliseconds. Add a button to stop the LED from blinking.

Solution:

c) Initialize the LEDs on startup

On startup, set the hardware button’s LED to white and turn the other LED off!

Solution:

d) Hardware button activates the LED

Make the LED light blue when a user presses the hardware button.

Solution:

e) Hardware button toggles the LED

Now, when the user presses the hardware button again, turn the LED light off. Basically, make the button a switch for the LED light.

Solution:

f) Random colors

Enhance the program from e) and let the LED change randomly between the colors green, red, blue, and orange when a user presses the hardware button. (The button doesn’t turn the LED off anymore, it changes the light’s color randomly).

Solution:

g) Sequence of colors

Change the program from f) and, which each button press, change the LED's color in a defined sequence of white, yellow, green, blue, and red. The change of color should no longer be random.

Solution:

Last updated

Was this helpful?