# input and save input

#### Pommie

##### Well-Known Member
If you use 10 1k resistors and a large tiedown (50k) the voltage will be 0V for no press and 0.5V to 5V in 0.5V steps. Simple and obvious.

Mike.

#### be80be

##### Well-Known Member
That's why you paid the big bucks this works good too.

#### JonSea

##### Well-Known Member
IF you understand how a voltage divider works and can make simple calculations

AND

IF you understand how the ADC works and the meaning of a ratiometric system

THEN the rest is simple. Some choices make it easier to make calculations, but it's just more convenient if you do so.

IF you don't understand that this is just a big voltage divider problem, you'll have no clue why your readings simply cannot clump around the middle of the range and you will jump to erroneous conclusions like "it would work properly with 1% tolerance resistors."

Yay! 10,pages in the forum and still nowhere near a solution! This may be a MrDEB record - not for the number of posts but for keeping them in a single thread instead of multiple threads on multiple boards so no one can follow any of it.

Jon's Rule of Forums: When looking for an answer to a problem, a thread with a multitude of answers is probably not a worthwhile place to look. The key information may be there, but there's so much rubbish, it's not worth your time to muck about.

#### MrDEB

##### Well-Known Member
I looked at several examples and every one had result = (ADC.Read(0)+1)*500/1024 Saw one example where instead of 500 they used 5000.Why??
The reason for the 2K resistor is it was suggested in post #126
I realize I must have gone astray on this so an apology is in order "sorry"
While laying in bed this morning it dawned on me that I have 13 junctions 2 for Vcc & ground, 1 for the 2 1K resistors and 10 for the switches so 1023/13 = 79 BUT Mike stated I need to account for the no button press??(1023/26= 39)
So my first switch should be 79+39 = 118
Another issue might be I have no DIM statements defining INTEGER.

#### Pommie

##### Well-Known Member
Your posts inspire me. There must be so many people that have learnt so much from these threads. Well done MrDEB.

Mike

#### JonSea

##### Well-Known Member

I'm done. Everything has been explained to death by multiple people. The exact circuit to use had been drawn up. The values to measure have been calculated and verified by testing. The code has been outlined. And still MrDEB is as clueless as a gold fish.

Mike, Burt, Tumbleweed, care to join me at the pub? I think we have done all we can here.

#### be80be

##### Well-Known Member
I looked at several examples and every one had result = (ADC.Read(0)+1)*500/1024 Saw one example where instead of 500 they used 5000.Why??
There scaling for volts Mrdeb you don't need any of that for what your doing just a old result = ADC.Read .................................................
you want a value 0 to 1024 not a voltage 1 volt 2 volt LOL your just adding more to the clock doing math that's not needed.

#### JonSea

##### Well-Known Member
There scaling for volts Mrdeb you don't need any of that for what your doing just a old result = ADC.Read .................................................
you want a value 0 to 1024 not a voltage 1 volt 2 volt LOL your just adding more to the clock doing math that's not needed.

I explained that at least twice already....

#### be80be

##### Well-Known Member
I know nothing better to do work was called off christmas slower then they was thinking. Not good I guess seeing where I work.

#### MrDEB

##### Well-Known Member
The example I was referrng to n post #183 was doing exactly what I am trying to do, read 10 switches. One big difference was the example had the port pin tied high with a 100K resistor.
I tried
END FUNCTION

but it didn't work at all.
My setup, as mentioned in a post was it appeared that I was only reading 1/2 of the full 0-1024 scale.
going to try tying the port pin high with a 100K resistor.

#### Pommie

##### Well-Known Member
You are trying to put a fraction into a word.

Mike.

#### JonSea

##### Well-Known Member
.....Just a thought....
Everything you need to know has been explained here. Apply your thought process to understanding what has already been posted.

Geez oh grief, what a waste of so many people's time.

#### JonSea

##### Well-Known Member
You are trying to put a fraction into a word.

Mike.
Words don't mean much to MrDEB, as evidenced by this entire thread.

#### be80be

