Automatically water, soap, hand sanitizer and hand dryer problems

hello i am newbie programmer arduino. i make project automatically water, soap, hand sanitizer and hand. i use statemachice like this, but i cannot make conditions I want. Can anyone help me to make condition like this? i dont understand make loop statemachine in void loop() thank you :grin:

conditions :

  • condition 1 = when visitors come, PIR is active and turn on 0001.mp3
  • condition 2 = detection of visitor body temperature display on LCD and turn on 0002.mp3 / 0003.mp3
  • condition 3 = activate the relay (can be turned on / off continuously until condition 4)
  • condition 4 = if the visitor is not detected it will return to condition 1

this code :

void loop() {
  // put your main code here, to run repeatedly:
  statemachine();
}

void statemachine(){
    
          int statusIR1 = digitalRead(IR1);
          int statusIR2 = digitalRead(IR2);
          int statusIR3 = digitalRead(IR3);
          int statusIR4 = digitalRead(IR4);
          int statusIR5 = digitalRead(IR5);
          val_PIR = digitalRead(PIR);
          control_SM_prev = control_SM;
          
   switch (control_SM){
    
    case 0: //RESET
           control_SM = 1;
           

    case 1: // START PIR
           
           if (val_PIR == HIGH) {
           myDFPlayer.playMp3Folder(1); 
           Serial.println("Motion Detected");
           delay(5000);
           control_SM = 2; 
           }     
           else {
           Serial.println("No Motion");
           myDFPlayer.playMp3Folder(3);
           delay(1000);
           control_SM = 1;
           }
           

    case 2: // STAR MLX
           delay(1000);
           lcd.clear();
           lcd.setCursor(3,0);
           lcd.print("Non-contact");
 
           lcd.setCursor(2,1);
           lcd.print("Suhu: ");
           lcd.print(mlx.readObjectTempC());
           lcd.print("C     ");
           delay(2000);

           if(mlx.readObjectTempC() <= 37 and (mlx.readObjectTempC() >= 27)){
           digitalWrite(laser, HIGH);
           lcd.clear();
           lcd.setCursor(1,0);
           delay(500);
           
           lcd.print("Suhu Tubuh Anda"); 
           lcd.setCursor(5,1);
           lcd.print("Normal");
           delay(2000);
           control_SM = 2;}

           else if (mlx.readObjectTempC() >= 37){
           digitalWrite(laser, HIGH);
           lcd.clear();
           lcd.setCursor(1,0);
           delay(500);
                
           lcd.print("Suhu Tubuh Anda"); 
           lcd.setCursor(2,1);
           lcd.print("Tidak Normal");
           delay(2000);
           control_SM = 2;}
    
    else if (mlx.readObjectTempC() <= 25){
    digitalWrite(laser, LOW);
    lcd.clear();
    lcd.setCursor(2,0);
    delay(500);
          
    lcd.print("auto WITEZER"); 
    lcd.setCursor(5,1);
    lcd.print("ONLINE");
    delay(2000);
    control_SM = 3;}
    
    
    case 3: // STAR RELAY
    
  // variabel statusIR yang menyimpan nilai dari hasil pembacaan pinIR


  // Program sensor IR
          aktifkan_IR:
          if(statusIR1 == HIGH)
          {
            digitalWrite(PUMP1, LOW);
            Serial.println("Pompa Air Dimatikan");
          } else {
            digitalWrite(PUMP1, HIGH);
            Serial.println("Pompa Air Diaktifkan");
            
          }
          
          if(statusIR2 == HIGH) {
            digitalWrite(PUMP2, LOW); 
            Serial.println("Pompa Sabun Dimatikan");
          } else{
            digitalWrite(PUMP2, HIGH);
            Serial.println("Pompa Sabun Diaktifkan");
            
          }
          
          if(statusIR3 == HIGH) {
            digitalWrite(PUMP3, LOW);
            Serial.println("Pompa Hand Sanitizer Dimatikan");
           } else {
            digitalWrite(PUMP3, HIGH);
            Serial.println("Pompa Hand Sanitizer Diaktifkan");
          }
          
          if(statusIR4 == HIGH) {
            digitalWrite(HDRYER4, LOW);
            Serial.println("Hand Dryer Dimatikan");
          } else {
            digitalWrite(HDRYER4, HIGH);
            Serial.println("Hand Dryer Diaktifkan");
            
          }
           if(statusIR5 == LOW) {
            control_SM = 3;
            Serial.println("Pengunjung Terdeteksi");
            } else {
            control_SM = 4;
            Serial.println("Pengunjung Tidak Terdeteksi");}
            
            
   case 4: // TRIGGED
          control_SM = 0;
   } 
  }

The easier you make it to read and copy the code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

yes thank you this is very helpful in writing on the post :grin:

IR_on;

That's not how you call a function

aktifkan_IR:

I do hope you're not thinking of using a goto

Hello
Insert comments and unique names for variables into your sketch.

my bad, but how to call a function?
i want to make it this condition :
conditions :

  • condition 1 = when visitors come, PIR is active and turn on 0001.mp3
  • condition 2 = detection of visitor body temperature display on LCD and turn on 0002.mp3 / 0003.mp3
  • condition 3 = activate the relay (can be turned on / off continuously until condition 4)
  • condition 4 = if the visitor is not detected it will return to condition 1

hello i am newbie programmer arduino. i make project automatically water, soap, hand sanitizer and hand. I have made each of the functions but the problem is I am confused about combining these functions into one program that suits the conditions I want. Can anyone help me combine this code with state machine? thank you :grin:

conditions :

  • condition 1 = when visitors come, PIR is active and turn on 0001.mp3
  • condition 2 = detection of visitor body temperature display on LCD and turn on 0002.mp3 / 0003.mp3
  • condition 3 = activate the relay (can be turned on / off continuously until condition 4)
  • condition 4 = if the visitor is not detected it will return to condition 1

