CCCPaste

"CO Sensor" rev_01

/********* 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: "CO Sensor"
	- Source Code NOT compiled for: Arduino Pro Mini 5V
	- Source Code created on: 2024-06-09 12:54:51

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* if button is pressed, so read CO data from sensor */
	/* and print on serial. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include   //https://github.com/evert-arias/EasyButton
#include   //https://github.com/miguel5612/MQSensorsLib

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void onPressed();  // Prototype for the button press callback function

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t button_PushButton_PIN_D2 = 2;
const uint8_t CO_Sensor_MQ7_DOUT_PIN_D3 = 3;

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

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton button(button_PushButton_PIN_D2);  // Instantiate EasyButton object

// Hardware Related Macros
#define Board "Arduino Pro Mini"
#define Pin A0  // Analog input pin for the sensor
#define PreheatPin5 3  // Preheat pin for 5V
#define PreheatPin14 4  // Preheat pin for 1.4V

// Software Related Macros
#define Type "MQ-7"
#define Voltage_Resolution 5
#define ADC_Bit_Resolution 10
#define RatioMQ7CleanAir 27.5

// Declare Sensor
MQUnifiedsensor MQ7(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);

void onPressed() {
  Serial.println("Button pressed");

  // Read CO data from the sensor
  MQ7.update();
  float CO_concentration = MQ7.readSensor();

  // Print CO concentration on serial
  Serial.print("CO Concentration: ");
  Serial.print(CO_concentration);
  Serial.println(" ppm");
}

void setup(void) {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println();
  Serial.println(">>> EasyButton pressed example <<<");

  pinMode(button_PushButton_PIN_D2, INPUT_PULLUP);
  pinMode(CO_Sensor_MQ7_DOUT_PIN_D3, INPUT_PULLUP);
  pinMode(CO_Sensor_MQ7_AOUT_PIN_A0, INPUT);

  button.begin();  // Initialize the button
  button.onPressed(onPressed);  // Attach the callback function to the button press event

  pinMode(PreheatPin5, OUTPUT);
  pinMode(PreheatPin14, OUTPUT);

  // Set regression method and constants for CO concentration
  MQ7.setRegressionMethod(1);
  MQ7.setA(99.042);
  MQ7.setB(-1.518);

  // Initialize the sensor
  MQ7.init();

  // Preheat cycle
  Serial.println("Preheating, please wait 2.5 minutes");
  digitalWrite(PreheatPin5, HIGH);
  digitalWrite(PreheatPin14, LOW);
  delay(60000);
  digitalWrite(PreheatPin5, LOW);
  digitalWrite(PreheatPin14, HIGH);
  delay(90000);
  digitalWrite(PreheatPin5, HIGH);
  digitalWrite(PreheatPin14, LOW);
  delay(60000);
  digitalWrite(PreheatPin5, LOW);
  digitalWrite(PreheatPin14, HIGH);
  delay(90000);
  digitalWrite(PreheatPin5, HIGH);
  digitalWrite(PreheatPin14, LOW);

  // Calibration
  Serial.print("Calibrating, please wait.");
  float calcR0 = 0;
  for (int i = 1; i <= 10; i++) {
    MQ7.update();
    calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
    Serial.print(".");
  }
  MQ7.setR0(calcR0 / 10);
  Serial.println(" done!");

  if (isinf(calcR0)) {
    Serial.println("Warning: Connection issue, R0 is infinite. Please check wiring and supply.");
    while (1);
  }
  if (calcR0 == 0) {
    Serial.println("Warning: Connection issue, R0 is zero. Please check wiring and supply.");
    while (1);
  }

  MQ7.serialDebug(true);
}

void loop(void) {
  // put your main code here, to run repeatedly:
  button.read();  // Continuously read the status of the button

  digitalWrite(PreheatPin5, LOW);
  digitalWrite(PreheatPin14, HIGH);
  delay(90000);
  digitalWrite(PreheatPin5, HIGH);
  digitalWrite(PreheatPin14, LOW);

  MQ7.update();
  MQ7.readSensor();
  MQ7.serialDebug();
}

/* END CODE */