Reading Multi switches on only one ADC pin

Post here to let others know of a project you're working on.

Moderators: Chuckt, Garth, bitfogav

Post Reply [phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Reading Multi switches on only one ADC pin

Post by bitfogav » Wed Jun 05, 2013 7:38 am

I know this isn't anything new but its worth sharing and someone here might like to use it in there next project..

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
adcpinswitches.jpg
Schematics:
adcpinswitches.jpg (21.89 KiB) Viewed 13883 times
*Edited - added more code to show keypress*
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Reading Multi switches on only one ADC pin

Post by brad » Wed Jun 05, 2013 10:40 pm

I remember reading up on this on digital-diy.com a while back. It's a great way to get multiple buttons on one port pin with minimal hassel.

Thanks for sharing the firewing code - Actually I think I'll open up a new forum specifically for firewing because I think eventually I will head down that path.

User avatar
bitfogav
Moderator
Moderator
Posts: 915
Joined: Sun Mar 28, 2010 9:03 pm
Location: United Kingdom
Contact:

Re: Reading Multi switches on only one ADC pin

Post by bitfogav » Thu Jun 06, 2013 2:47 am

brad wrote:Thanks for sharing the firewing code - Actually I think I'll open up a new forum specifically for firewing because I think eventually I will head down that path.
Thats not a bad idea actually Brad.

Firewing at this time is limited to only one pic 24HJ128, I like the fact that its so easy to port code from Swordfish to Firewing and vice versa, though im not going to give up on Swordfish has I really like the 18F pic's.. :)
If you don't know what Voltage your country is using, you shouldn't be doing electronics ;-)

User avatar
brad
Site Admin
Site Admin
Posts: 2578
Joined: Fri Mar 26, 2010 10:30 pm
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Re: Reading Multi switches on only one ADC pin

Post by brad » Thu Jun 06, 2013 9:52 pm

Good to hear that you're still sticking with 18F's (They are still my favourite!)

Post Reply
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable
[phpBB Debug] PHP Warning: in file [ROOT]/vendor/twig/twig/lib/Twig/Extension/Core.php on line 1266: count(): Parameter must be an array or an object that implements Countable

Who is online

Users browsing this forum: No registered users and 8 guests