4
\$\begingroup\$

I am making an IOT weather station based on a particle photon that sends data via webhook to one of my other projects. I am very new to c++ and programming in general.

What can I do better with my code? What can be optimised? And what are industry best practices that I can put into place?

Here's my code:

#include <Adafruit_DHT/Adafruit_DHT.h>

#define DHTPIN 2 

#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
Serial.begin(9600);
dht.begin();
}

void loop() {

delay(2000);

//Variables
//DHT11 sensor floats
float h = dht.getHumidity();
float t = dht.getTempCelcius();
float f = dht.getTempFarenheit();
float hi = dht.getHeatIndex();
//UV sensor floats
float sensorValue = analogRead(A0);
int UVLevel = 0;

//Error Retry
if (isnan(h) || isnan(t) || isnan(f)) 
{
Serial.println("Failed to read from DHT sensor!");
return;
}

if(sensorValue <=10)
{
     UVLevel = 0;
}
if(sensorValue <= 46 && sensorValue > 10)
{
     UVLevel = 1;
}
if(sensorValue <= 65 && sensorValue > 46)
{
     UVLevel = 2;
}
if(sensorValue <= 83 && sensorValue > 65)
{
     UVLevel = 3;
}
if(sensorValue <= 103 && sensorValue > 83)
{
     UVLevel = 4;
}
if(sensorValue <= 124 && sensorValue > 103)
{
     UVLevel = 5;
}
if(sensorValue <= 142 && sensorValue > 124)
{
     UVLevel = 6;
}
if(sensorValue <= 162 && sensorValue > 142)
{
     UVLevel = 7;
}
if(sensorValue <= 180 && sensorValue > 162)
{
     UVLevel = 8;
}
if(sensorValue <= 200 && sensorValue > 180)
{
     UVLevel = 9;
}
if(sensorValue <= 221 && sensorValue > 200)
{
     UVLevel = 10;
}
if(sensorValue <= 240 && sensorValue > 221)
{
     UVLevel = 11;
}
if(sensorValue > 240)
{
     UVLevel = 12;
}

//Serial Print DHT
Serial.println();
Serial.println();
Serial.print("Humid: ");
Serial.print(h);
Serial.print("%");
Serial.println();
Serial.print("Temp: ");
Serial.print(t);
Serial.print("C ");
Serial.println();
Serial.print("Apparent Temperature: ");
Serial.print(hi);
Serial.println();
Serial.println();

//Serial Print UV
Serial.print("UV Level =");
Serial.print(UVLevel);
Serial.println();

//Publish Data To Particle Cloud
Particle.publish("Humidity", String(h));
Particle.publish("Temperature", String(t));
Particle.publish("Apparent Temperature", String(hi));
Particle.publish("UV Index",String(UVLevel));
delay(5000);
}

For reference these are the sensors I am using:

DHT 11 Temp/Humidity sensor

UV sensor

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Indentation seems off, is this on purpose or a mistake from formatting? \$\endgroup\$
    – yuri
    May 14 '18 at 16:13
  • \$\begingroup\$ The code is as is in my editor. Now you mention it, it does look odd. @yuri \$\endgroup\$
    – Xander
    May 14 '18 at 16:28
  • 1
    \$\begingroup\$ @Xander Replace TAB characters by four spaces first before copying your code into the question. \$\endgroup\$ May 14 '18 at 16:46
6
\$\begingroup\$

I'll assume the indentation is an artifact of pasting the code here.

The big problem here is that long series of if statements. A simple improvement is to make use of the else clause, along with knowledge of what values you've tested for already, it can be simplified to

if (sensorValue <= 10)
    UVLevel = 0;
else if (sensorValue <= 46)
    UVLevel = 1;
// etc.

But we can do better. Since the range of values you're checking with is contiguous, and the result (UVLevel) is linear, we can set up an array to hold the data, then just use a loop to find the right place.

static values[] = { 10, 46, 65, /* ... */, 240 };
UVLevel = std::size(values);    // 12
for (int i = 0; i < std::size(values); ++i)
    if (values[i] <= sensorValue) {
        UVLevel = i;
        break;
    }
}

We initially set UVLevel the the maximum value so that if the sensor value is greater than 240 we get the correct result without having to check after the loop.

For a larger range of possible values, the for loop used here can be replaced with a more complicated binary search.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.