Page 178 - MDP2020-2
P. 178

|    인천전자마이스터고등학교  ·············································································································
            172


                나.  tds  센서


            #define  TdsSensorPin  A1
            #define  VREF  5.0
            #define  SCOUNT    30

            int  analogBuffer[SCOUNT];
            int  analogBufferTemp[SCOUNT];
            int  analogBufferIndex  =  0,copyIndex  =  0;
            float  averageVoltage  =  0,tdsValue  =  0,temperature  =  25;



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



            void  loop()
            {
                  static  unsigned  long  analogSampleTimepoint  =  millis();
                  if(millis()-analogSampleTimepoint  >  40U)
                  {

                      analogSampleTimepoint  =  millis();
                      analogBuffer[analogBufferIndex]  =  analogRead(TdsSensorPin);
                      analogBufferIndex++;
                      if(analogBufferIndex  ==  SCOUNT)

                              analogBufferIndex  =  0;
                  }
                  static  unsigned  long  printTimepoint  =  millis();
                  if(millis()-printTimepoint  >  800U)
                  {
                        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;
   173   174   175   176   177   178   179   180   181   182   183