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!
Werner
Constrain and map analog input?
Moderator: Benj
-
- Posts: 95
- Joined: Sat Jun 11, 2011 4:12 am
- Has thanked: 87 times
- Been thanked: 31 times
- Contact:
Constrain and map analog input?
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
-
- 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?
Hi Werner,
What about using the decision branch: You should also be able to use decision branch for map as well.
I'm not that up on Arduino language
Martin
What about using the decision branch: You should also be able to use decision branch for map as well.
I'm not that up on Arduino language
Martin
Martin
-
- 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?
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.
Have a great day Professor!
Werner
Have a great day Professor!
Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
- 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?
Hello Werner,
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
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.There is something very clever about some of the Arduino functions?
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
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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?
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
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"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
-
- 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?
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: So you can use output = input *17/10 - 1275
Martin
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: So you can use output = input *17/10 - 1275
Martin
Martin
-
- 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?
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
Thanks so much!
Have a great day!
Werner
Thanks so much!
Have a great day!
Werner
Werner
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"
STUDENT OF: Martin - Professor of Flowcode, John, Jan, Fotios and Nicolas "Spanish Dude"