Main Site Documentation

AnalogInput


#1

Hello,

In FEZ Domino how do I read an analog input into a cycle of repetition?


#2

The best for you is to begin with the beginner tutorial and beginer’s ebook.
Both are available here : http://tinyclr.com/dl/
You will find there most answers to start with FEZ :slight_smile:


#3

A code snippet here you’ll find. It shows one way to do it. ADC pin A0 is read into an array with a delay between points. Nothing sacred about the 117 points or the delay. This is just one option and not necessarily the most elegent. Good luck.

                    AnalogIn ai = new AnalogIn(AnalogIn.Pin.Ain0);  //instantiate ADC pin
                    int k = 0;
                    int[] ai_vals = new int[117];
                    for (k = 0; k <= 116; k++) // quickly collect 117 data points with fixed delay between
                    {
                        i = 0;
                        ai_vals[k] = ai.Read();
                        while (i < 12)  // This while loop sets the delay between samples 
                        {
                            i++;
                        }
                    }

Forgot to mention earlier that since C# .NETMF isn’t real time, garbage collection and other system calls can interfere with the sample timing. You should make sure your analysis can tolerate those types of perturbations in the digitized signal. In my case garbage collection takes about 3ms and is pretty spaced out so I can tolerate it.


#4

Thanks for the help but notice the 100% this example, why do you use 116 and 117?!


#5

Not sure what you mean by “100%”.

As for 116 and 117, these values accomplished what was needed in this particular program. You can make them equal and use any value up to the limits of C# arrays.


#6

Ok, but what makes this small proceeds exactly the same code? can you give a little simple explanation?
Thanks


#7

I am not really sure what your last question really is.

Originally you asked for a sample program that will collect samples in a loop - the above does that, for 117 samples. If you now need to have the program explained, I suspect you’re new to C# and you probably should check out the getting started guide as your first step, work through it (click the Downloads & Tutorials menu above to find many resources including the beginners guide).


#8

Yes I am new to programming in C # so I am here asking for help to do my job, thank you for your attention and I hope you will continue to help.
Thanks


#9

We all are here to help but new users need to help us so we can help them better. For example, read the free ebook and go through this page in details http://www.tinyclr.com/support/

For beginners, everything they need is probably already on that page :slight_smile:


#10

Idafez,

In the preceding example does not have the variable “i” declared but why do you use an array?


#11

In the snippet of code I posted earlier I left out the line that declared the i variable - sorry if that was confusing.

An array provides a simple way for me to process the data after it is collected. In my application the time rate of change of the signal is important so I can use the array values knowing that they were sampled with a roughly known delay between samples. The caveat is that C# isn’t real time so your analysis has to be able to tolerate an occasional 3ms garbage collection that will extend the time between two samples occasionally. My app can stand that.

If your signal is slowly varying you may not need to use the array and could just sample and analyze in a loop. All depends on the what bandwidth is required to faithfully capture the information you seek. If you want a more complex description you need to satisfy the Nyquist criteria to prevent aliasing of your signal.

From your other post, “FEZ DOMINO, READ AN ANALOG INPUT IN A CYCLE”, it sounds like you might have a slowly varying signal, and can use a relatively slow sample rate.