How to store sensor value to array after a few seconds

Hello everyone,

Im currently trying to read sensor data for a few seconds using millis and then store it to array, but it seems didnt work. On the serial it just keeps reading. Im using arduino uno and DFRobot TDS sensor for a reference

#define TdsSensorPin A1
#define VREF 5
#define SCOUNT 30
int analogBuffer[SCOUNT];
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0,copyIndex = 0;
float averageVoltage = 0,tdsValue = 0,temperature = 25;

byte arrayTDS[30];
byte valueTDS;

unsigned long currentMillis;
unsigned long startMillis;
long interval = 5000;
int timeX = 0;

void setup()
{
    Serial.begin(115200);
    pinMode(TdsSensorPin,INPUT);

    startMillis = millis();
    Serial.print("Time internal of readings : ");
    Serial.print(interval);
    Serial.println(" milliseconds");
}

void loop()
{
   static unsigned long analogSampleTimepoint = millis();
   if(millis()-analogSampleTimepoint > 40U)
   {
     analogSampleTimepoint = millis();
     analogBuffer[analogBufferIndex] = analogRead(A1);
     analogBufferIndex++;
     if(analogBufferIndex == SCOUNT) 
         analogBufferIndex = 0;
   }   
   static unsigned long printTimepoint = millis();
   if(millis()-printTimepoint > 2000U)
   {
      printTimepoint = millis();
      for(copyIndex=0;copyIndex<SCOUNT;copyIndex++)
        analogBufferTemp[copyIndex]= analogBuffer[copyIndex];
      averageVoltage = getMedianNum(analogBufferTemp,SCOUNT) * (float)VREF / 1024.0;
      float compensationCoefficient=1.0+0.02*(temperature-25.0);
      float compensationVolatge=averageVoltage/compensationCoefficient;
      tdsValue=(133.42*compensationVolatge*compensationVolatge*compensationVolatge
      - 255.86*compensationVolatge*compensationVolatge + 857.39*compensationVolatge)*0.5;
      Serial.print("TDS Value:");
      Serial.println(tdsValue,0);

   currentMillis = millis();
   if (currentMillis - startMillis >= interval){
    arrayTDS[valueTDS] = tdsValue;
    valueTDS++;
   }
    Serial.print("Hasil Tangkapan Sensor : ");
    Serial.print(arrayTDS);
}
}
int getMedianNum(int bArray[], int iFilterLen) 
{
      int bTab[iFilterLen];
      for (byte i = 0; i<iFilterLen; i++)
      bTab[i] = bArray[i];
      int i, j, bTemp;
      for (j = 0; j < iFilterLen - 1; j++) 
      {
      for (i = 0; i < iFilterLen - j - 1; i++) 
          {
        if (bTab[i] > bTab[i + 1]) 
            {
        bTemp = bTab[i];
            bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
         }
      }
      }
      if ((iFilterLen & 1) > 0)
    bTemp = bTab[(iFilterLen - 1) / 2];
      else
    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
      return bTemp;
}

this error report is a bit vauge! is anything printed to the serial monitor?
as an initial test put some print statements in the first loop to check values such as analogBufferIndex, etc have the values you expect

