/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Alcohol Sensor"
	- Source Code compiled for: Arduino Mega
	- Source Code created on: 2024-06-09 07:47:41

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* read alcohol and print on serial */
/****** END SYSTEM REQUIREMENTS *****/


/****** DEFINITION OF LIBRARIES *****/
#include <MQUnifiedsensor.h>  // https://github.com/miguel5612/MQSensorsLib

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t sensor_MQ303A_DOUT_PIN_D2 = 2;

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t sensor_MQ303A_AOUT_PIN_A0 = A0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
#define Board ("Arduino Mega")
#define Type ("MQ-303A")
#define Voltage_Resolution (5)
#define ADC_Bit_Resolution (10)
#define RatioMQ303ACleanAir (60) // RS / R0 = 60 ppm

MQUnifiedsensor MQ303A(Board, Voltage_Resolution, ADC_Bit_Resolution, sensor_MQ303A_AOUT_PIN_A0, Type);

void setup(void)
{
  // put your setup code here, to run once:
  Serial.begin(9600); // Start serial communication at 9600 baud rate

  pinMode(sensor_MQ303A_DOUT_PIN_D2, INPUT_PULLUP); // Set digital pin as input with pull-up resistor
  pinMode(sensor_MQ303A_AOUT_PIN_A0, INPUT); // Set analog pin as input

  MQ303A.setRegressionMethod(1); // Set regression method to Exponential
  MQ303A.setA(0.3934); // Set 'a' coefficient
  MQ303A.setB(-1.504); // Set 'b' coefficient

  MQ303A.init(); // Initialize the sensor

  Serial.print("Calibrating please wait.");
  float calcR0 = 0;
  for (int i = 1; i <= 10; i++) {
    MQ303A.update(); // Update sensor readings
    calcR0 += MQ303A.calibrate(RatioMQ303ACleanAir); // Calibrate sensor
    Serial.print(".");
  }
  MQ303A.setR0(calcR0 / 10); // Set the calibrated R0 value
  Serial.println("  done!.");

  if (isinf(calcR0)) {
    Serial.println("Warning: Connection issue, R0 is infinite (Open circuit detected) please check your wiring and supply");
    while (1); // Halt the program
  }
  if (calcR0 == 0) {
    Serial.println("Warning: Connection issue found, R0 is zero (Analog pin shorts to ground) please check your wiring and supply");
    while (1); // Halt the program
  }
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  MQ303A.update(); // Update sensor readings
  float alcoholPPM = MQ303A.readSensor(); // Read alcohol concentration in PPM
  Serial.print("Alcohol now (PPM): ");
  Serial.println(alcoholPPM);
  delay(500); // Delay for 500 milliseconds
}

/* END CODE */