toggling an output through on serial command.

Hello all! I have a ‘simple’ question. My programming skills are not very good so bare with me. I have written a program that will turn on 3 different output via serial command from terminal. It all works fine with if statements. However I want to use the same command to turn the input on and off. Right now one command is needed to turn on, and another to turn off.

#include <dht.h>
#include <LiquidCrystal.h>
int val;
int pump=51, fan=49, light=47;

const int rs = 45, en = 52, d4 = 50, d5 = 48, d6 = 46, d7 = 44;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
dht DHT;

#define DHT_PIN 53

void setup()
{
Serial.begin(9600);
pinMode(pump,OUTPUT);
pinMode(fan,OUTPUT);
pinMode(light, OUTPUT);

lcd.begin(16,2); //16 by 2 character display
} void loop()
{ val=Serial.read();
if(val==‘p’)
{
Serial.println(“PUMP ON”);
digitalWrite(pump,HIGH);
delay(6000);
digitalWrite(pump,LOW);
delay(1000);
Serial.println(“PUMP Finished”);
}
if(val==‘f’)
{
Serial.println(“FAN ON”);
digitalWrite(fan,HIGH);
delay(3000);
digitalWrite(fan,LOW);
delay(1000);

}
if(val==‘l’)
{

Serial.println(“LIGHT ON”);
digitalWrite(light,HIGH);
delay(100);

}
}

if(val==‘z’)
{
Serial.print("Humidity = ");
Serial.println(DHT.humidity);
Serial.print("Temp = ");
Serial.println(DHT.temperature);
Serial.print("Soil Moisture = ");
Serial.println(analogRead(0));
}

could somebody help me out with a possible nested if loop that toggles based on current output state? Everything I’ve tried has just made the program get stuck in one loop. Thanks in advance!

I suggest using struct to keep track of your pins and their status:

//declare struct
struct worker {
   int pin;
   bool enabled;
};

//declare struct objects and initialize

worker my_workers[3] = {{51, false}, {49, false}, {47, false}};

void setup()
{

   ...
   //set pins to output
   for (int i=0; i<3; i++) {
      pinMode(my_workers[i].pin, OUTPUT);
   }
   ...
 
}

void loop()
{
   //get command
   //says 'f' will turn fan on and off
   if (val == 'f') {
      if (my_workers[1].enabled == false) {
         //the fan is not on
         //so turn it on here
         ...
         //set status to on
         my_workers[1].enabled = true;
      } else {
         //the fan is on
         //turn it off here
         ..
         //set status to off
         my_workers[1].enabled = false;
      }
   }



}

arduino_new:
I suggest using struct to keep track of your pins and their status:

//declare struct

struct worker {
  int pin;
  bool enabled;
};

//declare struct objects and initialize

worker my_workers[3] = {{51, false}, {49, false}, {47, false}};

void setup()
{


  //set pins to output
  for (int i=0; i<3; i++) {
      pinMode(my_workers[i].pin, OUTPUT);
  }
  …

}

void loop()
{
  //get command
  //says ‘f’ will turn fan on and off
  if (val == ‘f’) {
      if (my_workers[1].enabled == false) {
        //the fan is not on
        //so turn it on here
        …
        //set status to on
        my_workers[1].enabled = true;
      } else {
        //the fan is on
        //turn it off here
        …
        //set status to off
        my_workers[1].enabled = false;
      }
  }

}

This is very good advice, thanks for the help arduino_new! I will make a structure as you advised. I only need the light and fan to toggle. The water pump will only be run for a second or two and then turned off; can I leave the water pump out of the structure in that case?

I got it working by using your suggestion, thank you. :smiley:

Hi mike_drz

Any chance you can pop the completed sketch. I way newer at this then I should be. This function is mainly what I'm looking for but seem to be having trouble putting it all together!

Thanks