Your sketch does not get compiled. In addition to the the absence of the declaration of valueTDS variable, there is also a lack of { balancing.

what do you want to do with generated array?

#define TdsSensorPin A1
#define VREF 5
#define SCOUNT 30
int analogBuffer[SCOUNT];
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;
float averageVoltage = 0.0, tdsValue = 0.0, temperature = 25.0;

unsigned long arrayTDS[30];
byte valueTDSbyte=0;

unsigned long startMillis;
unsigned long interval = 5000;
int timeX = 0;

void setup(){
  Serial.begin(115200);
  pinMode(TdsSensorPin, INPUT);

  startMillis = millis();
  Serial.print("Time internal of readings : ");
  Serial.print(interval);
  Serial.println(" milliseconds");
}

void loop(){
  static unsigned long analogSampleTimepoint = millis();
  if (millis() - analogSampleTimepoint > 40UL)  {
    analogSampleTimepoint = millis();
    analogBuffer[analogBufferIndex] = analogRead(A1);
    analogBufferIndex++;
    if (analogBufferIndex == SCOUNT) analogBufferIndex = 0;
  }
  static unsigned long printTimepoint = millis();
  if (millis() - printTimepoint > 2000UL)  {
    printTimepoint = millis();
    for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
      analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
    averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0;
    float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0);
    float compensationVolatge = averageVoltage / compensationCoefficient;
    tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge
                - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5;
    Serial.print("TDS Value:");
    Serial.println(tdsValue, 0);

    if ( millis()- startMillis >= interval) {
      arrayTDS[valueTDSbyte] = (uint32_t)tdsValue;
      valueTDSbyte++;
      if(valueTDSbyte==30)valueTDSbyte=0;
    }
    Serial.print("Hasil Tangkapan Sensor : ");
    for (byte i = 0; i < 30; i++)Serial.println(arrayTDS[i]);
  }
}
int getMedianNum(int bArray[], int iFilterLen){
  int bTab[iFilterLen];
  for (byte i = 0; i < iFilterLen; i++)    bTab[i] = bArray[i];
  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++)  {
    for (i = 0; i < iFilterLen - j - 1; i++)    {
      if (bTab[i] > bTab[i + 1])
      {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }
  if ((iFilterLen & 1) > 0)    bTemp = bTab[(iFilterLen - 1) / 2];
  else    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  return bTemp;
}

Well i also going to calculate the average of the array data

which array, where you want to place the result?

The arrayTDS one, the tds sensor is one of my sensors that im working on my project for my hydroponic doping system, at first the sensor read for a few seconds, then calculate the average, compare it with source data to see if it less or not, so the result will be stored first then compare it with source data that stored in another array. There's a lot of array in my project

#define TdsSensorPin A1
#define VREF 5
#define SCOUNT 30
int analogBuffer[SCOUNT];
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;
float averageVoltage = 0.0,  temperature = 25.0;

unsigned long arrayTDS[30];
byte TDSindex = 0;

const unsigned long interval = 5000;

void setup() {
  Serial.begin(115200);
  pinMode(TdsSensorPin, INPUT);

  Serial.print("Time internal of readings : ");
  Serial.print(interval);
  Serial.println(" milliseconds");
}

void loop() {
  static unsigned long analogSampleTimepoint = millis();
  if (millis() - analogSampleTimepoint > 40UL)  {
    analogSampleTimepoint = millis();
    analogBuffer[analogBufferIndex] = analogRead(A1);
    analogBufferIndex++;
    if (analogBufferIndex == SCOUNT) {
      Serial.println("analogBuffer is full");
      analogBufferIndex = 0;
    }
  }
  static unsigned long printTimepoint = millis();
  if (millis() - printTimepoint > 2000UL)  {
    printTimepoint = millis();
    uint32_t TDSvalue = CalcS();
    Serial.print("TDS Value:");
    Serial.println(TDSvalue);
    arrayTDS[TDSindex] = TDSvalue;
    TDSindex++;
    if (TDSindex == SCOUNT) {
      TDSindex = 0;
      static unsigned long Result = 0;
      Serial.println();
      Serial.println("Hasil Tangkapan Sensor : ");
      for (byte i = 0; i < SCOUNT; i++) {
        Serial.println(arrayTDS[i]);
        Result += arrayTDS[i];
      }
      Result /= SCOUNT ;
      Serial.print("Average : "); Serial.println(Result);
    }
  }
}

uint32_t CalcS() {
  for (copyIndex = 0; copyIndex < SCOUNT; copyIndex++)
    analogBufferTemp[copyIndex] = analogBuffer[copyIndex];
  averageVoltage = getMedianNum(analogBufferTemp, SCOUNT) * (float)VREF / 1024.0;
  float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0);
  float compensationVolatge = averageVoltage / compensationCoefficient;
  return (uint32_t)((133.42 * compensationVolatge * compensationVolatge * compensationVolatge
                     - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5);
}

int getMedianNum(int bArray[], int iFilterLen) {
  int bTab[iFilterLen];
  for (byte i = 0; i < iFilterLen; i++)    bTab[i] = bArray[i];
  int i, j, bTemp;
  for (j = 0; j < iFilterLen - 1; j++)  {
    for (i = 0; i < iFilterLen - j - 1; i++)    {
      if (bTab[i] > bTab[i + 1])
      {
        bTemp = bTab[i];
        bTab[i] = bTab[i + 1];
        bTab[i + 1] = bTemp;
      }
    }
  }
  if ((iFilterLen & 1) > 0)    bTemp = bTab[(iFilterLen - 1) / 2];
  else    bTemp = (bTab[iFilterLen / 2] + bTab[iFilterLen / 2 - 1]) / 2;
  return bTemp;
}

Hello ghoond
Take some time and check the handling of the variables for the timers.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.