##### Well-Known Member
Well i would write some code to try but I deleted windows by mistake and lost about five years of my life. Going blind does that to you
thought i was formatting a usb drive and phone rang i wasn't watching i guess and clicked On my windows drive hit format gave root password
ask if I was sure and I cried. LOL not really but wanted too.

#### MrDEB

##### Well-Known Member
I understand what the circuit is supposed to do as well as the ratio between each adc output but the output is not correct.
Am wondering if the code I am using is at fault?? won't be the first time.
Will clean up my code and post if maybe it would help??

#### tumbleweed

##### Active Member
go ahead and post it, along with exactly how you have the resistors connected (all of them)

don't clean it up... just post what you're using.

#### JonSea

##### Well-Known Member
Well i would write some code to try but I deleted windows by mistake and lost about five years of my life. Going blind does that to you
thought i was formatting a usb drive and phone rang i wasn't watching i guess and clicked On my windows drive hit format gave root password
ask if I was sure and I cried. LOL not really but wanted too.

Oh man Burt. Ouch! That sucks big time. Hell of a way to end the year.

I hope you can recover some of it at least, and get Windows back in business.

Too late to help, but thst's why I use Dropbox. Anything of importance gets saved there so even if someone....I mean something....pushes me over the edge and I destroy my computer with an axe, my files will be safe. I am not exactly sure how Dropbox does it, but I can save a file on my laptop and by the time I sit down at my desktop (admittedly, that takes a while with my new knees) or look on my phone, the file is there ready to use.

#### be80be

##### Well-Known Member
Should be something more like this
Code:
function ADInAsVolt() as word
end function
Case <25
'no button pressed
Case <100       ' ideal=92
'button 1 pressed
Case <200       ' ideal=185
'button 2 pressed
Case <300       'ideal=278
'button 3 pressed
Case <400       'ideal=371
'button 4 pressed
Case <500       'ideal=464
'button 5 pressed
Case <600       'ideal=557
'button 6 pressed
Case <700       'ideal=650
'button 7 pressed
Case <800       'ideal=743
'button 8 pressed
Case <900       'ideal=836
'button 9 pressed
Case <1000       'ideal=929
'button 10 pressed
else
'something is wrong
End Select

Last edited:

#### JonSea

##### Well-Known Member
...Am wondering if the code I am using is at fault?? won't be the first time.
Trust me. You don't have to wonder about that. It goes without saying.

I must say, the Swordfish documentation and example files are the worst. They don't explain why they are doing what they do and they are not complete.
I suggest you read and understand the following. Barring that, the ONLY things you need in your code as far as this switch ladder is concerned is in blue below. Copy and paste NOTHING ELSE.

First, INCLUDE ADC.BAS. This is the module that handles the details for you.

Next, forget about the examples you have read. You do not want to convert the readings to volts. This is needless math and it's just wrong because you're operating from 3-something volts, so using Vdd as a reference where you are assuming it's 5 volts makes the numbers wrong anyway. You want the raw ADC output, which is a 10 bit number meaning it can range between 0 - 1023. A reading of 1023 means the level you are measuring is the same as Vdd, a reading of 512 means the level is 50% of Vdd, etc. We don't know what Vdd from your battery is and we don't care what the actual voltage is. The only thing we care about is the ratio of the measured level compared to full scale.

From the Swordfish help file, next we need:

// initialise <-------- this goes at the start of the program code, after subroutines
TRISA.0 = 1 // configure AN0 as an input
ADCON1.7 = 1 // set analogue input on PORTA.0
delayms (500)

This function is in the ADC module, and is what we will use to read the ADC.

function Read(pChannel as byte) as word <---------- This is in the ADC module. Don't put this in your code any place. You need to look at it to understand what you are doing.

<---------- This is in the ADC module. Don't put this in your code any place. You need to look at it to understand what you are doing.

When you want to read the ADC value, this is what you use:

Now to do something with it. The value you read is in ADResult.

Case <93

(this has all been explained in other posts. Figure it out from here)

#### JonSea

##### Well-Known Member
Ignore the first 3 lines in what Burt posted. You don't want the readings in volts, as I explained above.

Delete those 3 lines (and insert NOTHING IN THEIR PLACE) and change line 4 to