I am new here. I was working with particle Photon and MQ4 I2C gas sensor. I have a normal I2C code for this sensor
#include <application.h>
#include <spark_wiring_i2c.h>
// ADC121C_MQ4 I2C address is 0x50(80)
#define Addr 0x50
int raw_adc = 0;
double ppm = 0.0;
void setup()
{
// Set variable
Particle.variable("i2cdevice", "ADC121C_MQ4");
Particle.variable("PPM", ppm);
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise serial communication, set baud rate = 9600
Serial.begin(9600);
delay(300);
}
void loop()
{
unsigned int data[2];
// Start I2C transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x00);
// Stop I2C transmission
Wire.endTransmission();
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// raw_adc msb, raw_adc lsb
if (Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
delay(300);
// Convert the data to 12-bits
raw_adc = ((data[0] & 0x0F) * 256) + data[1];
ppm = (10000 / 4096.0) * raw_adc + 200.0;
// Output data to dashboard
Particle.publish("Methane concentration : ", String(ppm));
delay(1000);
}
So I am a noob in coding and don't know how to separate this code into .cpp and .h files to get the clean code.
Please help me out, how this code can be converted into .cpp and .h files.
Thanx in advance.