I am reading a sine wave using the following code:
#include <LCD4Bit_mod.h>
#include <Arduino.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
void setup()
{
Serial.begin(128000);
Serial.println("CLEARDATA");
Serial.println("LABEL, Time, Reading");
pinMode(9, INPUT);
lcd.init();
lcd.clear();
}
void loop()
{
int array[2500] = {0};
int row = 0;
int average = 0;
for(int i = 0; i < 2500; i++)
{
array[i] = analogRead(9);
delay(5);
}
for(int x = 0; x < 2500; x++)
{
Serial.print("DATA,TIMER,");
Serial.println(array[x]);
row++;
}
delay(5000);
}
My goal is find the frequency of the wave. It works well, but it seems the Arduino thinks about something else briefly every 200-250 data points, which makes the reading discontinuous. (The array is continuous, but I need good time to determine the frequency.) Here is the data in Excel:
I have only been seriously using Arduino for a few months, so my skills at debugging this are limited.
That looks like the error is in the drawing rather than the sampling. Each of the gaps is discontinuous with respect to time because two consecutive samples are spaced much further apart than they should be.
For example, with the gap at 3.8secs, if you were to take all the samples to the right of this gap and move them to the left, the gap would close and the two samples which make up the gap would "fit" together properly.
Since you are not storing the sample time, you assume every sample is 5ms from the prior. So how can there be a discontinuity in the time dimension on this plot at all? I think the problem is with the plot as well. Let's see the sample data that was sent from the ardunino to your serial monitor. I have to assume you saved that.,
Right... When you store a digital waveform, it's a one-dimensional array and there is no "time". There is simply a sequence of values in your array. (You did it the "right" way.)
You have to know the sample rate (timing) in order to re-construct the waveform at the correct frequency. (A WAV audio file has the sample rate stored in the header.)
You do know the sample rate, but something's wrong with the timing in your plot. So, although your array values are correct the waveform-timing is getting re-constructed incorrectly.
That's going to use rather a lot of memory. How much memory does your Arduino have?
Where does the 'time' axis in your plot come from? One possible explanation for the discontinuity is that either the sender or the receiver are pausing for some reason at regular intervals. I notice that this happens after about 256 samples which is obviously a significant number. But, for this to affect the graph the receiver would have to be timestamping the value at the point it was received from the Arduino. Since the Arduino does not send the results in real time, for the receiver to do this would utterly defeat what you're trying to achieve. It should be timestamped based on the known interval between samples in your sketch. Actually I don't know exactly what the sample interval is since you aren't controlling that, so the best we cansay is that it is 5ms ish per sample. If you aren't looking for high accuracy, you can probably afford to ignore the 'ish'.
for(int i = 0; i < 2500; i++)
{
array[i] = analogRead(9); // this takes something like 105 usec
delay(5);
}
If you look at how Blink Without Delay works or find Morris Dovey's Timer Library then you can get reads on a set time basis without wasting most of the time in do-nothing delays. That time can be spent outputting data (as integers, not ASCII text) as fast as it is being read. 5 ms is a long time at 115200 baud.