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. 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
This word will read the temperature sensor and use the LEDs as a thermometer scale, for temperatures between 20 and 35 degrees Celsius:
: thermo
begin
1 \ this gives a bit to turn a LED on
temp drop \ read temp and discard fraction
20 - \ make 20 degrees our baseline
for
2 * \ move the bits to the left for each degree
1+ \ make it a bar graph
next
leds \ and display the bit
key? until \ loop until a key is pressed
;
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
;