This is the toaster oven temperature profile controller
example:
(Please note that as of v1.40, a sleep unit specifier --
s,
ms, or
us -- was
added to the sleep
statement.)
(Please note that as of v1.84, the units of servo output pins was changed from
centi-milliseconds (cms) to microseconds (us).)
10 dim target, secs
20 dim thermocouple as pin an0 for analog input
30 dim relay as pin an1 for digital output
40 data 512, 90, 746, 105, 894, 20, -1, -1
50 configure timer 0 for 1000 ms
60 on timer 0 do gosub adjust
70 while target!=-1 do
80 sleep secs s
90 read target, secs
100 endwhile
110 let relay = 0
120 end
130 sub adjust
140 if thermocouple>=target then
150 let relay = 0
160 else
170 let relay = 1
180 endif
190 endsub
This is
the LCD digital thermometer, displaying both Celsius and Fahrenheit:
(Please note that as of v1.40, a sleep unit specifier --
s,
ms, or
us -- was
added to the sleep
statement.)
(Please note that as of v1.84, the
units of servo output pins was changed from centi-milliseconds (cms) to microseconds
(us).)
10 dim temp, line1$[32], line2$[32],
blink$[2]
20 let blink$ = " *"
30 gosub initdisplay
40 while 1 do
50 gosub gettemp temp
60 vprint line1$ = temp, "degrees C"
70 vprint line2$ = temp*9/5+32, "degrees F", blink$[seconds%2:1]
80 gosub display line1, line2
90 sleep 500 ms
100 endwhile
110 end
120 rem --- gettemp ---
130 sub gettemp temp
140 dim cmd as byte, rsp[2] as byte
150 let cmd = 0
160 i2c start 0x48
170 i2c write cmd
180 i2c read rsp
190 i2c stop
200 let temp = rsp[0]
210 endsub
220 rem --- display ---
230 sub display line1, line2
240 dim cmd1 as byte, data as byte, cmd2 as byte
250 let cmd1 = 0x80, data = 0x2, cmd2 = 0x40
260 i2c start 0x3c
270 i2c write cmd1, data, cmd2, line1
280 i2c stop
290 let cmd1 = 0x80, data = 0xc0, cmd2 = 0x40
300 i2c start 0x3c
310 i2c write cmd1, data, cmd2, line2
320 i2c stop
330 endsub
340 rem --- initdisplay ---
350 sub initdisplay
360 dim i, init[10] as byte
370 for i = 1 to init#
380 read init[i-1]
390 next
400 i2c start 0x3c
410 i2c write init
420 i2c stop
430 sleep 100 ms
440 endsub
450 data 0, 0x38, 0x39, 0x14, 0x78, 0x5e, 0x6d, 0xc, 0x1, 0x6
This example implements a
wireless
remote LED dimmer,
using two CPUStick and the 2.4GHz zigflea wireless transport from two IOSticks.
(Please note that as of v1.40, a sleep unit specifier --
s,
ms, or
us -- was
added to the sleep
statement.)
(Please note that as of v1.84, the
units of servo output pins was changed from centi-milliseconds (cms) to microseconds
(us).)
On nodeid 1, which is the node connected to the potentiometer:
10 dim potentiometer as pin an0 for
analog input
20 dim led as remote on nodeid 2
30 while 1 do
40 let led = potentiometer
50 sleep 100 ms
60 endwhile
On nodeid 2, which is the node connected to the LED:
10 dim led as pin dtin0 for analog
output
20 while 1 do
30 endwhile
Just for fun, this example runs a single-player
accelerometer-based pong/paddleball game with sound effects on the Badge Board!
(Please note that as of
v1.40, a sleep unit specifier -- s,
ms, or
us -- was
added to the sleep
statement.)
(Please note that as of v1.84, the
units of servo output pins was changed from centi-milliseconds (cms) to microseconds
(us).)
10 dim x, y, xv, yv, p
20 rem --- interface to the accelerometer ---
30 dim gsel1 as pin ptc4 for digital output
40 dim gsel2 as pin ptc5 for digital output
50 dim sleep as pin ptc3 for digital output inverted
60 dim tilt as pin ptb5 for analog input inverted
70 let gsel1 = 0, gsel2 = 0, sleep = 0
80 rem --- interface to the buzzer ---
90 dim audio as pin ptf2 for frequency output
100 rem --- timer to update paddle position ---
110 configure timer 1 for 50 ms
120 on timer 1 do gosub paddle
130 rem --- main program loop ---
140 while 1 do
150 rem --- new game ---
160 let x = 0, y = 0, xv = 1, yv = 1, p = 4
170 jmclear -1, -1
180 jmset y, x
190 sleep 3 s
200 rem --- main game loop ---
210 while 1 do
220 rem --- clear old ball position ---
230 jmclear y, x
240 rem --- integrate the new ball position ---
250 let x = x+xv, y = y+yv
260 rem --- if we hit an edge ---
270 if x==0||x==15 then
280 rem --- bounce horizontal ---
290 let xv = -xv
300 endif
310 rem --- if we hit the bottom ---
320 if y==4 then
330 rem --- if the paddle was nowhere close
---
340 if x<p-2||x>p+2 then
350 rem --- you lose ---
360 gosub lose
370 break
380 elseif x<p-1&&xv==1||x>p+1&&xv==-1 then
390 rem --- we hit the edge of
the paddle ---
400 rem --- bounce horizontal
---
410 let xv = -xv
420 endif
430 endif
440 rem --- if we hit the bottom or top ---
450 if y==0||y==4 then
460 rem --- bounce vertical ---
470 let yv = -yv
480 endif
490 rem --- set new ball position ---
500 jmset y, x
510 rem --- beep and delay ---
520 let audio = 100
530 sleep 100 ms
540 let audio = 0
550 sleep 100 ms
560 endwhile
570 endwhile
580 sub paddle
590 rem --- clear the old paddle position ---
600 jmclear 4, p-1
610 jmclear 4, p
620 jmclear 4, p+1
630 rem --- integrate the new paddle position from accelerometer ---
640 let p = p+(tilt-1650)/100
650 rem --- bound us by the screen ---
660 if p<1 then
670 let p = 1
680 elseif p>14 then
690 let p = 14
700 endif
710 rem --- set the new paddle position ---
720 jmset 4, p-1
730 jmset 4, p
740 jmset 4, p+1
750 endsub
760 sub lose
770 rem --- hold off timer interrupts ---
780 mask timer 1
790 rem --- scroll the message ---
800 jmclear -1, -1
810 jmscroll "you lose"
820 rem --- play a dropping scale ---
830 let audio = 5000
840 do
850 sleep 50 ms
860 let audio = audio*11/12
870 until audio<400
880 let audio = 0
890 rem --- wait for the message to scroll ---
900 jmscroll " "
910 rem --- resume a new game ---
920 unmask timer 1
930 endsub