Setting up decision windows

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Setting up decision windows

Post by medelec35 »

I need Flowcode to make decisions, depending on input range to an analogue i/p.
In the real world there will be a tolerance.
E.G. instead of : If i/p = 2.821V then do something Or if i/p = 1.276V do something else.

It should be If i/p is between 2.921V and 2.721V then do something Or If i/p 1.176V and 1.376V do something else.

If I have the following voltages:
0.448
0.904
1.276
1.552
1.874
2.205
2.481
2.821
3.055
3.297
3.501
3.651
3.833
4.037
4.197

How do I set Flowcode up to make a unique decision based on the above voltages with 100mV tolerance either side?

If just given example for the 1st and 2nd set of figures, I can fill in the rest.
Thank you.
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Setting up decision windows

Post by Benj »

Hello

You could use a lookup table and a switch case statement.

Firstly use Excel to map out 256 values these 256 values correspond to the full range of 0-5 V on the ADC reading. Then for each analogue reading specify the mode you want the device to go into.

Eg.

Analogue value out of range - 0
Analogue Mode 1 - 1
Analogue Mode 2 - 2
etc.

Reading: 0, 1, 2, 3, 4, 5, ..., 255
Mode: 0, 0, 0, 1, 1, 0, ..., 0

Use Seans article on Lookup Tables to get the excel data into Flowcode and then use the analogue reading as the index value for your lookup table. Use the return value from this in your switch case icon and hey presto you can switch to different macros etc to process the diffferent modes etc.

For higher resolution you could use the 10-bit adc reading but you would have to create 4 arrays of 256 bytes to allow the lookup table to work for every potential ADC reading. Arrays cannot be over 256 bytes therefore the reason for needing 4 of them.

Hope this helps.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Setting up decision windows

Post by medelec35 »

Thanks Ben.
I am using 4 switches so its 4 bit. There will be a tolerance in components, so with switches in the same position I have to allow for this tolerance.
Won’t the lookup table assume there is a 0% tolerance of components? I may of misread what you said.
I am looking for a way to detect reading between two figures.
Here is my 1st attempt. Im sure there could be a better way using calulations!
Decision.JPG
Decision.JPG (33.59 KiB) Viewed 8484 times
If more than one unit is produced with a tolerence in components, and power supply, without a window, one unit might work, but the next one may not work.
I am already using 4 sets of lookup tables, I have to be careful don't run out of memory.
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Setting up decision windows

Post by medelec35 »

Zoomed in so more of calulations can be seen. Also correct a couple of things.
Decision2.JPG
(59.13 KiB) Downloaded 2338 times
Ben. I understand what you are saying about the lookup table. I will give it a try, but with just 1 set of numbers 0 to 255.
Then will have to d: If mode= 0 or mode =1 or mode = 2 or mode =3or mode = 4 then.
If mode= 5 or mode =6 or mode = 6 or mode = 8 or mode = 9 then.
Since each number = 5/255=0.196mV

Correction: Just had a look and using 6 sets of look up tables(0 to 255) sofar not 4!
Martin

User avatar
Steve
Matrix Staff
Posts: 3434
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Setting up decision windows

Post by Steve »

I think decisions will only work for integers, and not for floating point values.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Setting up decision windows

Post by medelec35 »

Ah. Thanks Steve will change all to integers.
What way would you go with, lookup table or 15 decisions?
Or even something else?
I can totally understand why Ben said about lookup tables.
Just looking for most reliable and simplest solution.
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Setting up decision windows

Post by Benj »

Hello

If you have 4 switches connected to your DAC then can you simply take an 8-bit analogue reading, divide the result by 16 and then use the result as the switch result. This may not work if your tolerances are slightly off but you may be able to get away with using this method. This way you would not need the 256 byte lookup table.

If this will not work then you could divide by 2 to get a 128 byte lookup table or divide by 4 to get a 64 byte lookup table etc.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Setting up decision windows

Post by medelec35 »

Thanks Ben, that's another good suggestion!
Martin

User avatar
Steve
Matrix Staff
Posts: 3434
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Setting up decision windows

Post by Steve »

Another possibility is to have a function that accepts the analogue input voltage and compares it to each "ideal" voltage in turn. When it finds the closest match (i.e. the one with the smallest value for |Vin - Videal| , the absolute value of the difference), then it will know that is the one. Here's a brief example of what I mean:

Code: Select all

Vdiff = 255
Switch = 255

//Check reading 1 (21 is the first "ideal" reading)
if (Vin > 21)
  ThisDiff = Vin - 21
else
  ThisDiff = 21 - Vin
endif
if (ThisDiff < Vdiff)
  Vdiff = ThisDiff
  Switch = 1
endif

//Check reading 2 (36 is the "ideal" reading)
if (Vin > 36)
  ThisDiff = Vin - 36
else
  ThisDiff = 36 - Vin
endif
if (ThisDiff < Vdiff)
  Vdiff = ThisDiff
  Switch = 2
endif

.
.
.
If you had spare ram, you could create an array of "ideal" values and implement this function as a loop.

I would also keep all of your arithmetic in integer form - using floating point makes the code much more bulky and slow and is best avoided in most situations. So, rather than using a value for the voltage, work with the actual 10-bit ADC reading.

I think Flowcode could do with an elegant way of implementing a look-up table. Also, the new "switch" icon could be changed to accept individual conditions instead of just numbers. Either of these ways would make your code look neater.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Setting up decision windows

Post by medelec35 »

Thanks Steve, What you posted last, takes a bit of working out. In the mean time.I have got a working version. do think yours or Ben's way is simpler?
I have posted results form a different set of resistors and completed a Flowchart based on results.
Attachments
test switches adc.FCF
(14.04 KiB) Downloaded 329 times
multiple switches to ADC(1).xls
(68.5 KiB) Downloaded 323 times
Martin

User avatar
Steve
Matrix Staff
Posts: 3434
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Setting up decision windows

Post by Steve »

My way is far superior in every way, but I would say that!! :lol: :lol: :lol:

Seriously though, if you have something that works and you're happy with it then I would probably leave it as it is.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Setting up decision windows

Post by medelec35 »

lol thanks.
Martin

Post Reply