![]() |
Forum Index : Microcontroller and PC projects : Problem with ADC
Author | Message | ||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
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] Microblocks. Build with logic. |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 direction |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
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: ThailandPosts: 2209 |
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. Microblocks. Build with logic. |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 KingdomPosts: 1702 |
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] |
||||
disco4now![]() Guru ![]() Joined: 18/12/2014 Location: AustraliaPosts: 1000 |
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 KingdomPosts: 1702 |
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 either |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10250 |
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 |
||||
lew247![]() Guru ![]() Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
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 KingdomPosts: 1702 |
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 KingdomPosts: 2934 |
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 KingdomPosts: 1702 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |