• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

input and save input

Should be something more like this

function ADInAsVolt() as word
result = ADC.Read(0)
end function
Select ADInAsVolt()
ARGHHHHHHHHHHHHHH!


ADC.Read(0) reads the ADC and returns the unmodified reading. Use that, exactly as shown. Don't call another function. Don't convert it to volts. Don't add 1. Don't multiply or divide it by anything.


Just use the code in post #177, which is the one Burt copied from. (I'd suggest Mike's method, but that one's bound to get us another 40 pages)
 

be80be

Well-Known Member
I didn't post nothing that give a result in volts
I just fixed his messed up function
To return just the ADC value.
 

be80be

Well-Known Member
Dear god
I took the function fixed it to work with the case statement

Code:
function ADInAsVolt() as word
   result = ADC.Read(0)
end function
Select ADInAsVolt()
     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
Right this is not needed but it would have one in his code doing the wrong thing so I made him one that works
Code:
function ADInAsVolt() as word
   result = ADC.Read(0)
end function
 

be80be

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

Select Read(AN0)

The rest is perfect.
Jon that's not true All Did was fix the function he had and make it just return adc reading
and showed how to use it with a case statement that has been posted
 

be80be

Well-Known Member
Ok Mrdeb copied from swordfish the function that scales to voltage I removed the math and fixed that. Nothing wrong with that but a renaming of a function

Now as for this all he really needs is to call adc.read(0) and test with the case
But both will work one posted by someone and fixing his XXXXup function
 

JonSea

Well-Known Member
Sorry Burt, you are right. I didn't notice you removed all the math from the ADCasVolts function. But calling it that just confuses the issue. It doesn't give you volts and you don't want volts...and for some one who doesn't pay attention to details or understand how things work, it's going to add to the confusion. Besides,

ADC.Read(An0) is pretty fricken' clear and easy.

I would add the ADC prefix as many modules have a read command. The prefix insures you get the right one.
 

be80be

Well-Known Member
Jon I just redid his function cause no mater what we say it will have one. Only reason his was not working is because he was changing the reading with the math.

And I posted it with the case statement with one change that would make them work together.

But like you say
ADC.Read(An0) is pretty fricken' clear and easy.
 

MrDEB

