



symbol Sw_Mode	=	pin3	;write switch
 
;-----------------------------------------------------------------------------------------


;---------------- Vars -----------------
;------ Words ------ 

symbol	working =	w0	;b0,b1
symbol	note =	w1	;b2,b3
symbol	freq1 =	w2	;b4,b5
symbol	length =	w3	;b6,b7
symbol	length_max =w4	;b8,b9
symbol	sweep_max =	w5	;b10,b11
symbol	sweep	=	w6	;b12,b13
symbol	note_max =	w7	;b14,b15

;------ bytes ------ 
symbol	i	=	b18
symbol	chop_max =	b19
symbol	chop =	b20
symbol	sweep_inten=b21
symbol	working_key=b22
symbol	mode =	b23	
symbol	key =		b24
symbol	lock	=	b25
symbol	a_value =	b26

setfreq M32
mode 		= 1
length_max 	= 240 
sweep_max 	= 0
sweep_inten = 300
freq1 	= 0
chop_max 	= 255
readadc 2,a_value
lock = a_value

Main:

readadc 2,a_value

readadc 1,key

;------------------------ mode select -------------------
if Sw_Mode = 1 then 
	mode = mode +1 
	if mode = 6 then : mode = 1:endif
	
	for working = 1 to mode
	high 4
	pause 1000
	low 4
	pause 500
	next working
endif	
;--------------------------------------------------------
;for i = 1 to 4

;-------------- mode working ---------------------------------	
	if a_value <> lock then	
		if mode = 1 then			;-- mode 1 frequency	
			freq1 = a_value * 4
			freq1=1025-freq1
			freq1=freq1*2
		endif
		
		if mode = 2 then			;-- Mode 2 sweep size
			sweep_max = a_value * 4
			sweep_max = 1025 - sweep_max
			sweep_max = Sweep_max * 8			
		endif
	
		if mode = 3 then			;-- mode 3 sweep intensity
			sweep_inten = a_value *2		 	
		endif
		
		if mode = 4 then			;-- mode 4 chopper intensity
			chop_max = a_value 
			if chop_max = 0 then : chop_max = 1 :endif		 	
		endif
		
		if mode = 5 then			;-- Mode 5 note length
			length_max = a_value 
			if length_max = 0 then : length_max = 1 : endif
			if length_max > 252 then :length_max = 64000	:endif
		endif
	endif
;-----------------------------------------------------------------


 

;--------------------- led flash -----------------------------------------
;	if led_flash = led_stop then 
;		high 4
;		led_stop = mode * 30		 
;		led_flash = 0
;			if Led_break = mode then : led_break =0: led_stop =250:endif
;		inc led_break
;	 
;	endif
;
;	inc led_flash
;-------------------------------------------------------------------------



 if key < 5 then: key = working_key ;this happens when we dont hit a new note
 else length = 0;new note
 pauseus 1
 endif
 
 
 	if key < 19      then: note =  1120    ; Note C 69		10 and key>8 < 19
 	else if key <60  then: note = 1370    ; Note B 69		23 and key < 60
 	else if key <100 then: note = 1450    ; Note Bb 69		80 and key < 100
 	else if key <135 then: note = 1530    ; Note A 69 *	128 and key < 135
 	else if key <160 then: note = 1630    ; Note Ab 69		141 and key < 160
 	else if key <200 then: note = 1740    ; Note G 69 *	174 and key < 200
   	else if key <230 then: note = 1855    ; Note F#		210 and key < 230
 	else if key <234 then: note = 1985    ; Note F 69		233 and key < 234
 	else if key <240 then: note = 2150    ; Note E 69 *	236 and key < 240
 	else if key <246 then: note = 2305    ; Note Eb 69		242 and key < 246
 	else if key <249 then: note = 2495    ; Note D 69 *	247 and key < 249
 	else if key <252 then: note = 2695    ; Note C#		250 and key < 252
	;else if key >252 then: note = 2900    ; Note C 61 *	253
	else note =2695
	endif  
	
      if key <> working_key  and length = 0 then : sweep = 0 :endif ; lets us reset sweep with new note

	
 	
	note_max = sweep_max + note ;value we let note get to in the sweep
	sweep = sweep + sweep_inten ;get new sweep value
	
	
	if length < length_max	then 
		
		if chop > chop_max then
			chop = 0
			;pause 10
		else
			note = note + freq1 + sweep
			Pulsout 0,note
			inc length
			if note > note_max then : sweep = 0:endif ;reset sweep if we hit max value
			
		endif ;play note	
	endif
	
	inc chop
	
	
	
		
	lock = a_value ; keep track if we moved the rotory control
	working_key = key ;keep track if a new or no key is presses
;	 next i
 goto Main

