How to execute "if" statements simultaneously

Hi all, I have been working on a weather IOT Station, and I am trying to use the messenger widget in the IOT Cloud dashboard to send me a message when temperature/humidity/light intensity/pH get either too high or too low. However, I have been having troubles executing it. The if statements successfully work for one variable, but do not respond to the other.

Here is my code:

// DHT sensor library - Version: Latest 
#include <DHT.h>
#include <DHT_U.h>

// Adafruit NeoPixel - Version: Latest 
#include <Adafruit_Sensor.h>

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/b3722b54-8d53-4029-ad54-e89e684ac0d2 

  Arduino IoT Cloud Variables description

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

  String message;
  float humidity;
  float pHVal;
  float temperature;
  int blueVal;
  int greenVal;
  int light;
  int redVal;

  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"


int greenPin=2; 
int redPin=3;
int bluePin=4;
int PHPIN= A1;
int lightPin=A2;
float Voltage;
int Offset=0;
#define DHTPIN 1
#define DHTTYPE DHT11
DHT dht (DHTPIN,DHTTYPE);

void setup() {
  // Initialize serial and wait for port to open:
  pinMode(lightPin,INPUT);
  Serial.begin(9600);
  dht.begin();
  pinMode(lightPin,INPUT); 
  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();
  // Your code here 
 
  analogWrite(greenPin,greenVal);
  analogWrite(redPin,redVal);
  analogWrite(bluePin,blueVal);
  light=analogRead(lightPin);
  Voltage= analogRead(PHPIN);
  float volt=Voltage*5.0/1024;
  pHVal=3.5*volt+Offset;
  temperature=dht.readTemperature();
  humidity=dht.readHumidity(); 

  
  Serial.print("greenVal=");
  Serial.print(greenVal);
  Serial.print(", redVal=");
  Serial.print(redVal);
  Serial.print(", blueVal=");
  Serial.print(blueVal);
  Serial.print(", pH = ");
  Serial.print(pHVal); 
  Serial.print(", Temperature = ");
  Serial.print(temperature); 
  Serial.print(", Humidity = ");
  Serial.print(humidity); 
  Serial.print(", Light = ");
  Serial.println(light);
  
  
  if (light<150) {
    
    message = "LIGHT BELOW LIMIT OF 150";
  }
  else{
    
    if (light>900) {
    message = "LIGHT ABOVE LIMIT OF 900";
  }
  else{
    message = "LIGHT NORMAL";
  }
  }
  
  }
  
  
  
 


/*
  Since GreenVal is READ_WRITE variable, onGreenValChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onGreenValChange()  {
  // Add your code here to act upon GreenVal change
}

/*
  Since RedVal is READ_WRITE variable, onRedValChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onRedValChange()  {
  // Add your code here to act upon RedVal change
}

/*
  Since BlueVal is READ_WRITE variable, onBlueValChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onBlueValChange()  {
  // Add your code here to act upon BlueVal change
}

/*
  Since PHVal is READ_WRITE variable, onPHValChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onPHValChange()  {
  // Add your code here to act upon PHVal change
  if (pHVal>7) {
    message = "pH is more than" + String(7);
  }
}



/*
  Since Message is READ_WRITE variable, onMessageChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMessageChange()  {
  // Add your code here to act upon Message change
}


/*
  Since Light is READ_WRITE variable, onLightChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLightChange()  {
  // Add your code here to act upon Light change
  if (light <150) {
    message = "BELOW INTENSITY LIMIT OF" + String(100);
  }
}



I want it to do this below, except for all 4 variables.
image

Hope you can help me out thank you

Which are exactly the "other variables" you're talking about, and what do you mean with "do not respond"? If you need to write messages based on the value of some variable compared with low and high limits, the schema is the same. The one you wrote can be made in a more compact and readable version like:

  // Lower limit
  if (light<150) {
    message = "LIGHT BELOW LIMIT OF 150";
  }
  // Higher limit
  else if (light>900) {
    message = "LIGHT ABOVE LIMIT OF 900";
    }
    // Nominal
  else{
      message = "LIGHT NORMAL";
  }

You could even create a function to do that for any other value you need to check, giving the value and limits. Check this example out (derived from a piece of code I wrote a couple of years ago):

char checkFormat[][30] = {
  "%s BELOW LIMIT OF %d", 
  "NORMAL", 
  "%s ABOVE LIMIT OF %d" };

void setup()
{
  Serial.begin(9600);
  Serial.println("START");

  // 200 is good
  checkLimits("LIGHT", 200, 150, 900);
  // 10 is too low
  checkLimits("LIGHT", 10, 150, 900);
  // 1000 is too high
  checkLimits("LIGHT", 1000, 150, 900);
}

void loop(){

}

int checkLimits(const char *Name, int Value, int Vlow, int Vhigh) 
{
  int i = 1;
  int ret = 0;
  int val = 0;
  char checkMsg[30];
  
  if (Value<Vlow) {
    i = 0;
    ret = -1;
    val = Vlow;
  }
  if (Value>Vhigh) { 
    i = 2;
    ret = 1;
    val = Vhigh;
  }
  sprintf(checkMsg, checkFormat[i], Name, val);
  Serial.println(checkMsg);
  return ret;
}

PS: Besides, there's no "execute if statements simultaneously", arduino can process "simultaneously" nothing, it can run code very/fairly quickly enough to look like it did that at the same time.

Thanks for this btw
My main concern is lets say I copy paste this block of code right below each other 3 more times, except each time I replace the variable with temperature, humidity and pH. When I run the code, only one of the variables shows up in the message widget, the others dont show up at all. This leads me to assume, correct me if I am wrong, that the if statement blocks containing the other variables are unable to run simulataneously together.


Like this per se.

So how can I solve this?
Thank you very much

You are reusing the same variable, message. Try instead making a message variable for each message. Such as message_pH, message_lux, etc. Then print them all at the end.

Just a small question, I can only link one messenger to one string variable at a time. How can I make all the messages appear in one messenger widget?
Thanks in advance

As @er_name_not_found said, this is because of the way you handle the messages, the culprit is neither the ifs nor any kinda "simultaneously". If you need separate messages you need separate variables, or chain the message texts on the same variable (as "message" is a "String"), something like this (not tested, you must do it):

  message = "";
  if (light<150) {
    message += "LIGHT BELOW LIMIT OF 150\n";
  }
  // Higher limit
  else if (light>900) {
    message += "LIGHT ABOVE LIMIT OF 900\n";
    }
    // Nominal
  else{
      message += "LIGHT NORMAL\n";
  }
  if (pHVal<5) {
    message += "Too acidic\n";
  }
  // Higher limit
  else if (pHVal>900) {
    message += "Too basic\n";
    }
    // Nominal
  else{
      message += "Solution stable\n";
  }

Anyway, please never send code as a picture, just paste the text you need to show us, and mark it as "code" like you did before. We can't copy the text from a picture.

build the string with all you need inside by concatenating the messages

@docdoc and @J-M-L have the way of it. You were overwriting the message variable. Instead, you need to concatenate it in each if. That lets you keep it to one message variable.

Thank to ALL OF you so much, now my project is finally complete. It was the last final straw I had to put in. Especially @docdoc @J-M-L @er_name_not_found .

Thank you once again.
Happy belated thanksgivings!

1 Like

Awesome! Hope you stick with it and make other cool stuff.

1 Like

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