0

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.

Rajbir
  • 1
  • 6
  • Header files should typically contain symbolic constants (or preprocessor macros), structure definitions, and function declarations (not definitions, implementations) that are all needed by multiple source files. If you don't have multiple source files, or there's nothing that needs to be shared between them, then you don't really need header files (and adding a header file anyway could make the code harder to read, understand and maintain). – Some programmer dude Jan 20 '20 at 11:48
  • As for the code you currently show, it's plenty clean already. It could use some consistent indentation but is otherwise okay. – Some programmer dude Jan 20 '20 at 11:49
  • In which environment do you think that converted code? Still in arduino? – Yunus Temurlenk Jan 20 '20 at 11:50
  • Yes it is a clean code for sure but I want to learn how to convert this in CPP and h files – Rajbir Jan 20 '20 at 12:15
  • Here is the link(https://github.com/ryker1990/CE_ARDUINO_LIB/tree/master/ADC121C_MQ4) of the same code for Arduino – Rajbir Jan 20 '20 at 12:16
  • The only thing that could be "moved" to a header file is the definition of the macro `Addr`. Even if you have `extern` declarations of the two global variables you still need their definition in the source file like you have now. – Some programmer dude Jan 20 '20 at 12:20
  • Could you please show me how to do it? – Rajbir Jan 20 '20 at 12:31
  • Create a new file, name it something ending with `.h`. Cut the macro definition from the current source file, and paste it into the newly created file. Include the newly created file into your current source file. – Some programmer dude Jan 20 '20 at 12:57
  • Are you interested in creating a .h and .cpp so that other people can use those files as an Arduino library? If so, I - like Some programmer dude - recommend against doing that. There are two reasons I see for not making this code a library: 1) it's really just a few lines of code, and 2) it contains a lot of Sketch-specific code relative to Sketch-independent code. If you still want to convert your code into a library, see https://www.arduino.cc/en/Hacking/libraryTutorial for some hints about how to do that. – Bradford Needham Jan 20 '20 at 17:01
  • If instead you're interested in creating a .h and .cpp so that this code can be part of a larger project, I'd suggest creating a class named something like MethaneSensor, with member functions something like void begin() and readAndPublish(). But again, the code is so short that you might do better just to write a readAndPublish() function. Also, unless you need the variables raw_adc and ppm outside the current loop, I'd declare them inside this new readAndPublish() function. – Bradford Needham Jan 20 '20 at 17:09
  • @BradfordNeedham could you please show me some example code.It will be very helpful to me – Rajbir Jan 21 '20 at 10:21
  • Stackoverflow isn't a good venue for a tutorial. The most I can do is point you to the tutorial I mentioned above: https://www.arduino.cc/en/Hacking/libraryTutorial That tutorial shows the step-by-step process of changing a simple set of functions into a .h and a .cpp file. – Bradford Needham Jan 22 '20 at 07:26
  • Ok Thanx fir the help – Rajbir Jan 23 '20 at 10:41

0 Answers0