The following shows some example small phatIO config to do useful things.
Each of the code listings below can be copied into the file
PHATIO/io/run.lio on your phatIO device. Or open the file in a text editor and paste and save.
(keyboard "hello world")
The following changes the state of an pin 10 every 500ms (so it flashes on for half a second and then off for half a second)
(defconst ledpin 10) (pinmode ledpin OUTPUT) (defvar state 0) (every 500 (setpin ledpin state) (= state (~ state)) )
ledpin is set as a constant for the pin the LED is attached to - 10, its then set as an OUTPUT. A variable (
state) is defined to hold the current value of the pin. Every 500 milliseconds the code is called to set the ledpin to the current state and then toggle the state with the binary negation operator (~1 equals 0 and ~0 equals 1).
Note there’s no loop or delay functionality in phatIO, everything is event or schedule driven. We could blink the onboard LEDs at different rates with the following:
(defvar g 0) (every 100 (led GREEN g) (= g (~ g)) ) (defvar r 0) (every 200 (led RED r) (= r (~ r)) )
After setting the built in LEDs on or off they won’t respond in their normal way to file read and writes, reset them with the following:
(led GREEN) (led RED)
The following will cycle an LED connected to pin 10 through the range of PWM values (0 through 255 and then back to 0).
Every 5 milliseconds it sets the pin to the pwm value (stored in variable
value), then sets the value back to 0 if it equals the maximum - 255, or increments it by 1:
(pinmode 10 PWM) (defvar value 0) (every 5 (setpin 10 value) (if (eq value 255) (= value 0) (+= value 1)) )
See the ideas for more example code.