Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 19:51 15 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Problem with ADC

Author Message
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 11:22pm 10 Mar 2016
Copy link to clipboard 
Print this post

I;m trying to get wind direction to work
My wind vane is a 10k pot and it gives a nice linear voltage reading between 0 - 3.3V depending on which direction it's pointing

I cannot get the reading from the adc to work properly

I'm using a micromite+ to test the code and pin 62 should be an analog pin

here's a sample of the code thats really bugging me
[code]
Const wDirPin=62 'connected to the centre pin of the potentiometer
Setpin wDirPin,Ain

Sub WindDirProc

wDir=Pin(wDirPin) 'wdir=Pin(62)
select case wDir
case 0.993 to 1.023 'if the voltage on wdirPin is between 0 and 0.031 it should print "wheading North"
wheading = North
case else
case 0 to 0.031
wheading=North
CASE ELSE
case 0.032 to 0.096
wheading = NNE
end select
End Sub
print "Heading"; wDir[/code]


No matter what I try and do it always prints wheading 0

Any ideas why? or is there a special way to measure the voltage on an analog input pin


I did read the manual and I did try other options with case such as using case IS =0 and >=0.32 and various permutations around this
Edited by lew247 2016-03-12
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 11:33pm 10 Mar 2016
Copy link to clipboard 
Print this post

Try this (untested!)

[code]
Const wDirPin=62 'connected to the centre pin of the potentiometer
Setpin wDirPin,Ain


do
print "Heading"; WindHeading(PIN(wDirPin))
pause 5000
loop

FUNCTION WindHeading(value) AS STRING

SELECT CASE value
CASE 0.993 to 1.023 'if the voltage on wdirPin is between 0 and 0.031 it should print "wheading North"
WindHeading = "N"

CASE 0 to 0.031
WindHeading = "N"

CASE 0.032 to 0.096
WindHeading = "NNE"

CASE ELSE
WindHeading = "?"
END SELECT

END FUNCTION
[/code]
Edited by MicroBlocks 2016-03-12
Microblocks. Build with logic.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 12:30am 11 Mar 2016
Copy link to clipboard 
Print this post

Thanks Jean, I tried many variations of that but it just wont work
from what I can see with what you suggested

