Dependent Events in Sketch

Good day, everyone.

I have a sketch I'm working on which includes a water sensor, a fire sensor and a light sensor.

At the moment, when my water sensor detects water, it lights up an LED which stays on. The same for the fire sensor and the light sensor. Regardless of the order in which each sensor detects each thing. This is what I want except for one thing.

How can I make my sketch run such that the each sensor will only run once the previous sensor has had a positive detection?

For example, I would like my light sensor to detect light, but ONLY AFTER my fire sensor has detected fire, but ONLY AFTER my water sensor has detected water. So water first, then fire, then light.

So until the water is detected, the fire and light sensors will not work. And only until water AND fire is detected, will the light sensor work.

int pinFire = 5; //the pin where we connect the fire sensor
int FireLED = 4; //the pin we connect the fire LED

int pinWater = 3; //the pin where we connect the water sensor
int WaterLED = 6; //the pin we connect the water LED


const int analogPin = A0;    // pin that the light sensor is attached to
const int LightLED = 12;       // pin that the light LED is attached to
const int threshold = 900;   // an arbitrary threshold level that's in the range of the analog input





void setup() {
  pinMode(pinWater, INPUT); //set the button pin as INPUT
  pinMode(WaterLED, OUTPUT); //set the LED pin as OUTPUT

  pinMode(pinFire, INPUT); //set the button pin as INPUT
  pinMode(FireLED, OUTPUT); //set the LED pin as OUTPUT
  

  // initialize the LED pin as an output:
  pinMode(LightLED, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}




void loop() {

 waterLoopCode() ;
 flameLoopCode() ;
 laserLoopCode() ;
}


void waterLoopCode() {
int stateButton = digitalRead(pinWater); //read the state of the button
  if(stateButton == 0) { //if is pressed
     digitalWrite(WaterLED, HIGH); //write 1 or HIGH to led pin
  }
  
}


void flameLoopCode() {
int stateButton = digitalRead(pinFire); //read the state of the button
  if(stateButton == 0) { //if is pressed
delay(500);
     digitalWrite(FireLED, HIGH); //write 1 or HIGH to led pin
  }
}


void laserLoopCode() {

// read the value of the potentiometer:
  int analogValue = analogRead(analogPin);

  // if the analog value is high enough, turn on the LED:
  if (analogValue > threshold) {
    digitalWrite(LightLED, HIGH);
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(500);        // delay in between reads for stability
}

Thank for in advance for your help!

You are describing a state machine. There are many examples out there if you google for them. You start off in the waiting for water mode and when that happens transition into the wait for fire mode. Based on which mode you are in, you only check and react to that particular sensor.

Thanks, blh64!

I had no idea what it was called. I'll give it a Google and report back!

You could make variables to store the three alarm states or just read the output pins:

void loop() 
{
 waterLoopCode() ;

  if (digitalRead(WaterLED) == HIGH)
   flameLoopCode() ;

  if (digitalRead(FireLED) == HIGH)
   laserLoopCode() ;
}

johnwasser:
You could make variables to store the three alarm states or just read the output pins:

void loop() 

{
waterLoopCode() ;

if (digitalRead(WaterLED) == HIGH)
  flameLoopCode() ;

if (digitalRead(FireLED) == HIGH)
  laserLoopCode() ;
}

This seem like a simpler solution. Thanks!

johnwasser:
You could make variables to store the three alarm states or just read the output pins:

void loop() 

{
waterLoopCode() ;

if (digitalRead(WaterLED) == HIGH)
  flameLoopCode() ;

if (digitalRead(FireLED) == HIGH)
  laserLoopCode() ;
}

I've gotta thank you. This worked absolutely perfectly. Such a simple solution as well!