Constrain and map analog input?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times
Contact:

Constrain and map analog input?

Post by Werner »

Hi Guys, is there an easy way to constrain and map an analog input in FC, similar to the snippet of code I have below.

void loop() {
int val = analogRead(sesePin);

val = constrain(val,750, 900);
int ledLevel = map(val, 750, 900 255, 0);

analogWrite(ledPin, ledLevel);

}

Thanks for the help! Have a great day! :D

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

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

Re: Constrain and map analog input?

Post by medelec35 »

Hi Werner,
What about using the decision branch:
Constrain.png
(20.86 KiB) Downloaded 4331 times
You should also be able to use decision branch for map as well.
I'm not that up on Arduino language

Martin
Martin

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times
Contact:

Re: Constrain and map analog input?

Post by Werner »

Hi Martin, Thanks for the reply! That certainly works. There is something very clever about some of the Arduino functions? I had to scratch my head for a while to see the math for scaling the "analog in" to say for example a 0 -255 byte. It is almost to easy with the Arduino function. :D

Have a great day Professor!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

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: Constrain and map analog input?

Post by Benj »

Hello Werner,
There is something very clever about some of the Arduino functions?
The maths isn't too bad, it just uses real numbers which can be a bit memory intensive. YOu get away with functions like this on an AVR but try them on a 8-bit PIC and you quickly eat into your RAM/ROM etc.

I take it you need 750 as 0 and 900 as 255 with value scaled in between.

First use Martin's decision method to add your limits. Next here is the maths to do the ranging.

900 - 750 = 150

255 - 0 = 255

255 / 150 = 1.7

output = (input - 750) * 1.7


Some worked examples.

(750 - 750) * 1.7 = 0

(800 - 750) * 1.7 = 85

(850 - 750) * 1.7 = 170

(900 - 750) * 1.7 = 255

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times
Contact:

Re: Constrain and map analog input?

Post by Werner »

Hi Ben, Thanks for the great reply!! It seems so easy now that you showed me how to do it.
I really appreciate your thought and time you took to show me!

Have a great day Buddy!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

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

Re: Constrain and map analog input?

Post by medelec35 »

Hi Werner,
Alternatively, if you have excel or a free equivalent:
Download Formula Generator excel sheet from here
If you put in the values required then you will get the required calculation assuming using integers:
Formula.png
(32.09 KiB) Downloaded 4244 times
So you can use output = input *17/10 - 1275

Martin
Martin

Werner
Posts: 95
Joined: Sat Jun 11, 2011 4:12 am
Has thanked: 87 times
Been thanked: 31 times
Contact:

Re: Constrain and map analog input?

Post by Werner »

Hi Professor, You Guys have beautiful minds! Buddy this is a great tool, very useful! This will work for anything I need to do. And boy do I need it! Ha ha :D

Thanks so much!
Have a great day!

Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"

Post Reply