dim i as integer
dim state as integer

dim n as integer 		'number of letters in our alphabet (currently 27)
dim v1 as integer		'getADC 13 // index finger 	(range = 0 to 12, starts @ 7)
dim v2 as integer 		'getADC 15 // middle finger
dim v3 as integer		'getADC 17 // thumb

dim strHello as string	'welcome message
dim str as string		'current letter
dim strThis as string  'current message + current letter
dim strFinal as string 'current message
dim strLast as string	'current message - 1 character (used for delete key)

dim c as integer 		'count (determins position in the alphabet)
dim d as single		'delay between characters on LCD
dim df as single		'speed of delay FAST
dim ds as single		'speed of delay SLOW	

dim mode as byte		'switch btw LCD and music

dim flex_abc_next as integer
dim flex_next_fast as integer
dim flex_abc_prev as integer
dim flex_prev_fast as integer
dim flex_return as integer
dim flex_del as integer
dim flex_clear as integer  

Sub Main()
	call delay(0.5)	
	
	strHello = "midterm time!"
	call LCDInit()	' initialize LCD		
	call LCDLine(1)	
	call LCDPutStr(strHello)
	call delay(2.0)
	
	call LCDInit()	'wipe LCD again
	strHello = ""
	call LCDLine(1)	
	call LCDPutStr(strHello)

	str = ""
	strThis = ""
	strFinal = ""
	strLast = "" 

	flex_abc_next  = 17		'index finger
	flex_next_fast = 20
	flex_abc_prev  = 14
	flex_prev_fast = 12
	flex_return    = 17		'middle finger
	flex_clear     = 4 
	flex_del       = 4			'thumb

	df = 0.2
	ds = 1.0

	c = 0 
	n = 27	
	d = ds
	
	if getPin(8) = 0 then 
		debug.print "1 | lcd"
		strHello = "Plan A: keyboard"
		
		call LCDInit()	' initialize LCD		
		call LCDLine(1)	
		call LCDPutStr(strHello)
		call delay(2.0)
		strHello = "               "
		call LCDInit()	' initialize LCD		
		call LCDLine(1)	
		call LCDPutStr(strHello)

		call Count()
	elseif getPin(8) = 1 then 
		debug.print "0 | audio"
		
		strHello = "Plan B:  audio"
		call LCDInit()	' initialize LCD		
		call LCDLine(1)	
		call LCDPutStr(strHello)
		call delay(2.0)
		strHello = "               "
		call LCDInit()	' initialize LCD		
		call LCDLine(1)	
		call LCDPutStr(strHello)

		call Audio()
		'call Piano
	end if
	
End Sub


Sub Count()
	do
		if getPin(8) = 1 then 
			call Main()
		end if 

		v1 = getADC(13) 	'index
		v2 = getADC(15)	'middle
		v3 = getADC(17)	'thumb
		
		'debug.print "flex1: " ; cstr(v1)	
		'debug.print "flex2: " ; cstr(v2)
		'debug.print "flex3: " ; cstr(v3)

		if v3 >= 3 then 			'if the thumb is bent
			if c = 27 then 		'if the user selects char 27 (*) then clear the LCD
				strFinal = "" 
			else
				strLast = strFinal
				strFinal = strFinal & str	'else, add letter to string
			end if
			
			call LCDLine(1)	
			call LCDPutStr(strFinal)
			
			debug.print "final: "; strFinal
		end if


		if v2 >= 17 then 			'if middle finger is down
			strFinal = strLast		'delete last character
			
			strHello = "               "	
			call LCDPutStr(strHello)
			call LCDPutStr(strFinal)
		end if


		if v2 <= 4 then 		'bend middle finger up and reset form
			strFinal = ""
			call Main()
		end if 

		'debug.print "final: "; strFinal

		if v1 >= 18 then			'count up (ABCDE)
			c = c + 1
			if c > n then 		'if we count PAST the numbers of chars in our alphabet, loop to the fist letter
				c = 0 
			end if
			
			if v1 >= 20 then		'controls speed of char scrolling 
				d = df			'fast
			else
				d = ds			'slow
			end if
	
		elseif v1 < 14 then			'count down (ZYXWV)
			c = c - 1
				if c < 0 then		'if we count BELOW the number of letters in the alphabet, loop to the last char
					c = n
				end if 	
		
			if v1 < 10 then		'controls speed of char scrolling 
				d = df		'fast
			else
				d = ds		'slopw
			end if
		end if

		'debug.print cstr(v1)
		'debug.print cstr(d)
		'debug.print "count: " ; cstr(c)		'show us the count
		Call LCD()		
	loop

End Sub


