Scenario problem

I have a smart bathroom project and I'm trying to make some functions and call them i want to use that function called door and when the pir value is 0 then clean function start but when I'm trying to do that the 2 functions always works together at the same time immediately

#define shfat 29
#define led 32
#define ir 23
#define pump 24
#define pir 25
#define fan1 30
#define fan2 31
#define pump_water1 36
#define pump_water2 37
#define button_opendoor 39
#define button_closedoor 40
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
const int trigpin=27;
const int trigecho=28;
const int fan_relay=26;
long dur;
int dis;
int diss;
int x;
int redLed = 34;
int greenLed = 35;
int smokeA0 = A0;
int sensorThres = 500;
#define flame 22
#define buzzer 33
int data_flame;
int data_door;
int pirdata;
int data;
int flag=0;
int pos;
int data_opendoor;
int data_closedoor;
int flag_for_clean;
int flag_stop_clean;
long time1,time2,time3,time4,time5,time6;
////////////////////////////////////////////////////////////////////////////////////
void setup() {
  // put your setup code here, to run once:
  lcd.init();                      // initialize the lcd 
  lcd.init();
  // Print a message to the LCD.
  pinMode(fan1,OUTPUT);
pinMode(fan2,OUTPUT);
pinMode(pump_water1,OUTPUT);
pinMode(pump_water2,OUTPUT);
pinMode(flame,INPUT);
  pinMode(buzzer,OUTPUT);
pinMode(shfat,OUTPUT);
pinMode(led,OUTPUT);
pinMode(ir,INPUT);
pinMode(pump,OUTPUT);
pinMode(trigpin,OUTPUT);
pinMode(trigecho,INPUT);
pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(fan_relay, OUTPUT);
  pinMode(smokeA0, INPUT);
  pinMode(button_opendoor,INPUT);
  pinMode(button_closedoor,INPUT);
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on GIO2 to the servo object    
time1=millis();
}
////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // put your main code here, to run repeatedly:
  
data=digitalRead(ir);
pirdata=digitalRead(pir);
data_flame=digitalRead(flame);
data_opendoor=digitalRead(button_opendoor);
data_closedoor=digitalRead(button_closedoor);  
digitalWrite(shfat,0);
x=digitalRead(led);
digitalWrite(led,0);
if(millis()-time1>=10000)
{ time1=millis();
welcomescreen();
  }
  else if(millis()-time2>=1000&&pirdata==1)
{ time2=millis();
door();
  }
  else if(millis()-time3>=100)
{ time3=millis();
opendoor();
closedoor();
  }
  else if(millis()-time4>=1000)
{ time4=millis();
tap_drayer();
}
else if(millis()-time5>=100)
{ time5=millis();
smoke();
Flame();
}
digitalWrite(led,1);
if(millis()-time6>=100000&&pirdata==0)
{ time6=millis();

cleaning();
}

}
void cleaning()
{
  if(flag_for_clean!=0&&pirdata==0&&flag!=0&&x!=0)
  {
  lcd.clear();
  digitalWrite(led,1);
 lcd.setCursor(3,0);
  lcd.print("Cleaning");
  lcd.setCursor(2,1);
  lcd.print("Please Wait"); 
 digitalWrite(led,1);
digitalWrite(pump_water1,0);
digitalWrite(pump_water2,0);
delay(500);
digitalWrite(pump_water1,1);
digitalWrite(pump_water2,1);
digitalWrite(fan1,0);
digitalWrite(fan2,0);
delay(1000);
digitalWrite(fan1,1);
digitalWrite(fan2,1);
delay(200);
flag=0;
flag_stop_clean=1;
flag_for_clean=0;
}
}
void door()
{
  /*
  for (pos = 100; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  */
    if(pirdata==01)
    {      
  digitalWrite(led,0);
  delay(2000);
  digitalWrite(led,1);
  flag=1;
  flag_for_clean=1;
    }
}
 

Please no pictures of code.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

Hi,
in your code there are 2 times lcd.init(); in setup().

And calls a function that doesn't exist.
welcomescreen();
opendoor();
closedoor();
......
......

and that's why it has compilation error.

Hi,
Have you looked at switch.. case function?

That above link uses a sensor input to select an output result.
Google;

arduino switch case

Tom... :smiley: :+1: :coffee: :australia:

