Saltatu nabigazioa

Sketch programatu

Sketch: programazioa

Koadro honetan ikus dezakezu liburutegia kargatu ondoren Arduino MKR Wifi 1010 plakara bidaliko dugun programa:

/*

TDSk (disolbatutako solidoak guztira) litro bat uretan disolbatzen diren solido disolbagarrien zenbat miligramo diren adierazten du. Oro har, zenbat eta TDS balio handiagoa izan, orduan eta solido disolbagarri gehiago disolbatzen dira uretan eta ura zikinagoa izango da.

Erabili ADS1115 16-Bit ADC modulua (ASIN: B08KFZ3PVT, Model: CQRADS1115)

TDS tresnaren sentsorea erabiltzen denean, sentsorearen zunda likidoan murgiltzen da, eta sentsorearen zundaren babes-estalkia kendu behar da, datuen irakurketa okerra eta funtzionamendu okerra saihesteko.

Sketch generated by the Arduino IoT Cloud Thing "p03"
https://create.arduino.cc/cloud/things/9176448a-c5af-4830-8d7a-8119f8eda4f6

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are made to the Thing

float ec;
float tdsValue;

Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

#define TdsSensorPin A1
#define VREF 5.0 // analog reference voltage(Volt) of the ADC
#define SCOUNT 30 // sum of sample point

int analogBuffer[SCOUNT]; // store the analog value in the array, read from ADC
int analogBufferTemp[SCOUNT];
int analogBufferIndex = 0, copyIndex = 0;

float averageVoltage;
float temperature;

void setup() {
// Hasieratu seriea eta itxaron ataka ireki arte:
Serial.begin(9600);

// Atzerapen honek serieko monitore baten zain egoteko aukera ematen du blokeatu gabe, ez bada aurkitu
delay(1500);

// thingProperties-en definitua.h
initProperties();

// aldagaien hasierako balioa

averageVoltage = 0;
tdsValue = 0;
temperature = 25;

// Konektatu Arduino IoT Cloud-era
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();

Serial.begin(115200);
pinMode(TdsSensorPin, INPUT);

}

void loop() {
ArduinoCloud.update();
// Hemen zure kodea

// Arduino Cloud plataformarako konexioa probatu

if (ArduinoCloud.connected() == 0) {
// Ez dago konexiorik
Serial.println("Ez dago konexiorik Arduino Cloud plataformarekin");
Serial.println();
digitalWrite(LED_BUILTIN, LOW);

} else {
// Konexiorik badago
Serial.println("Arduino Cloud plataformarako konexiorik badago");

digitalWrite(LED_BUILTIN, HIGH);
}

// sentsore kodea

static unsigned long analogSampleTimepoint = millis();
if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(TdsSensorPin); //read the analog value and store into the buffer
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; // balio analogikoa egonkorragoa irakurri, medianaren iragazketa-algoritmoaren bidez, eta tentsio-balioan bihurtu
float compensationCoefficient = 1.0 + 0.02 * (temperature - 25.0); //temperature compensation formula: fFinalResult(25^C) = fFinalResult(current)/(1.0+0.02*(fTP-25.0));
float compensationVolatge = averageVoltage / compensationCoefficient; //temperature compensation
tdsValue = (133.42 * compensationVolatge * compensationVolatge * compensationVolatge - 255.86 * compensationVolatge * compensationVolatge + 857.39 * compensationVolatge) * 0.5; //tentsio-balioa tds-ko balio bihurtu

// Serial.print("voltage:");
// Serial.print(averageVoltage,2);
// Serial.print("V ");

Serial.print("Valor medido de TDS:");
Serial.print(tdsValue, 0);
Serial.println("ppm");
Serial.println("--------------");
delay(1500);

}
}

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;

}

}

Adi!

Gogoratu:

  1. Google Chrome nabigatzailearekin lan egin behar dugu.
  2. Programa plakara bidali behar da goiko aldean dagoen gezi itxurako botoia erabiliz.
Programa Arduinora bidaltzeko botoia
Mercedes Jimeno Badiola (Jabari publikoa)

Creado con eXeLearning (Ventana nueva)