Sub LCD()
	'str = "dpstyles"
	
	if c = 0 then 
		str = "_"
	elseif c = 1 then 
		str = "A"
	elseif c = 2 then 
		str = "B"
	elseif c = 3 then 
		str = "C"
	elseif c = 4 then 
		str = "D"
	elseif c = 5 then 
		str = "E"
	elseif c = 6 then 
		str = "F"
	elseif c = 7 then 
		str = "G"
	elseif c = 8 then 
		str = "H"
	elseif c = 9 then 
		str = "I"
	elseif c = 10 then 
		str = "J"
	elseif c = 11 then 
		str = "K"
	elseif c = 12 then 
		str = "L"
	elseif c = 13 then 
		str = "M"
	elseif c = 14 then 
		str = "N"
	elseif c = 15 then 
		str = "O"
	elseif c = 16 then 
		str = "P"
	elseif c = 17 then 
		str = "Q"
	elseif c = 18 then 
		str = "R"
	elseif c = 19 then 
		str = "S"
	elseif c = 20 then 
		str = "T"
	elseif c = 21 then 
		str = "U"
	elseif c = 22 then 
		str = "V"
	elseif c = 23 then 
		str = "W"	
	elseif c = 24 then 
		str = "X"
	elseif c = 25 then 
		str = "Y"
	elseif c = 26 then 
		str = "Z"
	elseif c = 27 then 
		str = "*"
	else
		str = "(null)"
		debug.print cstr(c)
	end if

	'debug.print "char: "; str	'display what char the user selected
	
	strThis = strFinal & str	'the newly formed string (+ 1 new character)

	call LCDLine(1)	
	call LCDPutStr(strThis)
	call sleep(d)			'delay so the count doesn't move faster than the user can control || d conmtrol speed

End Sub


Sub Audio()
	call delay(0.1)

	dim v as integer  'voltage
	dim n as integer  'number

	n = 14 
	
	do
		if getPin(8) = 0 then 
			call Main()
		end if 


		v = getADC(13) 		'index finger
		'debug.print cstr(v)		

		if v >= n then 
			do while v >= n							
				call Meep()
			loop
		else
			do while v < n
				call Meep()
			loop
		end if

		'debug.print cstr(v)	
		'x = 220   			'starting note frequency
		'au = (x + (14 * v)) * o
		'call FreqOut(19,au,au,50)
	loop
End Sub


Sub Meep()
	dim v as integer  'voltage
	dim au as integer 'audio
	dim x as integer	'starting note (A,B,C...)
	dim o as integer  'audio octave (1,2,3...)
	dim i1 as integer	'counter 	
	dim i2 as integer	'counter 	
	dim t as integer	'time (for freqout)
	dim r as integer	'reset the note 

	n = 14			'midpoint for flex sensor

	v = getADC(13) 	'index fingah	

	o = getADC(15) 	'middle fingah   RANGE = dn 17 <-> 13 <--> 4 up
	'debug.print cstr(o)
	if o < 6 then 
		o = 2
	elseif o < 14 then 
		o = 5
	elseif o < 20 then 
		o = 10
	end if 

				
	r = getADC(17)	'thumb	RANGE = in 4 <--> 2 <--> 0 out
	'debug.print cstr(r)	

	
	x = 220 			'starting note
	
	if v >= n then 
		if r >= 3  then 
			i1 = 0 
		elseif  r <= 1 then 
			i1 = i1 * 10	
		else
			i1 = i1 + 1
		end if 
		au = (x + (14 * i1)) * 5
	else 
		if r >= 3  then 
			i2 = 0 
		elseif  r <= 1 then 
			i2 = i2 * 10	
		else
			i2 = i2 + 1
		end if 
		au = (x - (14 * i2)) * 5
	end if	

	call FreqOut(19,au,au,50)	't used to be 50 
	
	debug.print "r: " ; cstr(r)
	debug.print "i1: " ; cstr(i1)
	debug.print "i2: " ; cstr(i2)
	debug.print "o: " ; cstr(o)
	debug.print "au:" ; cstr(au)
	debug.print "*********"

End Sub 

Sub Piano()
	debug.print "piano"
	dim thu as integer		'thunb
	dim mdl as integer		'middle
	dim idx as integer		'index
	dim au as integer 
	dim time as integer 

	time = 100 

do

	if getPin(8) = 0 then 
		call Main()
	end if 


	thu = getADC(17)
	mdl = getADC(15)
	idx = getADC(13)

	
	'index
	if idx >= 14 then

		if getPin(8) = 0 then 
			call Main()
		end if 

	
		au = 220
		call FreqOut(19,au,au,time)

		thu = getADC(17)
		if thu >= 4 then
			au = 233 
			call FreqOut(19,au,au,time)

			mdl = getADC(15)
			if mdl >= 15 then
				au = 247 
				call FreqOut(19,au,au,time)
			elseif mdl < 11 then
				au = 277 
				call FreqOut(19,au,au,time)
			end if 


		elseif thu < 3 then
			au = 294 
			call FreqOut(19,au,au,time)
			
			mdl = getADC(15)
			if mdl >= 15 then
				au = 247 
				call FreqOut(19,au,au,time)
			elseif mdl < 11 then
				au = 277 
				call FreqOut(19,au,au,time)
			end if 

		end if 


	else
		au = 311 
		call FreqOut(19,au,au,time)

		thu = getADC(17)
		if thu >= 4 then
			au = 233 
			call FreqOut(19,au,au,time)
			
			mdl = getADC(15)
			if mdl >= 15 then
				au = 247 
				call FreqOut(19,au,au,time)
			elseif mdl < 11 then
				au = 277 
				call FreqOut(19,au,au,time)
			end if 
			

		elseif thu < 3 then
			au = 294 
			call FreqOut(19,au,au,time)

			mdl = getADC(15)
			if mdl >= 15 then
				au = 247 
				call FreqOut(19,au,au,time)
			elseif mdl < 11 then
				au = 277 
				call FreqOut(19,au,au,time)
			end if 


		end if 

	end if

'missing
'262
	
loop
	

End Sub