So this is kind of complex, I'll do my best to be concise.
Originally, in my program I have something reading input voltage, assigning it as a float variable to an array. Then the array is printed. However, I need the read process to be as fast as possible, and I noticed in testing that there is a discrepancy between the assigned sampling rate and the actual S.R.. This is, I assume due to the time required to make the float conversion. So I had the idea to switch to long int. However, when I tried to print the long int straight to output, I got lots of 0s. So I thought maybe making a float conversion then printing (all after the data has been taken) would work. And thats where I'm at. My problem is that after making what seems to be the correct conversion syntax (at least from what Doug and most of the internet says) I get only a long list of very precise 0s. Here is the original (fully functional) code, and below are the changes I have tried to make, but have failed. I'll list some example outputs below each as well.
//------------------------------
//Simple read and store program
//James K. Ehrman, July 29 2010
//------------------------------
#include "mbed.h"
#define ON 1
#define OFF 0
LocalFileSystem local("local");
AnalogIn myinput(p18);
float V[1000];
int count = 0;
int i = 0;
int main() {
FILE *fp = fopen("/local/analog.txt","w"); // Open a file to save samples
for (i=0;i<1000;++i) {
V[i] = myinput.read(); // Read analog (range 0.0 to 1.0)
wait_us(500);
}
i = 0;
for (i=0;i<1000;++i) {
fprintf (fp, "%3i: %f \r\n", i, V[i]);
}
fclose(fp);
}
which yields such values:
29: 0.000000
30: 0.000000
31: 0.000000
32: 0.000000
33: 0.000000
34: 0.000000
35: 0.000000
36: 0.000000
37: 0.339194
38: 0.339194
39: 0.338950
40: 0.339194
41: 0.338706
42: 0.338950
43: 0.338950
44: 0.339194
45: 0.339194
Here are the changes I have tried:
#include "mbed.h"
#define ON 1
#define OFF 0
LocalFileSystem local("local");
AnalogIn myinput(p18);
long V[1000];
int count = 0;
int i = 0;
int main() {
FILE *fp = fopen("/local/analog.txt","w"); // Open a file to save samples
for (i=0;i<1000;++i) {
V[i] = myinput.read(); // Read analog (range 0.0 to 1.0)
wait_us(500);
}
i = 0;
for (i=0;i<1000;++i) {
float F = float(V[i]);
fprintf (fp, "%3i: %f \r\n", i, F);
}
fclose(fp);
}
which yields:
0: 0.000000
1: 0.000000
2: 0.000000
3: 0.000000
4: 0.000000
5: 0.000000
6: 0.000000
7: 0.000000
8: 0.000000
9: 0.000000
10: 0.000000
And it is definitely not the case, as may be possible in the latter set that I may be only observing a stage where the signal is 0 (a trough), all 1000 values are 0.000...
*EDIT*
I mentioned earlier that printing direct long int did not work, here is the code and output:
#include "mbed.h"
#define ON 1
#define OFF 0
LocalFileSystem local("local");
AnalogIn myinput(p18);
long V[1000];
int count = 0;
int i = 0;
int main() {
FILE *fp = fopen("/local/analog.txt","w"); // Open a file to save samples
for (i=0;i<1000;++i) {
V[i] = myinput.read(); // Read analog (range 0.0 to 1.0)
wait_us(500);
}
i = 0;
for (i=0;i<1000;++i) {
fprintf (fp, "%3i: %i \r\n", i, V[i]);
}
fclose(fp);
}
output:
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
10: 0
I heard that there is special significance to the fact that these are now 0 instead of 000000000000.
Hi,
I was curious as to how accurately the Mbed could read and store analog frequency data. I ran a few tests, and found that the data was gibberish. In general I get a file full of values you might expect from a square wave, a list of 0s then a list of 0.7s. However analyzing the data reveals some strange results. My design is fairly simple, the program is included at the end. I have a function generator hooked up to an analog input. The Mbed is grounded to the same ground as the input ground. I have the generator set to a steady 10 or 20 Hz square wave (verified by oscilloscope) and properly offset so that the bottom of the square wave is truly 0, ground. The output data file looks as it should but once I try to find the input frequency from this data, I get insane results. My method is this:
count the number of data points taken in a full oscillation of the wave (from the beginning of a trough to the beginning of the next trough) and multiply is by the sampling rate set in the program. That yields the period of the wave, and the reciprocal of that is the frequency. So here is a table of values I got from this method.
@10Hz, square wave
@20Hz, square wave
So clearly something is wrong. Does anyone have experience using the Mbed for frequency measurements and encountered this problem or done something differently?
Here is the program I used, written by D. Wendelboe, thanks