'Program Clap Music
main:
			low 4				     'Turn on start up light RED LED
			wait 40				  'Wait 40 seconds for circuit to settle 
lbl_start:	  readadc  1,b0		      'Read audio and store in variable b0
lbl_2read:	pause 20				'Pause for .02 seconds
			readadc 1,b1			'Read audio and store in variable b1
			if b0=b1 then good_data     'If b0 is equal to b1 goto good_data
			if b0>b1 then lbl_reverse     'Reverse subtraction if b0 is greater than b1
			let b2=b1-b0			 'Get difference between b0 and b1
lbl_check:	if b2> 5 then try_again	  'If too large, out of window, goto try_again
good_data:     let w6 = w6 + b0	   	'If good add it to running total
			let b3=b3+ 1		  	'Increase reading counter by 1
			if b3> 9 then lbl_settings	'If reading counter is 10 then goto lbl_settings 
			let b0=b1				'If reading counter less than 10 let b0 = b1
			goto lbl_2read			  'Go get new b1 reading

lbl_reverse:     let b2=b0-b1			  'Reverse the subtraction order, b0 minus b1
			goto lbl_check			  'Goto check for good data line

lbl_settings:     let b10 = w6/ 10	    	'Get average for quiet reading
			let b11=b10+ 15		    'Set upper start level at 15 above quiet
			let b12=b10- 15		     'Set lower start level at 15 below quiet
			let b8=b10+ 5			'Set upper quiet level at 5 above average
			let b9=b10- 5			 'Set lower quiet level at 5 below average
			high 4				       'Turn off start up light
lbl_back2:	let b6= 0				 'Set 2.5 second counter to zero
lbl_loop:	  setint  2, 2			       'Set interupt for pin 1 high
			pause 10				 'Wait for 10 milliseconds
			inc b6				       'Increment 2.5 second counter by 1
			if b6> 254 then lbl_time	 'If counter is past 2.54 seconds goto lbl_time
			goto lbl_loop			    'Loop back and look for interupt

lbl_time:	   let b7= 0			 	'Reset clap counter 
			goto lbl_back2		 	'Goto start of new 2.54 second count & look for clap

try_again:	let w6=0				'Reset average sum to zero
			let b3= 0				 'Reset average counter to zero
			goto lbl_start			  'Start looking for quiet time average again

interrupt:	
			for b0=1 to 9		  	'Set up a loop to look at 9 start data points
				readadc  1,b13      	'Read the data point and store it in b13
				if b13>b11 then clap_ok	'Does it exceed upper start level? yes-goto clap ok
				if b13<b12 then clap_ok	'Does it exceed lower start level? yes-goto clap ok
			next b0			    	'If not out of window, take another reading
lbl_return:	return				      'If all start points fail return to look for clap program

clap_ok:	   let b7=b7+ 1			   'Increment clap counter
			pause 200			 	'Pause for 200 milliseconds
			for b0=1 to 9		   	 'Set up a loop to look at 9 quiet points
				readadc  1,b13		'Read quiet point
				if b13>b8 then lbl_noisy	'If out of quiet window goto noisy
				if b13<b9 then lbl_noisy	'If out of quiet window goto noisy
			next b0				     'Get next data point
lbl_test1:	 if b7> 1 then lbl_toggle	    'If second clap is good goto toggle, b7>2 would require 3 claps
			let b6= 0				   'Reset 2.54 second counter to 0
			goto lbl_return			  'Return and look for claps

lbl_toggle: 
			If b4 > 3 then			  'If 5th song play Loony Tunes
				tune 2, 5,($6C,$00,$45,$44,$42,$40,$29,$40,$45,$44,$42,$43,$04,$44,$44,$40,$42,$40,$44,$40,$42,$69,$40,$67,$6A,$69,$65)
				b4 = 0
				else

				select case b4                 'Play song
				case 0
				    play 0,3
				case 1
				    play 1,3 
				case 2
				    play 2,3 
				case 3
				    play 3,3
				end select
				inc b4				'Move to next song
			endif
			high 4					'Turn LED on pin 4 off
			let b6= 254			       'Set counter to max-1
			goto lbl_return			  'Return

lbl_noisy:	 let b7= 0				   'Set good clap counts to zero
			goto lbl_test1			    'Goto test for toggle