Well-Known Member
Thread starter #209
I tried every suggestion and this code almost works right. Needs some tweeking at led7 etc.
Tried Burts last suggestion and no where near without putting the 500/1024 in the code.
Mikes suggestion of 1024/10 etc. got me close. It isn't as kosher or correct concerning the resistor ladder and having the correct ratios
{
*****************************************************************************
* Name : UNTITLED.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2017 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 12/21/2017 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
DEVICE = 18f2420
CLOCK = 8
INCLUDE "InternalOscillator.bas"
INCLUDE "SetDigitalIO.bas"
'INCLUDE "math.bas"
INCLUDE "adc.bas"
INCLUDE "Utils.bas"
'include "convert.bas"
DIM adval AS WORD
DIM adval1 AS WORD
DIM x AS BYTE
DIM position AS WORD
DIM Led0 AS PORTC.0
DIM Led1 AS PORTC.1
DIM Led2 AS PORTC.2
DIM Led3 AS PORTC.3
DIM Led4 AS PORTC.4
DIM Led5 AS PORTC.5
DIM Led6 AS PORTC.6
DIM Led7 AS PORTC.7
DIM Led8 AS PORTB.0
DIM Led9 AS PORTB.1
DIM led10 AS portb.2
DIM Player1 AS BOOLEAN
DIM Player2 AS BOOLEAN
DIM Player3 AS BOOLEAN
DIM Player4 AS BOOLEAN
DIM Player5 AS BOOLEAN
DIM Player6 AS BOOLEAN
DIM Player7 AS BOOLEAN
DIM Player8 AS BOOLEAN
DIM Player9 AS BOOLEAN
DIM Player10 AS BOOLEAN
DIM Player11 AS BOOLEAN
DIM Pos(11) AS WORD
FUNCTION Get_ADC_Sample() AS WORD // Function to grab the ADC sample for player position
result = ADC.Read(0)*500/1024 // Grab an ADC sample from channel 0
END FUNCTION
Player1 = false
Player2 = false
Player3 = false
Player4 = false
Player5 = false
Player6 = false
Player7 = false
Player8 = false
Player9 = false
Player10=false
INPUT (porta.0)
OUTPUT (PORTB.0)
OUTPUT (PORTB.1)
OUTPUT (portb.2)
OUTPUT (PORTC.0)
OUTPUT (PORTC.1)
OUTPUT (PORTC.2)
OUTPUT (PORTC.3)
OUTPUT (PORTC.4)
OUTPUT (PORTC.5)
OUTPUT (PORTC.6)
OUTPUT (PORTC.7)
PORTB.0=0
PORTB.1=0
portb.2=0
PORTC.0=0
PORTC.1=0
PORTC.2=0
PORTC.3=0
PORTC.4=0
PORTC.5=0
PORTC.6=0
PORTC.7=0
SetAllDigital
WHILE true
'For x = 0 To 9
'Pos(x) = 0
position = Get_ADC_Sample
DELAYMS(500)
position = Get_ADC_Sample
SELECT position
CASE <25 Player10 = false led10=1 DELAYMS(1000) led10=0 DELAYMS(1000)// No player

CASE <51 Player11 = true Led0=1 DELAYMS(1000) Led0=0 DELAYMS(1000)

CASE <153 Player1 = true Led1=1 DELAYMS(1000) Led1=0 DELAYMS(1000)

CASE <204 Player2 = true Led2=1 DELAYMS(1000) Led2=0 DELAYMS(1000)

CASE <255 Player3 = true Led3=1 DELAYMS(1000) Led3=0 DELAYMS(1000)

CASE <308 Player4 = true Led4=1 DELAYMS(1000) Led4=0 DELAYMS(1000)

CASE <355 Player5 = true Led5=1 DELAYMS(1000) Led5=0 DELAYMS(1000)

CASE <460 Player6 = true Led6=1 DELAYMS(1000) Led6=0 DELAYMS(1000)

CASE <500 Player7 = true Led7=1 DELAYMS(1000) Led7=0 DELAYMS(1000)

CASE <535 Player8 = true Led8=1 DELAYMS(500) Led8=0 DELAYMS(500)

CASE >1000 Player9 = true Led9=1 DELAYMS(1000) Led9=0 DELAYMS(1000)




END SELECT // need to indicate when player pos is done
WEND
 

MrDEB

Well-Known Member
Thread starter #210
I rearranged the resistor ladder by taking out the 100K/ .1 cap, and connecting the port A0 to the switches. The circuit works but needs some tweeking on the lower readings. Led 4-10 work just fine. Will figure out why etc and post results.
 

JonSea

Well-Known Member
I am so glad I've put so much effort into explaining things only to have it all ignored.

MrDEB, keep up with your copy&paste programming. There really is no need to actually understand anything...why do something in 10 minutes when you can get people to waste so much time with over 200 posts in a forum thread.

Let's be conservative and say each post is 10 minutes of effort. 2000 minutes is over 30 wasted hours of people's time, all for the joy of helping you.
 

JonSea

Well-Known Member
Not quite...he's not multiplying by 5. He's multiplying by 500/1024 for no apparent reason.

He forgot to multiply by 2017....I mean 2018 as of last night....and divide by 10 pages of forum replies.
 

JimB

Super Moderator
Most Helpful Member
I have been reading this thread for a few days now, it is like one of those really bad films that they sometimes (often?) show on television.
You start watching it, decide it is bad, but you cannot leave it, it is so bad that you just have to keep watching, there has to be a good bit coming up soon, but there never is.


To supplement the efforts of JonSea and be80be and to try in my own way to summarise what they have been saying, I offer the following few words:

1 Connect a bunch of resistors to make a potential divider.
2 Connect the junctions of the resistors to push button switches.
3 Connect the "common" of the switches to an analog input on a PIC micro.

4 Before writing any code, test the hardware.
4a Apply power.
4b Press the switches, one at a time.
4c Use a DMM and measure the voltage at the PIC input pin.
4d Write down the results.
4e Look at the results, are the voltages sensible? Do they step up/down as per the button pushes? Do they correspond to the values calculated in the design process?
4f If all is well, move to step 5. If all is not well, debug the hardware.

5 Write some code:
5a Initialise the PIC, pay special attention to the ADC.
5b Read the ADC, and display the ADC result on an LCD display.
(Note here that the result from the ADC is just a number in the range 0 to 1023. Don't mess about trying to convert it to a voltage, just display the raw ADC value).

6 Now test to code:
6a Apply power.
6b Press the switches, one at a time.
6c Read the numbers from the LCD.
6d Write down the results.
6e Look at the results, are the numbers sensible? Do they step up/down as per the button pushes? Do they correspond to the values calculated in the design process?
6f If all is well, move on and develop the CASE statement. If all is not well, debug the simple bit of code which you just wrote.

It is not that difficult.

JimB
 

JonSea

Well-Known Member
MrDEB either has a really good imagination (or loose grasp on reality), or he's just putting us on.

In his posted code, he has the command

SetAllDigital

which not surprisingly, sets all of the ADC inputs to digital I/O. I don't believe the ADC module does anything to override this because, as I explained previously, the Swordfish example file includes code to set up the analog input.

SmartSelectImage_2018-01-02-00-35-56.png

The Input(PortA.0] should take care of the Tris command, but without the next line, PortA.0 is a digital input because of the SetAllDigital command.

Help is happy given to those who make an effort to understand the replies, and whose next posts show a grasp of what's been explained. MrDEB can't explain why he decided to reinsert 500/1024 other than he saw it somewhere and he completely failed to understand what had been explained by at least 3 people. Sigh.
 

be80be

Well-Known Member
What the hell is this has to be a joke.
Code:
SELECT position
CASE <25 Player10 = false led10=1 DELAYMS(1000) led10=0 DELAYMS(1000)// No player

CASE <51 Player11 = true Led0=1 DELAYMS(1000) Led0=0 DELAYMS(1000)

CASE <153 Player1 = true Led1=1 DELAYMS(1000) Led1=0 DELAYMS(1000)

CASE <204 Player2 = true Led2=1 DELAYMS(1000) Led2=0 DELAYMS(1000)

CASE <255 Player3 = true Led3=1 DELAYMS(1000) Led3=0 DELAYMS(1000)

CASE <308 Player4 = true Led4=1 DELAYMS(1000) Led4=0 DELAYMS(1000)

CASE <355 Player5 = true Led5=1 DELAYMS(1000) Led5=0 DELAYMS(1000)

CASE <460 Player6 = true Led6=1 DELAYMS(1000) Led6=0 DELAYMS(1000)

CASE <500 Player7 = true Led7=1 DELAYMS(1000) Led7=0 DELAYMS(1000)

CASE <535 Player8 = true Led8=1 DELAYMS(500) Led8=0 DELAYMS(500)

CASE >1000 Player9 = true Led9=1 DELAYMS(1000) Led9=0 DELAYMS(1000)
END SELECT // need to indicate when player pos is done
The lowest case would be 44 the highest would be about 500
but why do it like that no wonder your getting nothing like has been posted.
 
Last edited:
JimB -

You forgot the all important Step #7....

7 Ignore steps 1-6 and insert random code/hardware changes until "it works". Be sure to avoid ANY and ALL suggestions made by others. Most importantly, make sure you have no idea what any of the code you copy is doing (otherwise it might accidentally work, and we'll have none of that!).
 

MrDEB

Well-Known Member
Thread starter #218
Jon may have the reason why this circuit is not doing what it is supposed to do (see post #215)
will try this out
 

MrDEB

Well-Known Member
Thread starter #219
Well I think Jon may have hit on the solution (see post #215)
As for the FUNCTION I tried with and without the +1 *500/1023 and both work the same or that's what it appears.
I need to jingle the higher CASE SELECT numbers as they are slightly off. Probably due to resistor tolerances etc. but it appears to work.
Now just waiting for switches and solder up this circuit using switches instead of an alligator clip.
 

JonSea

Well-Known Member
...As for the FUNCTION I tried with and without the +1 *500/1023 and both work the same or that's what it appears....
Do you really not understand ANYTHING about programmimg????

How on Earth can it work the same with or without "*500/1024"? Multiplying by 500 and dividing by 1024 means with this, the value read is only half what it is without it. It works the same either way? Then it's NOT WORKING AT ALL

AND WHEN YOU SAY "JON HIT THE NAIL ON THE HEAD" BY ENABLING THE ADC? WHAT IN THE HELL HAVE YOU BEEN DOING? YOU CAN'T HAVE GOTTEN ANY RESULTS THAT MAKE SENSE IN THE LEAST!!!!!!!!!
 

Latest threads

EE World Online Articles

Loading

 
Top