FADING LED


08. Fading LED

The first example of analog output uses a LED to realize a fading LED. It can be managed from the Internet via start and stop commands.

Figure 1 - LED circuit

1. Circuit

Connect the LED as shown in figure 1:

  • positive PIN to the PIN d2
  • negative PIN to the PIN GND (Ground).

The long PIN is positive as shown in Figure 2.

2. Sketch

Write a sketch K to define:

  • the output connection: led::d2
  • the commands to control the LED: start, stop
led :: d2   
d = 50   
start = [led=led+d#,led=0]   
stop = [led=0]   
led==0 -> d=50   
led==1000 -> d=-50   

or simply load the sketch with the command

] load e08_fading_led

3. Try and learn

Try the following commands now:

  • ] start

the brightness of the LED grows (fade in) and decreases (fade out)

  • ] pause

the fading of the led stops

  • ] play

the fading of the led resumes

  • ] ??
--- Periodic commands
led=led+d #

shows the periodic commands running

  • ] trace=1
ok
] 
# led=led+d
...
]
# led==0 -> d=50
# d=50
]
# led=led+d
...
] 
# led==1000 -> d=-50
# d=-50
] 
# led=led+d
...

traces the commands running

  • ] trace=0

ends the tracing

  • ] stop

turns off the led and stops the fading of the led

  • ] ??
null

stop command also clears the commands running.

Try replacing the LED with a BUZZER connected to d2. What happens?

4. Summarizing

In this lesson you learned:

  • how to use a LED
  • the commands: load, start, pause, play, ??, trace, stop.