print "Heading"; WindHeading(PIN(wDirPin)
MEANS
print "Heading"; result of direction function(value of pin62) =

the WindHeading function from what I can see has no way of knowing it's meant to be the value of pin62 (0-1023) ?


in my example
wHeading=Pin(wDirPin) 'wheading = value of pin62 (0-1024)

told the program that the value of pin62 = wheading then the Case "SHOULD" have decided the directionEdited by lew247 2016-03-12
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 12:48am 11 Mar 2016
Copy link to clipboard 
Print this post

Does PIN() give a 0-1024 (or 1023) value?

If it does, it needs dividing by 1024 for the code to have a chance to work.

John
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 12:52am 11 Mar 2016
Copy link to clipboard 
Print this post

A function will work with the paramaters that you supply.
The name of that parameter is then a local variable for that function only.

So PIN(wDirPin) which is exactly the same as writing PIN(62) will return the value of pin 62.
Lets imagine it is 0.123.

It will then use that value to pass to the function.
Equivalent to WindHeading(0.123)

So the function does not know anything about any analog pins, and it doesn't need to. It's only purpose is to translate the value into something meaningful.

But first things first. You need to know if you are getting good values from the analog port.
A tiny program that does only that will make sure.
[code]
SETPIN 62, AIN
DO
PRINT PIN(62)
PAUSE 1000
LOOP
[/code]

Once that works you can add the function again.
Edited by MicroBlocks 2016-03-12
Microblocks. Build with logic.
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 01:21am 11 Mar 2016
Copy link to clipboard 
Print this post

Thank you so much
I never would have thought of just trying it on it's own

Tring the print PIN(62)
it gives a voltage reading of 0-3.3V depending on which direction it's pointing.
I'll carry on with it now and sort it out (hopefully)
Thanks
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 02:26am 11 Mar 2016
Copy link to clipboard 
Print this post

I really don't get it

IF I just measure the voltage on pin 62 using the program it works and prints out the voltage
If I try and get it to do anything else it just will not work no matter what I try

The program below is the closest I have got to what I want
it prints [quote]Direction: YES[/quote]

The problem is - I had pin 62 set to 0V even changing it to 3.3V it still said the same

its like the conversion using the CASE is not working

I have tried every combination of CASE I could reading through the manual, and even some that are not in there, I've tried 0 TO 1,5 and 1.6 TO 3.3 and that does not work
which is why I changed to <= symbols
it "SHOULD" be working, the micromite does measure the voltage and print it out IF I don't use the sub WindDirProc

I'm completely stumped
This is the full program

[code]
SETPIN 62, AIN
dim Wheading as string

Do
Pause 1000
WindDirProc
Print "Direction: "; wheading
loop
Sub WindDirProc

select case Pin(62)
case IS > 1.51, is <= 3.3 'if the voltage on wdirPin is between 0 and 1.5 it should print "Yes"
wheading = "Yes"
case is <= 0, is <= 1.5
Wheading = "NO" 'if the voltage on wdirPin is between 1.51 and 3.3 it should print "No"

end select
'END function
End Sub
[/code]Edited by lew247 2016-03-12
 
disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1000
Posted: 02:49am 11 Mar 2016
Copy link to clipboard 
Print this post

I think the case statement below does not do as you expect. It is true when it finds either of the conditions true. It does not require both to be true, the test IS <=3.3 will always be true and hence this case is the one selected.


case IS > 1.51, is <= 3.3 ' ALWAYS true if EITHER condition met.

Regards
Gerry

Latest F4 Latest H7 FotS
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 03:00am 11 Mar 2016
Copy link to clipboard 
Print this post

How can I get it to compare readings then and print a different value for each reading?

I've tried
[code]
select case value
CASE 0.993 to 1.023 'if the voltage on wdirPin is between 0 and 0.031 it should print "wheading North"
wheading = "N"
CASE ELSE
CASE 0 to 0.031
Wheading = "N"
CASE ELSE
case 0.97 to 0.160
wheading = "NE"
CASE ELSE
case 0.161 to 0.224
wheading = "East"
[/code]
and using this format doesn't work eitherEdited by lew247 2016-03-12
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10250
Posted: 03:09am 11 Mar 2016
Copy link to clipboard 
Print this post

You should only have one "CASE ELSE" and it must be the last.

so try this:


SETPIN 62, AIN
do
value=pin(62) 'are you forgetting this?
select case value
CASE 0.993 to 1.023 'if the voltage on wdirPin is between 0 and 0.031 it should print "wheading North"
wheading$ = "N"

CASE 0 to 0.031
Wheading$ = "N"

CASE 0.97 to 0.160
wheading$ = "NE"

CASE 0.161 to 0.224
wheading$ = "East"

CASE ELSE
wheading$="not yet assigned

end select
print wheading$
pause 1000
loop
Edited by matherp 2016-03-12
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 03:22am 11 Mar 2016
Copy link to clipboard 
Print this post

Thank you again, I'll never be able to repay all the help I've received
That now works, I'll now try it with all 32 directions defined and "hopefully" it will work still
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 04:07am 11 Mar 2016
Copy link to clipboard 
Print this post

Working perfectly now
Thank you everyone so much especially MicroBlocks and matherp for your help, and also to MikeO who spent a very painful 2 days through email helping me with various stuff relating to this.

This forum is amazing (in a very good way )
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2934
Posted: 06:32am 11 Mar 2016
Copy link to clipboard 
Print this post

  lew247 said  My wind vane is a 10k pot and it gives a nice linear voltage reading between 0 - 3.3V depending on which direction it's pointing


Hi Lewis,

Glad you've made good progress today with this.

Out of interest, is your Wind Vane effectively a continuous rotation pot? Apologies if you have posted a link previously, but any chance of more details about the vane (did you purchase - or is it home made?)

Thanks,
WW
 
lew247

Guru

Joined: 23/12/2015
Location: United Kingdom
Posts: 1702
Posted: 07:21am 11 Mar 2016
Copy link to clipboard 
Print this post

Yes it's a continuous rotation pot 20K
[code]
Wind Speed
Measurement range 0-100 mph (standard)
0-50 m/s, 0-100 knots, or 0-200 km/hr (optional)
Speed constant 1.25 mph = 1 pps
75 mph = 60 Hz (pps)
Transducer type Reed switch
Speed threshold 0.8 mph
Accuracy 1 mph or ± 3%
Wind Direction
Range 0-360 degrees azimuth
Transducer type Potentiometer, 20k ohms, conductive plastic
Potentiometer gap 5°
Azimuth accuracy ± 3°
Threshold 1.2 mph
Bearings Bushing

The wind sensor output signal is a reed switch closure. There are three switch closures
for each revolution of the cup assembly. [/code]

The only reason I have this one is I got it at a real bargain price on Ebay. I'd never had considered it otherwise

User manual www.novalynx.com/manuals/200-ws-23-manual.pdf


Edited by lew247 2016-03-12
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025