Ik wil de analog waarden van A0 uitlezen en in een array plaatsen. Later wil ik daar de min en max uithalen.
Ik heb een voorbeeld gevonden op internet en een beetje aangepast voor mij. (origineel draait alles in de loop()). Ik heb dit opgesplitst naar array vullen en array uitprinten.
De bedoeling is dat ik later meer dan 1 ingang wil uitlezen (A0-A5).
Wat ik niet begrijp in dit programma is dat het ongeveer 50sec duurt voor er iets in de console komt en om de 50 seconden pas opnieuw een volgende waarde geprint wordt. Als ik de wachtlus (via millis()) uit de loop haal gaat dit allemaal sneller.
Waarom wordt de loop niet elke seconde (1000 millis()) opnieuw gestart?
Dank u
Herman
Voorbeeld van internet: Arduino fill array with values from analogRead - Stack Overflow
Code:
bool takeAnalogReadings(uint16_t* p_numReadings = nullptr, uint16_t** p_analogVals = nullptr);
bool readingsDone=false;
bool gedaan=false;
uint16_t numReadings;
uint16_t* analogVals;
unsigned long eventInterval = 1000;
unsigned long previousTime = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
unsigned long currentTime = millis();
if (currentTime - previousTime >= eventInterval)
{
if (readingsDone==false) vullen(); /*** array vullen ******/
if (gedaan==true) LaatZien(); /*** array uitprinten ******/
previousTime = currentTime;
}
readingsDone=false;
gedaan=false;
}
// ****** Functies
void vullen()
{
readingsDone = takeAnalogReadings(&numReadings, &analogVals);
gedaan=readingsDone;
}
void LaatZien()
{
readingsDone=true;
gedaan=false;
Serial.print("analogVals: ");
for (uint16_t i=0; i<numReadings; i++)
{
Serial.print(analogVals[i]);
Serial.print(",");
}
Serial.println();
}
//---------------------------------------------------------------------------------------------------------------------
// Take analog readings to fill up a buffer.
// Once the buffer is full, return true so that the caller can read out the data.
// Optionally pass in a pointer to get access to the internal buffer in order to read out the data from outside
// this function.
//---------------------------------------------------------------------------------------------------------------------
bool takeAnalogReadings(uint16_t* p_numReadings, uint16_t** p_analogVals)
{
static const uint16_t NUM_READINGS = 50;
static uint16_t i = 0; // index
static uint16_t analogVals[NUM_READINGS];
const uint32_t SAMPLE_PD = 1; // ms; sample period (how often to take a new sample)
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)
{
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;
}
}
// Assign the user-passed-in pointers so that the user can retrieve the data if they so desire to do it this way
if (p_numReadings != nullptr)
{
*p_numReadings = NUM_READINGS;
}
if (p_analogVals != nullptr)
{
*p_analogVals = analogVals;
}
return bufferIsFull;
}