Hello,
I am working on a project that involves 4 sensors: vibration, TDS, water, and turbidity. I went ahead and coded each sensor individually to see if they all work and have added them to a dashboard that I am making. The vibration, TDS, and water sensors are working great with the dashboard, however, I am not getting a read from my turbidity sensor even though I have tested it individually. In the individual test, I am getting read from the serial console, but with the combined code, IT IS NOT WORKING AT ALL! In the code below, you can see that it is already declared as a float value since that is what I want the gauge to display. I'm super sad that it is not reading even though it works by itself. Can anyone help me troubleshoot?
Here is my code from the full program:
/*
The following variables are automatically generated and updated when changes are made to the Thing
float tdsValue;
float turbidityValue;
int vibrationSensor;
bool waterLevel;
*/
#include "thingProperties.h"
#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 = 0,temperature = 25;
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(115200);
//water sensor
pinMode(34,INPUT);
//TDS sensor
pinMode(33, INPUT);
//vibration Sensor
pinMode(32, INPUT);
//turbidity Sensor
pinMode(25, INPUT);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
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();
}
void loop() {
ArduinoCloud.update();
// Water level code
waterLevel=digitalRead(34);
//vibration sensor code
vibrationSensor = TP_init();
/*if(vibrationSensor>15000)
{
spill= true;
}
else
{
spill = false;
}
*/
//Turbidity Sensor Code
turbidityValue = analogRead(25)*(5.0/4095.0);
//TDS code
static unsigned long analogSampleTimepoint = millis();
if (millis() - analogSampleTimepoint > 40U) //every 40 milliseconds,read the analog value from the ADC
{
analogSampleTimepoint = millis();
analogBuffer[analogBufferIndex] = analogRead(33); //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; // read the analog value more stable by the median filtering algorithm, and convert to voltage value
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; //convert voltage value to tds value
}
}
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;
}
int TP_init(){
int measurement=pulseIn(32, HIGH); //wait for the pin to get HIGH and returns measurement
return measurement;
}
Again, I'm not sure why this is happening SOMEONE PLEASE HELP :'((