# How to determine trending of values..

Hi All,

I have been searching the forum, but I have not been able to find an example of how to do this:

-I want to read a voltage on a couple of the analog pins...yes, this is the easy part... :-) Now, I want to say take successive readings within a loop, in other words, constantly monitor the input, and then determine whether the value being read is increasing or decreasing...

Reason why I want to do this, is because I then want to control the rotation of a stepper or servo motor...in other words, if the value that I am reading is increasing, the motor needs to turn clockwise, if the value is decreasing, it needs to turn counter-clockwise...until some pre-determined value is reached, then stop....

So keep reading value....is value increasing? Turn clockwise...Decreasing, counter-clockwise....value reached, stop....

I have tried doing this, and I can't quite get it right....

I hope what I am asking is not completely stupid, and would appreciate an example as to how best to do this?

thank you all for any guidance!

Determining if one value is higher or lower that another value is easy.

Determining if the average of a number of values is higher or lower than a previous average is not much more difficult.

How often does the value change? What is providing this value? Over what time are you interested in the trend? Or, is it instantaneous changes that are of interest?

Thanks Paul...

It is pretty much instantaneous, but the reading will be triggered upon an event, which will be monitored by another pin going high...until it goes low...

So something like this...

--Keep monitoring some pin until it goes high.... if pin = high....keep taking readings from some analog pin.....until pin goes low.... if values keep increasing during this duration, turn motor CW, until they reach a minimum reading....then turn another stepper , and check the same thing...

Keep going back and forth between the steppers, until the minimum value is reached....in other words, the turning of the motors will have an effect on the readings...but I need to be able to determine if the readings on the analog pin are increasing or decreasing, so that I can determine which way I need to turn my motors....

hope I am making sense...it is this logic I am struggling with...how to put it all together in a loop that makes sense....

thank you again, really do appreciate it!

Not very clear why you want to do it. Just reading what you want to do doesn't make me understand it enough to give advice. If you can tell the whole store, you can get more people to understand the situation and help out. What Paul asked was very important, how quickly are the readings fluctuating what rate of increase you are looking for as an increasing trend and how often you take readings. Your math relies on these facts.

If the reading changes more of less smoothly, you can use a running average to keep an average and compare a new reading with the running average to determine the increase/decrease thing. If the fluctuation is a problem, you have to fit a line through your past data to see if there is an increasing trend of how many units per second. It's more time-consuming to do the fit.

OK, I am sorry about that, I will try to be much more descriptive…

–When an event triggers, as I described, I want to start reading the voltage value present at some analog pin…while this event is true (as monitored by some pin going high), the readings need to be taken as fast as possible. the feedback needs to be pretty instantaneous, as physically possible offcourse. The readings are going to be fluctuating by a few millivolts, but I will not need more than the 10 bit resolution as provided on the chip…I will be mapping the readings to one of the 0-1023 A/D bits on the chip, so really, the logic needs to be driven by values out of the A/D converter, do not need much higher resolution than that.

–Based on these readings, I want to make a decision: Is the reading <= to some predefined value? Then do nothing.

–is the reading greater than some predefined value?: Start to turn a stepper, and keep reading, to see if the value increases or decreases.

–If the value increases, turn CW, if it decreases, turn CCW. Stop, when the minimum value has been reached…whatever that is…

–Record the value, and the stepper position.

Really, that is the logic, not sure how much more descriptive I can be? I am explaining what I want the loop to do, and I am sorry if I am not doing a good job at it…hopefully this will allow you to give me some hints?

Anything else I can answer? Really do appreciate your time, as I am really struggling with this…

Well, you spelled out a fairly specific set of criteria there. Fairly unique as well, so you aren't going to find any examples that will meet your criteria.

However, on the surface I don't see anything complicated there. If you know how to take analog input readings, drive a stepper or servo (and their are libraries that make doing either of those fairly simple), and perform some fairly simple math and comparisons, then it shouldn't be too difficult to write the code to execute the task you've laid out.

