Code:
; Nano28_40 version. This will NOT work with a Nano18!
;;; 081910 - EDIT had to update LCD pinouts syntax, added servos
; Wiring: (If you are using a Nano Development Board, refer to data sheet diagram)
; P0, P1, P4 to P7 - LCD
; P3 - LCD Backlight
; P8 - Servo 1
; P9 - Servo 2
; P10 - SPK1 (speaker), install 1uF - 10uF capacitor between P10 and speaker!
; P11 - SW2 (button1)
; P12 - SW3 (Button2)
; P13 - SW4 (Button3)
; P14 - LED1
; P15 - LED2
; P30 - POT2 (for Nano40), P17 for Nano28
; P31 - POT1 (for Nano40), P18 for Nano28
;Setup 32 bit variable
audio var long
temp var long
temp2 var long
cnt var long
cnt=0
; Set floating point variable
volume var float
oldvolume var float
; Set Servo position values
SERV1POS var word
SERV2POS var word
SERV1POS = 1800 ; 1800 seemed a safe value to start at, your position values may be different
SERV2POS = 1800
; Setup LCD
lcdinit p0\p1\p7\p6\p5\p4
lcdwrite p0\p1\p7\p6\p5\p4,[CLEARLCD,HOMELCD,SCR,TWOLINE,"Hello World!"]
PAUSE 500
lcdwrite p0\p1\p7\p6\p5\p4,[CLEARLCD,HOMELCD, "POT1 POT2"]
;Set pins to known state
low p15
low p14
high p3 ; turns on LCD backlight, not available on Nano Dev Board
; Set servos at HOME position
SERVO 8, 0
SERVO 9, 0
;Enter main program. Read adin pins, load values into variables
main
IF cnt = 0 then
SEROUT s_out, i9600, [0] ; clear terminal display
ENDIF
; Set servos full CW
SERVO 8, SERV1POS
SERVO 9, SERV2POS
cnt=cnt+1
adin P18, temp ; use P31 for Nano40, P18 for Nano28 (controls frequency at speaker)
output P3
adin P17, temp2 ; use P30 for Nano40, P17 for Nano28 (controls speaker volume, LCD contrast)
; Set volume level of speaker based on potientometer value
volume = 1.0 - (TOFLOAT(temp2)/1024.0)
; Put tone out on speaker, frequency and volume based on pot1 and -2 settings
if(TOINT(oldvolume<>volume) OR temp<>audio)then
audio = 1023 - temp ; As temp rises and falls, so does the frequency
hpwm 0,audio*16,TOINT(TOFLOAT(audio*8)*volume)
oldvolume = volume
endif
;Write value to LCD
lcdwrite p0\p1\p7\p6\p5\p4,[SCRRAM+40,dec4 temp\4," ",dec4 temp2\4]
;Send analog and volume values to serial port
serout S_OUT,i9600,["POT1: ", dec temp," POT2: ",dec temp2," VOLUME: ",real volume, " CNT: ", dec CNT, 13]
;Button switch state is loaded to LED (output pin).
out15=in13 ; SW4 controls LED1
out14=in12 ; SW3 controls LED2
out3=in11 ; press SW2, turn off LCD backlight
; Set servos full CCW
SERVO 8, -(SERV1POS)
SERVO 9, -(SERV2POS)
goto main