Page 1 of 1

Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 2:50 pm
by daveb0360
Hi,
Can somebody help with a problem I have.
I need to measure the voltage on ADC1 and create a decision based on the voltage being outside a central window. For example. I want my code to wait for a voltage that is either below 1.5 V or above 3.5V and then trigger external port.
In other words, a voltage that is between 1.5 and 3.5, keeps the program in a wait state.

I can't seem to get this to work and it is not due to the setting of the ADC (i.e. readasbyte, readasvoltage etc) this is irrelevant.
I can get the program to respond to either a voltage below 1 or above 3 independantly but I can't seem to get the maths (if voltage <1.5>3.5 then....) I get a syntax error when simulating, telling me that > is not acceptable.

Please assist, I'm sure it's something simple I'm overlooking .......hopefully I've made this clear.

Dave

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 3:08 pm
by JohnCrow
Hi Dave

If you are using floats , you cannot use the

if V < 3.5 type statements. There are a set of commands to do greater than, less than , equal to etc.

Have a look in the flowcode help file on floats it shows how to do it.
Sorry I'm at work so not able to access my system to give a more concise answer, but hope this helps you get stated

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 3:30 pm
by daveb0360
Hi John and thanks for the reply. I appreciate you are at work so don't get too involved in this.....would hate to see you get pulled for wasting time.

I think I misrepresented what I was trying to acheive. I gave the 1.5V / 3.5V as an example. The precise voltages are not critical. I was approximating a 0 - 5V range divided by 3 with the centre mathematical 'window' being measured and ignored by the program. The program only responding with a logical decision if a voltage was detected outside the central 'window'.
I could approximate within a byte range of 0 - 255 instead of 0 - 5V range if it makes it easier. I am trying to approximate/replicate a hardware 'window comparator' function.
i.e. I could base the logical decision on an ignore window of 85 - 170 for example. Give a signal on any measured byte value below 85 or above 170.

Is this clearer? and would this be simpler to implement and code?

Regards
dave

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 4:59 pm
by Benj
Hi Dave,

If you want to detect if a value is within a range then this is one way to do it.

Component Macro: sample = ADC Read As Byte

Decision: (sample > 50) && (sample < 100)

yes: Component Macro: Print String "In Range"

no: Component Macro: Print String "Out Of Range"

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 5:45 pm
by medelec35
Hi Dave,

I have shown this as an alternate way to Ben's.
His would be the best if only one function if out of range. E.g only portA1 is activated if out of range
You could use my version if an o/p is required ,if Voltage too high, or a different o/p is required if voltage is too low
Win Comp.jpg
Win Comp.jpg (130.75 KiB) Viewed 9188 times
Martin

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 10:37 pm
by daveb0360
Genius !!! Thanks Ben,
This works fine. Brings to mind though, the need to explain correct syntaxfor all these commands and functions.
Not meaning to be critical because I love the program but since it is intended for engineers that have little or no knowledge of programming languages, it follows that many would not know correct syntax and need a reference document.

Dave

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 10:47 pm
by daveb0360
medelec35 wrote:Hi Dave,

I have shown this as an alternate way to Ben's.
His would be the best if only one function if out of range. E.g only portA1 is activated if out of range
You could use my version if an o/p is required ,if Voltage too high, or a different o/p is required if voltage is too low
Win Comp.jpg
Martin

Thanks Martin,
My project is in early stages and depending on how I advance this and expand functionality, I may use this also. I actually only need a single port to go high(low) if input goes outside the central window. The central point determined by a simple resistor divider from VDD. I have actually decided to print a string with the command to LCD also.

The purpose of the circuit is to take a trigger from an external source that can be a voltage (scaled by R network) or from volt free relay. I already designed and built the circuit in analogue which worked fine but since there was spare capacity in the micro, I decided it would be good to combine all functionality in a single device.

Thanks again, I am sure I will have many other questions before I am done with this.

Dave

Re: Decision based on Voltage 'Window'

Posted: Fri Aug 05, 2011 11:04 pm
by medelec35
No problem
One thing about my flowchart, is you can use the formula in association with Ben's method
So you know the result is 35, then ADC is measuring 3.5V since result = V ADC x 10.

Martin

Re: Decision based on Voltage 'Window'

Posted: Fri Mar 15, 2013 2:22 pm
by JDR04
Thanks Martin, I'll go through this later.

I have however got a strange one for you. When I opened you attachment this morning and ran the circuit all went 100% OK.

However, when I open the attachment now the two LED's are missing?? I've tried opening the attachment a few times with the same result. I also noticed with the file I saved the two LED's were also missing??? Ghost busters???