Advertisement
pleasedontcode

Alcohol Detector

Dec 26th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 3.80 KB | Source Code | 0 0
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: AlcoholDetector
  13.     - Source Code compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2023-12-26 09:22:47
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* If sensor detects an alcohol percentage over a */
  21.     /* threshold2, so activate the buzzer and led, switch */
  22.     /* off relay and print on lcd ""Eccessive alcohol". */
  23. /****** SYSTEM REQUIREMENT 2 *****/
  24.     /* If sensor detects an alcohol concentrati on below */
  25.     /* a threshold1 (that Is lower of threshold2), so */
  26.     /* switch off the led and buzzer, maintain on the */
  27.     /* relay and do not display anything on lcd. */
  28. /****** END SYSTEM REQUIREMENTS *****/
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Arduino.h>
  32. #include <Wire.h>
  33. #include <MQUnifiedsensor.h>
  34. #include <LiquidCrystal_I2C.h>
  35.  
  36. /****** SYSTEM PARAMETERS *****/
  37. const int threshold1 = 100; // Threshold 1 for low alcohol concentration
  38. const int threshold2 = 200; // Threshold 2 for high alcohol concentration
  39.  
  40. /****** FUNCTION PROTOTYPES *****/
  41. void setup(void);
  42. void loop(void);
  43. void handleAlcoholConcentration(int alcoholConcentration);
  44.  
  45. /***** DEFINITION OF ANALOG INPUT PINS *****/
  46. const uint8_t Mq3_alcohol_value_PIN_A0 = A0;
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t Buzzer_ActiveBuzzer_output_PIN_D2 = 2;
  50. const uint8_t Led_LED_PIN_D3 = 3;
  51. const uint8_t Relay_powerSupplyDCMotor_PIN_D4 = 4;
  52.  
  53. /***** DEFINITION OF I2C PINS *****/
  54. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  55. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  56. const uint8_t Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. MQUnifiedsensor MQ3("Arduino Pro Mini 5V", 5, 10, Mq3_alcohol_value_PIN_A0, "MQ-3");
  60. LiquidCrystal_I2C lcd(Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Assuming you are using 16x2 LCD
  61.  
  62. void setup(void)
  63. {
  64.   // put your setup code here, to run once:
  65.  
  66.   pinMode(Mq3_alcohol_value_PIN_A0, INPUT);
  67.  
  68.   pinMode(Buzzer_ActiveBuzzer_output_PIN_D2, OUTPUT);
  69.   pinMode(Led_LED_PIN_D3, OUTPUT);
  70.   pinMode(Relay_powerSupplyDCMotor_PIN_D4, OUTPUT);
  71.  
  72.   lcd.begin(16, 2); // Initialize LCD with 16 columns and 2 rows
  73.   lcd.backlight();
  74. }
  75.  
  76. void loop(void)
  77. {
  78.   // Read alcohol concentration from the MQ-3 sensor
  79.   MQ3.update();
  80.   int alcoholConcentration = MQ3.readSensor();
  81.  
  82.   // Handle the alcohol concentration based on the system requirements
  83.   handleAlcoholConcentration(alcoholConcentration);
  84.  
  85.   // Display the alcohol concentration on the LCD
  86.   lcd.setCursor(0, 0);
  87.   lcd.print("Alcohol: ");
  88.   lcd.print(alcoholConcentration);
  89.   lcd.print(" ppm");
  90.  
  91.   delay(500);
  92. }
  93.  
  94. void handleAlcoholConcentration(int alcoholConcentration)
  95. {
  96.   // If alcohol concentration is above threshold2
  97.   if (alcoholConcentration > threshold2)
  98.   {
  99.     digitalWrite(Buzzer_ActiveBuzzer_output_PIN_D2, HIGH);
  100.     digitalWrite(Led_LED_PIN_D3, HIGH);
  101.     digitalWrite(Relay_powerSupplyDCMotor_PIN_D4, LOW);
  102.     lcd.setCursor(0, 1);
  103.     lcd.print("Excessive alcohol");
  104.   }
  105.   // If alcohol concentration is below threshold1
  106.   else if (alcoholConcentration < threshold1)
  107.   {
  108.     digitalWrite(Buzzer_ActiveBuzzer_output_PIN_D2, LOW);
  109.     digitalWrite(Led_LED_PIN_D3, LOW);
  110.     digitalWrite(Relay_powerSupplyDCMotor_PIN_D4, HIGH);
  111.     lcd.setCursor(0, 1);
  112.     lcd.print("                  ");
  113.   }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement