CCCPaste

Sensor Calibration 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: Sensor Calibration
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-10-14 19:43:46

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

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Messenger app */
/****** SYSTEM REQUIREMENT 2 *****/
	/* The messenger app will feature a dashboard */
	/* displaying sensor data, allowing users to */
	/* visualize trends over time. It will support data */
	/* logging for historical analysis and reporting. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 	//https://github.com/miguel5612/MQSensorsLib

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Msd_MQ6_DOUT_PIN_D2		= 2;
const uint8_t Msd_MQ6_DOUT_PIN_D3		= 3;

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t Msd_MQ6_AOUT_PIN_A0		= A0;
const uint8_t Msd_MQ6_AOUT_PIN_A1		= A1;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Definitions for the MQ6 sensor
#define placa "Arduino UNO" // Define the board type
#define Voltage_Resolution 5 // Voltage resolution
#define ADC_Bit_Resolution 10 // ADC bit resolution
#define RatioMQ6CleanAir 10 // Ratio for clean air calibration

// Declare Sensor using MQUnifiedsensor constructor
MQUnifiedsensor MQ6(placa, Voltage_Resolution, ADC_Bit_Resolution, Msd_MQ6_AOUT_PIN_A0, "MQ-6");

// Variables for logging data
float sensorValue = 0; // Variable to store sensor value
unsigned long previousMillis = 0; // Store the last time data was logged
const long interval = 1000; // Interval for logging data (1 second)

void setup(void)
{
	// put your setup code here, to run once:
	pinMode(Msd_MQ6_DOUT_PIN_D2, INPUT_PULLUP);
	pinMode(Msd_MQ6_DOUT_PIN_D3, INPUT_PULLUP);
	pinMode(Msd_MQ6_AOUT_PIN_A0, INPUT);
	pinMode(Msd_MQ6_AOUT_PIN_A1, INPUT);

	// Initialize the sensor
	MQ6.init(); // Initialize the MQ6 sensor

	// Calibration
	Serial.begin(9600); // Start serial communication
	Serial.print("Calibrating please wait.");
	float calcR0 = 0;
	for(int i = 0; i < 10; i++) {
		MQ6.update(); // Update sensor data
		calcR0 += MQ6.calibrate(RatioMQ6CleanAir); // Calibrate and accumulate R0
		Serial.print(".");
	}
	MQ6.setR0(calcR0 / 10); // Set the average R0 value
	Serial.println(" done!");

	if (isinf(calcR0) || calcR0 == 0) {
		Serial.println("Warning: Connection issue detected.");
		while (1); // Stop execution if there's an issue
	}
}

void loop(void)
{
	// put your main code here, to run repeatedly:
	MQ6.update(); // Update sensor data
	MQ6.readSensor(); // Read the sensor value
	MQ6.serialDebug(); // Print debug information to serial

	// Log data to Serial every second
	unsigned long currentMillis = millis();
	if (currentMillis - previousMillis >= interval) {
		previousMillis = currentMillis; // Save the last time data was logged
		sensorValue = MQ6.getPPM(); // Get the sensor value in PPM
		Serial.print("Sensor Value (PPM): ");
		Serial.println(sensorValue); // Print the sensor value to Serial
		// Here you could add code to visualize trends over time
		// and support data logging for historical analysis
	}

	delay(500); // Delay for sampling frequency
}

/* END CODE */