Your original question was on the topic of determining whether your analog readings were increasing or decreasing, and that you've tried getting that to work but haven't gotten it quite right. So, what have you tried (ie, show us your code), and how exactly is it not quite right?

well, I have not tried it yet, because I am struggling on how to piece it together....I have all the steppers moving with some examples from the library, and I also will probably try to do it with servos, to see the difference...I also have example sketches working with the servo library..so my hookups, and for the most part, electronics are working as they should...

In other words, I am looking for a "skeleton" if you will, on the logic that I would use for the my loop to achieve this....

It probably needs to be nested somehow, and this is where I am struggling to piece it together, and hence my reach out for help....

Can someone take my statements above and provide a very rough example as to how the code would look like?

thank you again....

You did not really tell the whole story. You tell "how" you want to do "it". But we need to know what you want to do. Are you trying to build a servo from scratch? What is it that you are after?

[quote author=Udo Klein link=topic=102221.msg767103#msg767103 date=1334943776] You did not really tell the whole story. You tell "how" you want to do "it". But we need to know what you want to do. [/quote]

I have a similar impression. The whole story of "I'm building a ..." would be helpful in the logic. Often the logic is incorrect when the whole story is mentioned. Hopefully not in this case.

If you are reading fairly fast, just use a running average library on the play ground and compare current reading with the running average to determine inc/dec.

1. What is the expected minimum and maximum time between events?

2. If the analog values were plotted against time, do you expect a linear or non-linear graph? (If non-linear, what type of relationship are you expecting?)

3. Are you aware that there will be (in computer time) a significant delay between the time you acquire an analog reading and the time the stepper completes the indicated movement? One of the possibilities is to incorporate heuristic and/or predictive processing - but the (apparent) downside is that that kind of help can't be provided by "blindfolded" people...

[quote author=Morris Dovey link=topic=102221.msg767257#msg767257 date=1334954165] 1. What is the expected minimum and maximum time between events?

1. If the analog values were plotted against time, do you expect a linear or non-linear graph? (If non-linear, what type of relationship are you expecting?)

2. Are you aware that there will be (in computer time) a significant delay between the time you acquire an analog reading and the time the stepper completes the indicated movement? One of the possibilities is to incorporate heuristic and/or predictive processing - but the (apparent) downside is that that kind of help can't be provided by "blindfolded" people... [/quote]

--The expected time between min and max is short....lower than 1 second I would say....

--The results over time, for the most part, will be linear..ie a linear voltage rise, or voltage drop...

--Not worried much about the time it would take the stepper to move...some delay for this is OK, as I do not expect the change to happen instantaneously...ie I am willing to wait for the stepper to reach where it needs to go, with an adequate delay as required by the physical constrains of the stepper, it's speed, bias voltages currents etc...

Wow, I did not think this would be as hard as it sounds.....let me simplify it a bit in case it helps....

Let's assume this, or imagine it I guess.....

--I wire up a pot, say 10K to the analog inputs of the mega, and by varying it, I can vary the voltage across the pin between 0 and 5 volts..

Ok so far?

now, let assume, that I want to find the position that this pot will give me say 1.5 volts, through its rotation....

OK, now let's also assume that the shaft of this pot, is wired to a stepper motor....

we start out:

Pot is at some arbitrary point, and say at this point, it makes the analog pin read 2.5 volts...we want to get to 1.5 volts...

so we might start rotating the stepper CW...does the voltage go up?? NO? I guess we need to rotate the other way....does it go down? No, go the other way again.....etc

Keep doing this, until we reach the desired output, ie, a position that the pin will read 1.5 volts as we require, based on us rotating and TRACKING the motion of the stepper, on whether the voltage is going up or down....

In other words, mimic with the stepper, what a human would do, if asked the question: Find the position this pot will give you 1.5 volts, after starting at some arbitrary point...

I have tried to do this, but I am new to the arduino platform, and have not been able to find a close enough example to allow me to see what this logic looks like...this is what I am hoping some experts here might be able to provide.....

If you take the difference between regular (close to or same time between each) readings and then the difference between differences you can get a kind of serial second derivative (the change of change) that will point to trend.

However if you apply correction at the same speed your data can easily end up with a case of the 'wobbles'.

A good start would be just collect data, generate the differences and diff-of-diff values, print those out and see how it looks. Then hook in the reaction part and collect/display values to see what it's doing, knowing what the data looks like when you don't.

Does that help?

Another approach might be to acquire stepper position and voltage readings at both ends of the pot's range. Since it's linear, you can then interpolate to make an accurate estimate of desired stepper position, move the stepper to that position, take a voltage reading and fine tune (probably just a step or two in either direction) if needed.

OK...I am trying to follow what you guys are saying, but as my original question stated, I am struggling as to how to do something like this in the arduino development environment....

I know I might be asking for too much, and please excuse the request, but what would typical code look like for some logic like this...

all the averages, diff to diff, running averages, serial second derivitive? head spinning!!!

:-)

If you're reduced to fishing, it's time to step back from the task at hand and learn more about the more general nature of programming in the Arduino environment.

If the problem has your head spinning, then it's time to take a break from the implementing of a solution and think about what you have to work with and what, in general, you want to use it to achieve.

When you're more relaxed with the endeavor, try writing a complete functional specification as though you were were going to spend your own hard-earned money to have someone else (paid by the hour!) build your device for you.

(BTW, I'm a ShopBot owner - and I also have designed and built my own CNC router. I like the looks of your Stinger.)

cyborgcnc: all the averages, diff to diff, running averages, serial second derivitive? head spinning!!!

:-)

If your data is all coming in the same then there would be no difference between data points, right?

If the data is getting larger then the difference between data points, the last one subtracted from the one before, would be more than zero.

Example: data: difference, difference-of-difference 1 ............ no data before this to subtract from this so no difference or difference-of-difference 2: 1 ......... we see the data is increasing by 1. but no difference before this so no difference-of-difference 4: 2, 1 ...... we see the data increasing by 2 and we see that the increase is increasing by 1 7: 3, 1 12: 5, 2 ..... we see the increase is increasing by 2 16: 4, -1 .... we see the data increasing by 2 but now that increase is getting to be less 19: 3, -1 .... data still increasing but not as fast as before 21: 2, -1 .... if we drew a curve of the data it would still be going up but starting to 'hilltop'. 22: 1, -1 22: 0, -1 .... and here the data curve would be level 20: -2, -2 ... then it's going down

The difference is like rate of change of the data, +values are increasing and -values are decreasing. The difference-of-difference is like the rate that the difference itself is changing. If the data is not chaotic then the diff-of-diff (just abbreviating!) will tell you the trend farther ahead than just the difference will do.

By using the difference and difference-of-difference you can detect two levels of change to spot trends. You can take that as many levels as you want btw, but remember that the time between data points needs to be (at least close to) even for it to work.

Sorry about the terms, they aren't exactly standard and the approach is a bit rough. I just took an idea from calculus and applied it to data, have done so before to good ends even though it's not precisely calculus.

I have to repeat my question: what is it that you are after?

[quote author=Udo Klein link=topic=102221.msg767571#msg767571 date=1334991493] I have to repeat my question: what is it that you are after? [/quote]

I am after illumination, as to what the logic of a simple sketch would be to accomplish the example I described two posts ago...

I did not think that something like this would be so difficult to understand, nor that folks would be willing to provide so much lecturing, as to how to eat, sleep, think, and go about life.....other than an example as to how it might be done!

LOL!!

(BTW, I'm a ShopBot owner - and I also have designed and built my own CNC router. I like the looks of your Stinger.)

[/quote]

NICE!! A shopbot is a very nice machine, and I was actually considering it before building mine...however the somewhat "proprietary" control and software did not exactly appeal to me...I know folks driving them with mach3, and the machines are absolutely fantastic!

I am considering adding a fourth axis on mine, currently designing it solidworks as well, and we will see how that goes. Have been very happy with my design, it's rigidity, speed, and precision. If I was going to do it again, I would make the sides stiffer, this way I would be able to cut light metals, but for wood, so far the machine has performed beyond my wildest expectations!