No i don't have compilation error everything is okay except this 2function

I will try it

Hi,
What model Arduino are you using?
Is that first post your complete code?

Tom... :smiley: :+1: :coffee: :australia:

Hi
I'm using Arduino mega 2560
Yes it's the complete code

Hello
The sketch will not compile.

Where are these functions, then:

smoke();
Flame();

I have no compilation error while uploading code everything work as i need except these 2 function

They in another tab in my project i forget to put it here but there's no problem in these

check the setting in preferences:
grafik

void smoke()
{
  int analogSensor = analogRead(smokeA0); 
 Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  // Checks if it has reached the threshold value
   digitalWrite(greenLed, HIGH);
   delay(150);
  if (analogSensor > sensorThres) 
 {  
  digitalWrite(redLed, HIGH); 
   digitalWrite(greenLed, LOW);
digitalWrite(buzzer, HIGH);
  }
  else  {
   digitalWrite(redLed, LOW); 
   digitalWrite(greenLed, HIGH);
    digitalWrite(buzzer, LOW);
 }
  
  }
void Flame()
{
  if(data_flame==0)
    digitalWrite(buzzer,1);
  else
    digitalWrite(buzzer,0);

  }

I tried it but the same problem happen
This is my full code

#define shfat 29
#define led 32
#define ir 23
#define pump 24
#define pir 25
#define fan1 30
#define fan2 31
#define pump_water1 36
#define pump_water2 37
#define button_opendoor 39
#define button_closedoor 40
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
const int trigpin = 27;
const int trigecho = 28;
const int fan_relay = 26;
long dur;
int dis;
int diss;
int x;
int redLed = 34;
int greenLed = 35;
int smokeA0 = A0;
int sensorThres = 500;
#define flame 22
#define buzzer 33
int data_flame;
int data_door;
int pirdata;
int data;
int flag = 0;
int pos;
int data_opendoor;
int data_closedoor;
int flag_for_clean;
int flag_stop_clean;
long time1, time2, time3, time4, time5, time6;
////////////////////////////////////////////////////////////////////////////////////
void setup() {
  // put your setup code here, to run once:
  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  pinMode(fan1, OUTPUT);
  pinMode(fan2, OUTPUT);
  pinMode(pump_water1, OUTPUT);
  pinMode(pump_water2, OUTPUT);
  pinMode(flame, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(shfat, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(ir, INPUT);
  pinMode(pump, OUTPUT);
  pinMode(trigpin, OUTPUT);
  pinMode(trigecho, INPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(fan_relay, OUTPUT);
  pinMode(smokeA0, INPUT);
  pinMode(button_opendoor, INPUT);
  pinMode(button_closedoor, INPUT);
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on GIO2 to the servo object
  time1 = millis();
}
////////////////////////////////////////////////////////////////////////////////////
void loop() {
  // put your main code here, to run repeatedly:

  data = digitalRead(ir);
  pirdata = digitalRead(pir);
  data_flame = digitalRead(flame);
  data_opendoor = digitalRead(button_opendoor);
  data_closedoor = digitalRead(button_closedoor);
  digitalWrite(shfat, 0);
  x = digitalRead(led);
  digitalWrite(led, 0);
  if (millis() - time1 >= 10000)
  { time1 = millis();
    welcomescreen();
  }
  switch (pirdata) {
    case 1:

      if (millis() - time2 >= 1000)
      { time2 = millis();
        door();
      }

      break;
    case 0:
      if (millis() - time6 >= 1000 && pirdata == 0)
      { time6 = millis();


        cleaning();
        break;
      }
  }
  if (millis() - time3 >= 100)
  { time3 = millis();
    opendoor();
    closedoor();
  }
  else if (millis() - time4 >= 1000)
  { time4 = millis();
    tap_drayer();
  }
  else if (millis() - time5 >= 100)
  { time5 = millis();
    smoke();
    Flame();
  }




}
void opendoor()
{
  if(data_opendoor==1)
  {
  for (pos = 100; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  lcd.clear();
lcd.setCursor(3,0);
  lcd.print("Openning");
  }
  }
void closedoor()
{
  if(data_closedoor==1)
  {
  for ( pos = 0; pos <= 100; pos += 1) { // goes from 0 degrees to 90 degrees
    // in steps of 1 degree
    myservo.write(pos);
    delay(15);
          
 }
  lcd.clear();
lcd.setCursor(3,0);
  lcd.print("Closed");
  
  }
}
void cleaning()
{
  if(pirdata==0)
  {
  lcd.clear();
  //digitalWrite(led,1);
 lcd.setCursor(3,0);
  lcd.print("Cleaning");
  lcd.setCursor(2,1);
  lcd.print("Please Wait"); 
 //digitalWrite(led,1);
digitalWrite(pump_water1,0);
digitalWrite(pump_water2,0);
delay(500);
digitalWrite(pump_water1,1);
digitalWrite(pump_water2,1);
digitalWrite(fan1,0);
digitalWrite(fan2,0);
delay(1000);
digitalWrite(fan1,1);
digitalWrite(fan2,1);
delay(200);
flag=0;
flag_stop_clean=1;
flag_for_clean=0;
}
}
 void Flame()
{
  if(data_flame==0)
    digitalWrite(buzzer,1);
  else
    digitalWrite(buzzer,0);

  }
void welcomescreen()
{
  if(flag_stop_clean==1)
  {
  lcd.clear();
lcd.setCursor(3,0);
  lcd.print("Welcome");
  lcd.setCursor(2,1);
  lcd.print("Have A nice Day");
}
flag_stop_clean=0;
}
void door()
{
  /*
  for (pos = 100; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  */
    if(pirdata==1)
    {      
  digitalWrite(led,0);
  /*delay(2000);
  digitalWrite(led,1);*/
  flag=1;
  flag_for_clean=1;
    }
    else
    digitalWrite(led,0);
}
void smoke()
{
  int analogSensor = analogRead(smokeA0); 
 Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  // Checks if it has reached the threshold value
   digitalWrite(greenLed, HIGH);
   delay(150);
  if (analogSensor > sensorThres) 
 {  
  digitalWrite(redLed, HIGH); 
   digitalWrite(greenLed, LOW);
digitalWrite(buzzer, HIGH);
  }
  else  {
   digitalWrite(redLed, LOW); 
   digitalWrite(greenLed, HIGH);
    digitalWrite(buzzer, LOW);
 }
  
  }
void tap_drayer(){
//Pump Water from tap
if(data==0)
{
  digitalWrite(pump,LOW);
  }
  else if(data==1)
  {
   digitalWrite(pump,HIGH); 
    }
 //pump air from drayer   
    digitalWrite(trigpin,LOW);
delay(15);
digitalWrite(trigpin,HIGH);
delay(15);
digitalWrite(trigpin,LOW);
dur=pulseIn(trigecho,HIGH);
dis=dur*0.034/2;
diss=dis;
if(diss<=10)
  digitalWrite(fan_relay,0);
  else
    digitalWrite(fan_relay,1); 
}

I think part of the problem is the way you use millis() timers. For example:

  if (millis() - time1 >= 10000)
  {
    time1 = millis();
    welcomescreen();
  }

This means to call 'welcomecreen()' every ten seconds. That doesn't sound right to me.

  if (millis() - time3 >= 100)
  {
    time3 = millis();
    opendoor();
    closedoor();
  }

And this will call opendoor() and closedoor() ten times a second.

I think what you need is a finite state machine to control the sequence of operations.

Hi,
Can you just tell us in point form in what sequence you want your program to do?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

You could help yourself (and us) by using proper variable names.
Boolean variables (true/false, 1/0, high/low) are best named like this:
Bool doorIsOpen= false

Then in your code you can do:
If (doorIsOpen) {
DoSomething;
}

Or in case of a fire
If (isOnFire)

And in case it is a flag you may name the flag like this
Bool doorOpenRequest = false;

If (doorOpenRequest) {
OpenDoor();
}

This would make your code far more readable.
Also variable pir, pirdata, dis and diss do not make any sense to me.
And x is way too short for a global variable.
And data_ does not really add much...

Yes because i want these function to be ready to start any time because these function can't be executed until i push the button but cleaning and door works in a sequence after the pir data be 0 it the cleaning function start but when i try to compile the door function and cleaning function works together

Welcome screen first then servo then if you use drayer or tap any time they should work also smoke and flame should be ready to execute any time and you can use the buttons or close the door any time i want after the person leave the cleaning process start alone the problem that they always work if the pir 0 or 1