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
      • Scamp2 UART Pins
    • I2C >
      • I2C Overview
      • I2C Commands
    • SPI
    • PWM
    • Peripheral Pin Select
    • Input Capture
    • Digital Signal Modulator
  • Create
    • Creating PCBs >
      • Designing PCBs
      • Fabricating PCBs
      • Soldering
    • Sensing >
      • Measuring Temperature
    • LEDs >
      • Adding LEDs
      • LED arc-welder effect
    • Displays >
      • Adding a 7 Segment Display
      • Adding a Touch LCD
      • Touch LCD GUI
      • Game of Pong
    • Adding Extra GPIO
    • Adding a Low Side Switch
    • FizzBuzz
    • Adding MRAM
    • Model Train Control
    • Adding a Real Time Clock
    • Scamp Projects on Youtube
  • Resources
  • Store
  • About
  • Contact

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
Picture
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.
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. 

​: 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
      • Scamp2 UART Pins
    • I2C >
      • I2C Overview
      • I2C Commands
    • SPI
    • PWM
    • Peripheral Pin Select
    • Input Capture
    • Digital Signal Modulator
  • Create
    • Creating PCBs >
      • Designing PCBs
      • Fabricating PCBs
      • Soldering
    • Sensing >
      • Measuring Temperature
    • LEDs >
      • Adding LEDs
      • LED arc-welder effect
    • Displays >
      • Adding a 7 Segment Display
      • Adding a Touch LCD
      • Touch LCD GUI
      • Game of Pong
    • Adding Extra GPIO
    • Adding a Low Side Switch
    • FizzBuzz
    • Adding MRAM
    • Model Train Control
    • Adding a Real Time Clock
    • Scamp Projects on Youtube
  • Resources
  • Store
  • About
  • Contact