After we know how to light up an external LED, we will learn how to use the button to control the LED as well as make a notification light controlled by a knob. In fact, notification light is very common in our daily life. A good example is the signal lights on the mobile phones. Now, let us learn more about it!
Connect the button module (knob module) to P0 of the expansion board Connect the LED module to P1 of the expansion board
Program Task1: button controlled lamp Function: when the button is pressed, the LED module turns on. When the button is released, the LED turns off.
STEP 1: The button is connected to pin P0. The status of whether the button is pressed or not can be detected by ready the status of the pin P0. When the button is pressed, set the value of the pin as 1 and the LED turns on. When released, set the value of the pin as 0 and the LED turns off. This button module belongs to digital input and it can be found under “Advance" ->"Pins".
STEP2: The condition operator "=" under “Logic” is used to determine whether the button is pressed or not. If the status of the button equals 1, then we will know the button is being pressed, vice versa.
STEP3: If the button is pressed, the external LED lights up; otherwise, it stays off. We will use another condition operator, the “if-else” module. The module is also under “logic”
STEP4: By putting together all the modules mentioned as above, we will have the following program. What the program does is to detect the status of the button, and lights up the LED when the button is pressed.
Task 2； Knob controlled LED
Function: in the part, we will not only control the led from on to off, but use the knob to change its brightness. STEP 1: connect the circuit as below. The button module used in task1 is now replaced by the knob module.
STEP 2: The brightness of the LED is determined by the input value of the knob. The knob is an analog input module, which means it genets a signal range from 0 - 1023, by which controls the brightness of the LED correspondingly. the “analog read” function is under “Pins”. Also, remember to set the pin to P0.
STEP 3: The LED is now used as an analog output module. Similarly, the analog output signal is also ranged from 0-1023. "analog write" function can be found under “Pins”
STEP 4：Now, all what we need is to link the input directly to the output so that the LED will be under control of the knob. Put the “analog read” function inside the “analog write” function, then put them all inside the forever loop, and we are done. The final program is shown as bellow.