Using with the LED Array
In addition to an amber status LED, the Scamp2 has a bank of 16 red LEDs that can be accessed using the leds word. Place the value to be output on the stack, then call leds. (Remember that using binary or hex numbers will make more sense.)
$5555 leds will turn alternating LEDs on.
To turn all the LEDs off: 0 leds |
The LED array can be useful for debugging in your code. If you want to monitor values during debugging, you can echo those values to the LEDs. For example, you can display the current stack entry to the LEDs:
dup leds
You can use the LEDs to display the current value from an analog input:
sample leds
Using the LEDs as a Bar Graph
The following word definition uses the LED array as a bar graph:
: bar
dup
0=
if
leds
else
1
swap
1-
for
2 * 1+
next
leds
then
;
You can use bar to display a value on the stack (between 0 and 16) to the LEDs.
As an example, let's use bar to display a value from the ADC. The ADC resolution is (by default) 12 bits, which is a value in the range 0..4095. So 4095 divided by 16 is 255. Therefore, to display a value from the ADC to the LEDs as a bar graph, we simply do an unsigned divide on our sampled value by 255, and then call bar.
As an example, let's use bar to display a value from the ADC. The ADC resolution is (by default) 12 bits, which is a value in the range 0..4095. So 4095 divided by 16 is 255. Therefore, to display a value from the ADC to the LEDs as a bar graph, we simply do an unsigned divide on our sampled value by 255, and then call bar.
sample
#255 u/ bar
A simple countdown using bar:
: countdown
16 for
i bar \ display the loop count as it counts down
200 ms
next
;
Using the LEDs as a Visual Thermometer
This word will read the temperature sensor and use the LEDs as a thermometer scale, for temperatures between 20 and 36 degrees Celsius:
: thermo
begin
temp drop \ read temp and discard fraction
20 - \ make 20 degrees our baseline
\ (change this for colder climates!)
bar
key? until \ loop until a key is pressed
;
Blinking Lights
Alternatively, you can amuse family and pets by just playing with the LEDs.
This word will randomly toggle the LEDs like an old mainframe or mini computer, and will blink the amber status LED as well. The loop terminates when a key is pressed, and then the LEDs are turned off.
This word will randomly toggle the LEDs like an old mainframe or mini computer, and will blink the amber status LED as well. The loop terminates when a key is pressed, and then the LEDs are turned off.
|
|
This word will make the LEDs shuffle back and forward:
: shuffle
begin
$5555 leds
100 ms
$aaaa leds
100 ms
key? until
0 leds
;
To make a single LED scan left and right:
: left
#15 for
#30 ms
dup leds
#2 *
next
;
: right
#15 for
#30 ms
dup leds
#2 u/
next
;
: cylon
1
begin
left
right
key? until
drop
0 leds
;