Panel Cookies
Mist Humidifier - Code MEGA
05/04/2022 | Views: 9042 | Robotics | by: ELECTRONOOBS      


Is time to upload the code so download it from below. The code is very easy. We detect when the button is pressed and we change the mode from 0 to 4. Each of the modes 1, 2, 3 and 4 are for 15 minutes, 30, 45 and 1 hour time of mist generation. To let the user know which mode is selected, we make a beep, 2 beeps, 3 and 4 beeps and a long beep when the generator is off. When the mode is not 0, we use digital write to turn on the fan and the generator MOSFETs. And when the mode is 0, we turn them off. We count time in seconds and when the elapsed time is higher than 900 for example in case of 15 minutes, we jump to mode 0 where we turn everything off. That’s it for the code. Once uploaded give it a test and see if it works so connect 24V and test the buttons and if the fan turns on.


Main Arduino Code (05/03/2022)

Arduino schematic humidifier DIY






//Inputs/Outputs
int fan = 2;
int mist = 3;
int but_1 = 8;
int but_2 = 9;
int but_3 = 10;
int buzzer = 5;

//Variables
bool but_1_state = false;
bool but_2_state = false;
bool but_3_state = false;
int mode = 0;
int beep_delay = 100;
unsigned int millis_now, millis_previous;
int elapsed_seconds = 0;

void setup() {
  pinMode(fan, OUTPUT);
  pinMode(mist, OUTPUT);
  digitalWrite(fan, LOW);
  digitalWrite(mist, LOW);
  
  pinMode(but_1, INPUT);
  pinMode(but_2, INPUT);
  pinMode(but_3, INPUT);

  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer, LOW);

  Serial.begin(9600);
  millis_now = millis();
  millis_previous = millis();
}

void loop() {
  //Button 1 read
  if(digitalRead(but_1) && !but_1_state){
    mode ++;
    millis_previous = millis();
    elapsed_seconds = 0;
    if(mode > 4){                                 //15min 30min 45min 1hour for mode 1, 2, 3 and 4
      mode = 0;
    }
    if(mode == 0){
      tone(buzzer, 4000, 600);
    }

    else if(mode == 1){
      tone(buzzer, 3200, 100);
      
    }

    else if(mode == 2){
      tone(buzzer, 3200, 100);
      delay(beep_delay);
      tone(buzzer, 3200, 100);
    }

    else if(mode == 3){
      tone(buzzer, 3200, 100);
      delay(beep_delay);
      tone(buzzer, 3200, 100);
      delay(beep_delay);
      tone(buzzer, 3200, 100);
    }

    else if(mode == 4){
      tone(buzzer, 3200, 100);
      delay(beep_delay);
      tone(buzzer, 3200, 100);
      delay(beep_delay);
      tone(buzzer, 3200, 100);
      delay(beep_delay);
      tone(buzzer, 3200, 100);
    }       
    but_1_state = true;;
  }

  else if(!digitalRead(but_1) && but_1_state){
    but_1_state = false;
  }
  
  //Button 2 read
  if(digitalRead(but_2) && !but_2_state){
    tone(buzzer, 3200, beep_delay);
    but_2_state = true;
  }

  else if(!digitalRead(but_2) && but_2_state){
    but_2_state = false;
  }

  //Button 3 read
  if(digitalRead(but_3) && !but_3_state){
    mode = 0;
    tone(buzzer, 4000, 600);
    but_3_state = true;    
  }

  else if(!digitalRead(but_3) && but_3_state){
    but_3_state = false;
  }

  
  
  if(mode == 0){
    digitalWrite(fan, LOW);
    digitalWrite(mist, LOW);        
    Serial.println("Fan and mist are OFF");
  }

  if(mode == 1){
    digitalWrite(fan, HIGH);
    digitalWrite(mist, HIGH);
    millis_now = millis();
    if(millis_now - millis_previous > 1000){
      elapsed_seconds ++;
      if(elapsed_seconds > 900){             //15 minutes
        mode = 0;
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
      }
      millis_previous = millis_now;
    }    
    Serial.print("Fan and mist are ON for: ");
    Serial.print(elapsed_seconds); 
    Serial.println(" of 900s"); 
  }

  if(mode == 2){
    digitalWrite(fan, HIGH);
    digitalWrite(mist, HIGH);
    millis_now = millis();
    if(millis_now - millis_previous > 1000){
      elapsed_seconds ++;
      if(elapsed_seconds > 1800){             //30 minutes
        mode = 0;
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
      }
      millis_previous = millis_now;
    }    
    Serial.print("Fan and mist are ON for: ");
    Serial.print(elapsed_seconds); 
    Serial.println(" of 1800s");
  }

  if(mode == 3){
    digitalWrite(fan, HIGH);
    digitalWrite(mist, HIGH);
    millis_now = millis();
    if(millis_now - millis_previous > 1000){
      elapsed_seconds ++;
      if(elapsed_seconds > 2700){             //45 minutes
        mode = 0;
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
      }
      millis_previous = millis_now;
    }    
    Serial.print("Fan and mist are ON for: ");
    Serial.print(elapsed_seconds); 
    Serial.println(" of 2700s");
  }

  if(mode == 4){
    digitalWrite(fan, HIGH);
    digitalWrite(mist, HIGH);
    millis_now = millis();
    if(millis_now - millis_previous > 1000){
      elapsed_seconds ++;
      if(elapsed_seconds > 3600){             //1 hour
        mode = 0;
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
        delay(100);
        tone(buzzer, 3200, 100);
      }
      millis_previous = millis_now;
    }    
    Serial.print("Fan and mist are ON for: ");
    Serial.print(elapsed_seconds); 
    Serial.println(" of 3600s");
  }


  
}













Last tutorials

All about Arduino PWM frequencies
Smallest ESC based on ARDUINO
Debug Arduino and ESP with PlatformIO
Homemade Best Two Hand Multimeter with Arduino
Homemade Baby White Noise Generator

ADVERTISERS



Affiliate Disclosure

ADVERTISERS



PCBWAY PCB service





Curso Arduino Online nivel bajo