Hello everyone. I’m working a project using a light sensor, Photodiode, to measure light photon signal. the output voltage from amplifier is an analog pulse signal(Voltage) has duration with various time (100-1000 millisecond) and I want to record data to the array.
The Arduino start to receive signal from the sensor by Pin A0 (analogRead(A0)>=5) and stop when analogRead(A0)<5 with a sampling rate 25 ms. I stored the data to arrays in NUM_READINGS = 40 (total time = 25ms * 40 = 1000 ms for one pulse)
If time period of signal is 1000 ms the output in serial monitor show :
taking sample num 1
taking sample num 2
taking sample num 3
taking sample num 4
.
.
.
taking sample num 40
numReadings = 40
analogVals = [15, 35, 55, 55, 55, 56, 55, 55, 56, 56, 55, 55, 55, 55, 54, 55, 55, 55, 56, 56, 56, 55, 55, 55, 55, 55, 56, 55, 56, 55, 55, 55, 56, 55, 55, 55, 56, 55, 55, 35]
=====Hear is the problem: ==========
I has problem when the signal is lower than 1000 ms (let's say it's 200ms). The program will stop at 200
ms (NUM_READINGS = 8 ) and cannot run to the end of array. The output show. :
taking sample num 1
taking sample num 2
taking sample num 3
taking sample num 4
.
.
.
taking sample num 8
Can someone help me to resolve the problem to complete the array.
taking sample num 1
taking sample num 2
taking sample num 3
taking sample num 4
.
.
.
taking sample num 40
numReadings = 40
analogVals = [15, 35, 55, 55, 55, 56, 55, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bool takeAnalogReadings(uint16_t* p_numReadings = nullptr, uint16_t** p_analogVals = nullptr);
void setup()
{
Serial.begin(115200);
Serial.println("\nBegin\n");
}
void loop()
{
if (analogRead(A0)>=5)
{
//take readings and read out the values when the buffer is full
uint16_t numReadings;
uint16_t* analogVals;
bool readingsDone = takeAnalogReadings(&numReadings, &analogVals);
if (readingsDone)
{
Serial.print("numReadings = "); Serial.println(numReadings); //print the total number of readings
Serial.print("analogVals = [");
for (uint16_t i=0; i<numReadings; i++) //print data value to array
{
if (i!=0)
{
Serial.print(", ");
}
Serial.print(analogVals[i]);
}
Serial.println("]");
}
}
}
bool takeAnalogReadings(uint16_t* p_numReadings, uint16_t** p_analogVals)
{
static const uint16_t NUM_READINGS = 40;//total number of reading
static uint16_t i = 0; // index
static uint16_t analogVals[NUM_READINGS];
const uint32_t SAMPLE_PD = 25; // ms; sampling period
static uint32_t tStart = millis(); // ms; start time
bool bufferIsFull = false; // set to true each time NUM_READINGS have been taken
// Only take a reading once per SAMPLE_PD
uint32_t tNow = millis(); // ms; time now
if (tNow - tStart >= SAMPLE_PD)
{
Serial.print("taking sample num "); Serial.println(i + 1);
tStart += SAMPLE_PD; // reset start time to take next sample at exactly the correct pd
analogVals[i] = analogRead(A0);
i++;
if (i >= NUM_READINGS)
{
bufferIsFull = true;
i = 0; // reset to beginning of array
}
}
if (p_numReadings != nullptr)
{
*p_numReadings = NUM_READINGS;
}
if (p_analogVals != nullptr)
{
*p_analogVals = analogVals;
}
return bufferIsFull;
}