udamonic.com
  • Start
    • What is a Scamp?
    • Getting Started
    • Getting Started (Windows)
  • Learn
    • What is Forth?
    • Arithmetic
    • The Stack
    • Creating Words
    • Compilation Tricks
    • Decompilation
    • Comments
    • Characters and Strings
    • Accessing Memory
    • Constants, Variables and Values
    • Flow Control
    • Loops
    • Data Structures
    • Doubles, Triples and Quads
    • FlashForth Dictionary Reference
    • Scamp Dictionary Reference
  • Extras
    • Interrupts
    • Timers
    • Multitasking
    • Delays and Tick Count
    • Processor Words
    • Starting up with turnkey
    • Changing the Prompt
    • Formatting Text on Screen
  • Interfacing
    • Using the LED Array
    • Temperature Sensor
    • Input/Output
    • Analog Input
    • Serial >
      • Serial Communication
      • Scamp Serial Comms
      • Scamp2 UART Pins
    • I2C >
      • I2C Overview
      • I2C Commands
    • SPI
    • PWM
    • Peripheral Pin Select
    • Input Capture
    • Digital Signal Modulator
    • DataFlash
    • RS485
  • Create
    • Scamp Projects on Youtube
    • Prototyping >
      • Protoboards
      • Fabricating PCBs
      • Soldering
    • GPIO >
      • Adding Extra GPIO
      • 32-bit GPIO Module
      • Adding a Low Side Switch
    • Sensing >
      • Measuring Temperature
    • LEDs >
      • Adding LEDs
      • LED arc-welder effect
    • Memory >
      • Adding MRAM
      • AT24C256 EPROM Driver
    • Displays >
      • Adding a 7 Segment Display
      • Adding a Touch LCD
      • Touch LCD GUI
      • Game of Pong
    • FizzBuzz
    • Model Train Control
    • Adding a Real Time Clock
    • 1-D Pacman
  • Resources
  • Buy
  • Consulting
  • About

Using with the LED Array
​

Picture
In addition to an amber status LED, the Scamp2, Scamp3 and Scamp3e have a bank of 16 red LEDs that can be accessed using the leds word. (Note that the LED array is not available on the Scamp2e.) 

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
​

Note: this word is included in the Scamp dictionary from v1.8 onwards.
The following word definition uses the LED array as a bar graph:
: bars     \ for Scamp2 and Scamp3
​           \ Scamp3e has this built in
      dup
      0=
      if
         leds
      else
         1
         swap
         1-
         for
           2 * 1+
         next 
         leds
      then
;
You can use bars to display a value on the stack (between 0 and 16) to the LEDs. 

As an example, let's use bars 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 bars.
sample
#​255 u/ bars
A simple countdown using bars: 
: countdown
   16 for
      i bars   \ 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!)
      bars
   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. 

​: blinken
  begin
     random leds
     blink
  key? until
  0 leds
;
​
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
;

Site powered by Weebly. Managed by Hostwinds
  • Start
    • What is a Scamp?
    • Getting Started
    • Getting Started (Windows)
  • Learn
    • What is Forth?
    • Arithmetic
    • The Stack
    • Creating Words
    • Compilation Tricks
    • Decompilation
    • Comments
    • Characters and Strings
    • Accessing Memory
    • Constants, Variables and Values
    • Flow Control
    • Loops
    • Data Structures
    • Doubles, Triples and Quads
    • FlashForth Dictionary Reference
    • Scamp Dictionary Reference
  • Extras
    • Interrupts
    • Timers
    • Multitasking
    • Delays and Tick Count
    • Processor Words
    • Starting up with turnkey
    • Changing the Prompt
    • Formatting Text on Screen
  • Interfacing
    • Using the LED Array
    • Temperature Sensor
    • Input/Output
    • Analog Input
    • Serial >
      • Serial Communication
      • Scamp Serial Comms
      • Scamp2 UART Pins
    • I2C >
      • I2C Overview
      • I2C Commands
    • SPI
    • PWM
    • Peripheral Pin Select
    • Input Capture
    • Digital Signal Modulator
    • DataFlash
    • RS485
  • Create
    • Scamp Projects on Youtube
    • Prototyping >
      • Protoboards
      • Fabricating PCBs
      • Soldering
    • GPIO >
      • Adding Extra GPIO
      • 32-bit GPIO Module
      • Adding a Low Side Switch
    • Sensing >
      • Measuring Temperature
    • LEDs >
      • Adding LEDs
      • LED arc-welder effect
    • Memory >
      • Adding MRAM
      • AT24C256 EPROM Driver
    • Displays >
      • Adding a 7 Segment Display
      • Adding a Touch LCD
      • Touch LCD GUI
      • Game of Pong
    • FizzBuzz
    • Model Train Control
    • Adding a Real Time Clock
    • 1-D Pacman
  • Resources
  • Buy
  • Consulting
  • About