For a new project ive been working on ive used an ADC pin of a PIC to read several switches, this limits the amount of pins needed by the PIC to control several switches..
Basically by using several resistors inline between the switches we can read the voltage on the line using an ADC pin and then we can evaluate the switch that was pressed..
We can have a function (Firewing code) to read the ADC pin and we can return which key was pressed and use this in our prog:
Code: Select all
function Keypress() as byte
dim voltage as ushort = Adc.Read(A0) ' get ADC voltage(value)
Keypress = 0 ' set Keypress to default value
select voltage
case < 50 : Keypress = 1 ' select
case < 200 : Keypress = 2 ' fire
case < 400 : Keypress = 3 ' right
case < 600 : Keypress = 4 ' up
case < 800 : Keypress = 5 ' down
case < 1000 : Keypress = 6 ' left
end select
end function
Sub Main()
While True
Dim Key as Byte = Keypress
Console.Write("Key = ",str(Key),13,10)
End While
End Sub