code :

PIR & DFPlayer

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"

SoftwareSerial mySoftwareSerial(11,10); // RX, TX
DFRobotDFPlayerMini myDFPlayer;

int PIR = 35;

boolean isPlaying = false;

void setup() {
    pinMode(PIR, INPUT);      // PIR = Input
    digitalWrite(PIR, HIGH);

    mySoftwareSerial.begin(9600);
    Serial.begin(9600);

    delay(1000);

    Serial.println();
    Serial.println("DFPlayer Mini Demo");
    Serial.println("Initializing DFPlayer...");

    if (!myDFPlayer.begin(mySoftwareSerial)) {
        Serial.println("Unable to begin:");
        Serial.println("1.Please recheck the connection!");
        Serial.println("2.Please insert the SD card!");
        while (true);
    }
    Serial.println(F("DFPlayer Mini online."));
    myDFPlayer.setTimeOut(500);
    //----Set volume----
    myDFPlayer.volume(30); //Set volume value (0~30).


    //----Set different EQ----
    myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
    myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
    myDFPlayer.playMp3Folder(0); 
    isPlaying = true;
    Serial.println("Playing..");
}

void loop() {
  
    while (pengunjung < 1) {
    if (digitalRead(PIR) == HIGH) {
    Serial.println("Motion Detected");
    myDFPlayer.playMp3Folder(1);          // play 0001.mp3
    delay(5000);
    pengunjung++;
    
    }
    else {
    Serial.println("No Motion");
    delay(1000);
    }                                     // loop in while
  }
}

MLX and LCD

#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here
#define BACKLIGHT_PIN 3 // Declaring LCD Pins
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() {
  
// sensor_MLX activated

 delay(1000);
 lcd.clear();
 lcd.setCursor(3,0);
 lcd.print("Non-contact");
 
 lcd.setCursor(2,1);
 lcd.print("Suhu: ");
 lcd.print(mlx.readObjectTempC());
 lcd.print("C     ");
 delay(2000);

  while (pengunjung < 1) {
   if(mlx.readObjectTempC() <= 37 and (mlx.readObjectTempC() >= 27)){   // make range temeprature 27 to 37 = normal
    digitalWrite(laser, HIGH);  // laser on
    lcd.clear();
    lcd.setCursor(1,0);
    delay(500);
    lcd.print("Suhu Tubuh Anda"); 
    lcd.setCursor(5,1);
    lcd.print("Normal");
    delay(2000);
    pengunjung++;
    IR_on;}

    else if (mlx.readObjectTempC() >= 37){   // make temperature >37 = not normal
    digitalWrite(laser, HIGH);  // laser on
    lcd.clear();
    lcd.setCursor(1,0);
    delay(500);
    lcd.print("Suhu Tubuh Anda"); 
    lcd.setCursor(2,1);
    lcd.print("Tidak Normal");
    pengunjung++;
    delay(2000);}
    
    else if (mlx.readObjectTempC() <= 25){    // make if temperature = room temeperature show name device
    digitalWrite(laser, LOW); // laser off
    lcd.clear();
    lcd.setCursor(2,0);
    delay(500);
    lcd.print("auto WITEZER"); 
    lcd.setCursor(5,1);
    lcd.print("ONLINE");
    delay(2000);}
    }
}

}

IR & Pump

//Membuat variabel untuk LED
  int PUMP1 = 6;
  int PUMP2 = 7;
  int PUMP3 = 8;
  int HDRYER4 = 9;
 
// Membuat variabel untuk IR
  int IR1 = 25;
  int IR2 = 27;
  int IR3 = 29;
  int IR4 = 31;

void setup() {

Serial.begin(9600);

// Setting Sensor IR
  // make variable pin PUMP and HDRYER = output
  pinMode(PUMP1, OUTPUT);
  pinMode(PUMP2, OUTPUT);
  pinMode(PUMP3, OUTPUT);
  pinMode(HDRYER4, OUTPUT);
 
  // make variable pin IR = input
  pinMode(IR1, INPUT);
  pinMode(IR2, INPUT);
  pinMode(IR3, INPUT);
  pinMode(IR4, INPUT);
}

void loop() {
  
// variable statusBtn save value pin IR
  int statusIR1 = digitalRead(IR1);
  int statusIR2 = digitalRead(IR2);
  int statusIR3 = digitalRead(IR3);
  int statusIR4 = digitalRead(IR4);

// Code Sensor IR
  // When the push button is not pressed the value is HIGH
    
    if(statusIR1 == HIGH)
    {
      digitalWrite(PUMP1, LOW);
    } else {
      digitalWrite(PUMP1, HIGH);
    }
    if(statusIR2 == HIGH) {
      digitalWrite(PUMP2, LOW);
    } else{
      digitalWrite(PUMP2, HIGH);
    }
    if(statusIR3 == HIGH) {
      digitalWrite(PUMP3, LOW);
    } else {
      digitalWrite(PUMP3, HIGH);
    }
    if(statusIR4 == HIGH) {
      digitalWrite(HDRYER4, LOW);
    } else {
      digitalWrite(HDRYER4, HIGH);
    }
}

Your study topic for today… is STATE MACHINES.

They’re a logical programming method of sequencing, stepping or modifying the program flow in a planned way.

@rhaharu, do not cross-post. Threads merged.

hello, thank you. I've learned a little about state machine but i not understand to control state machine in the void loop() can you help me?

statemachine();

Like that.

yeah but when I tried it, this loop all case code, i need more "if" for make the condition i want

Once you separate conditions and actions, the structure of your state machine should become clearer

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