CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: How To: Prevent duplicate coding

  1. #1
    Join Date
    May 2020
    Posts
    4

    Lightbulb How To: Prevent duplicate coding

    I am rather new in programming and wonder how to compact the code I wrote for using a flexible number of CJMCU-8110 connected to my NodeMCU. In the attached test code below I have 2 connected, but in the near future I want to expand to 3 or 4 and would like to drive that by a variable (f.i. int SensorNumber = 2) instead of duplicating the code for each sensor like I did in the example.

    Who can guide me to a solution?

    Code:
    #include <Arduino.h>
    #include <Wire.h>    // I2C library
    #include "ccs811.h"  // CCS811 library
    #include "ClosedCube_HDC1080.h"
    
    CCS811 ccs811_1;
    CCS811 ccs811_2;
    
    ClosedCube_HDC1080 HDC1080_1;
    ClosedCube_HDC1080 HDC1080_2;
    
    int SDApin;
    int SCLpin = D1;
    
    HDC1080_SerialNumber sernum;
    char format[12];
    bool vBootphase = true;
    uint16_t eco2, etvoc, errstat, raw;
    
    void setup() {Serial.begin(115200); while(!Serial){;}}
    
    void loop() 
    {
      if (vBootphase == true)
      {
        // Channel 1: Setup
        SDApin = D2;
        Wire.begin(SDApin, SCLpin);
        delay(500);
        // ** CCS811 **
        Serial.println("");
        Serial.println("*** CCS1 ***");
        Serial.print("CCS :");
        ccs811_1.set_i2cdelay(50);
        ccs811_1.begin();
        delay(500);
        ccs811_1.start(CCS811_MODE_1SEC);
        delay(1000);
        Serial.print("setup: hardware    version: "); Serial.println(ccs811_1.hardware_version(),HEX);
        Serial.print("setup: bootloader  version: "); Serial.println(ccs811_1.bootloader_version(),HEX);
        Serial.print("setup: application version: "); Serial.println(ccs811_1.application_version(),HEX);
        // HDC1080
        HDC1080_1.begin(0x40);
        Serial.print("Manufacturer ID=0x");
        Serial.println(HDC1080_1.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
        Serial.print("Device ID=0x");
        Serial.println(HDC1080_1.readDeviceId(), HEX); // 0x1050 ID of the device
        Serial.print("Device Serial Number=");
        sernum = HDC1080_1.readSerialNumber();
        sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
        Serial.println(format);
    
        // Channel 2: Setup
        SDApin = D6;
        Wire.begin(SDApin, SCLpin);
        delay(500);
        // ** CCS811 **
        Serial.println("*** CCS2 ***");
        // Print CCS811 versions
        Serial.print("CCS2 :");
        ccs811_2.set_i2cdelay(50);
        ccs811_2.begin();
        delay(500);
        ccs811_2.start(CCS811_MODE_1SEC);
        delay(1000);
        Serial.print("setup: hardware    version: "); Serial.println(ccs811_2.hardware_version(),HEX);
        Serial.print("setup: bootloader  version: "); Serial.println(ccs811_2.bootloader_version(),HEX);
        Serial.print("setup: application version: "); Serial.println(ccs811_2.application_version(),HEX);
        // HDC1080
        HDC1080_2.begin(0x40);
        Serial.print("Manufacturer ID=0x");
        Serial.println(HDC1080_2.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
        Serial.print("Device ID=0x");
        Serial.println(HDC1080_2.readDeviceId(), HEX); // 0x1050 ID of the device
        Serial.print("Device Serial Number=");
        sernum = HDC1080_2.readSerialNumber();
        sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast);
        Serial.println(format);
    
        vBootphase = false;
      }
      else
      {
        // Channel 1
        SDApin = D2;
        Wire.begin(SDApin, SCLpin);
        delay(500);
        // ** CCS811 **
        Serial.print("\nSensor connected on: ");
        Serial.print("SDA - ");Serial.print(SDApin); Serial.print(" | SCL - ");Serial.print(SCLpin);
        Serial.print("\n");
        ccs811_1.read(&eco2,&etvoc,&errstat,&raw);
        delay(500);
        Serial.print("ErrStat: ");Serial.print(errstat);Serial.print(" | ");Serial.println(ccs811_1.errstat_str(errstat));
        if(!errstat==CCS811_ERRSTAT_OK) 
        {
          Serial.print("CCS811-1: ");
          Serial.println("Failed to read from sensor");
          ccs811_1.begin();
          delay(500);
          ccs811_1.start(CCS811_MODE_1SEC);
        }
        else
        { 
          Serial.print("CCS811-1: ");
          Serial.print("eco2=");    Serial.print(eco2);     Serial.print(" ppm | ");
          Serial.print("etvoc=");   Serial.print(etvoc);    Serial.print(" ppb");
          Serial.print("\n");
        }
        // ** HDC1080 **
        Serial.print("T="); Serial.print(HDC1080_1.readTemperature()); Serial.print("C");
        Serial.print(", RH="); Serial.print(HDC1080_1.readHumidity()); Serial.println("%");
    
        // Channel 2
        SDApin = D6;
        Wire.begin(SDApin, SCLpin);
        delay(500);
        // ** CCS811 **
        Serial.print("Sensor connected on: ");
        Serial.print("SDA - ");Serial.print(SDApin); Serial.print(" | SCL - ");Serial.print(SCLpin);
        Serial.print("\n");
        ccs811_2.read(&eco2,&etvoc,&errstat,&raw);
        delay(500);
        Serial.print("ErrStat: ");Serial.print(errstat);Serial.print(" | ");Serial.println(ccs811_2.errstat_str(errstat));
        if(errstat!=CCS811_ERRSTAT_OK) 
        {
          Serial.print("CCS811-2: ");
          Serial.println("Failed to read from sensor");
          ccs811_1.begin();
          delay(500);
          ccs811_2.start(CCS811_MODE_1SEC);
        }
        else
        { 
          Serial.print("CCS811-2: ");
          Serial.print("eco2=");    Serial.print(eco2);     Serial.print(" ppm | ");
          Serial.print("etvoc=");   Serial.print(etvoc);    Serial.print(" ppb");
          Serial.print("\n");
        }
        // ** HDC1080 **
        Serial.print("T="); Serial.print(HDC1080_1.readTemperature()); Serial.print("C");
        Serial.print(", RH="); Serial.print(HDC1080_1.readHumidity()); Serial.println("%");
    
        delay(10000);
      }
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: How To: Prevent duplicate coding

    What about using arrays? An array of CCS811, an array of int for SDApin, an array of ClosedCube_HDC1080 etc? Then use a loop to deal with each device. Just needs one set if code for however many devices there are.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  3. #3
    Join Date
    May 2020
    Posts
    4

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by 2kaud View Post
    What about using arrays? An array of CCS811, an array of int for SDApin, an array of ClosedCube_HDC1080 etc? Then use a loop to deal with each device. Just needs one set if code for however many devices there are.
    Thank you for the thought. I tried that, but it did not work out while the CCS does not read accurate values over 2 sensors when using only 1 alias. Apparently you need 1 per sensor.

    In the mean time somebody suggested to use a vector. This I checked and indeed it did.
    Code:
    std::vector<CCS811> vec_of_CCS811(2);
    int SDApin[]={D2,D6};
    and
    Code:
    for (int i = 0; i < vec_of_CCS811.size(); ++i){
       Wire.begin(SDApin[i], SCLpin);
       vec_of_CCS811[i].set_i2cdelay(50); 
       vec_of_CCS811[i].begin();
       .......}

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: How To: Prevent duplicate coding

    I suggested using arrays as I wasn't sure Arduino supported vectors - and you know in advance how many elements are needed. Your SDApin is an array, and CCS811 could be as well:

    Code:
    #define NO_SENSE 2
    #define C_DELAY 50
    
    CCS811 ccs[NO_SENSE];
    const int SDApin[NO_SENSE]={D2, D6};
    ...
    for (int i = 0; i < NO_SENSE; ++i){
       Wire.begin(SDApin[i], SCLpin);
       ccs[i].set_i2cdelay(C_DELAY); 
       ccs[i].begin();
       .......}
    Using a vector really only makes sense in these cases where the number of elements isn't known at compile time - or you need some specific 'feature' of a vector.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  5. #5
    Join Date
    May 2020
    Posts
    4

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by 2kaud View Post
    I suggested using arrays as I wasn't sure Arduino supported vectors - and you know in advance how many elements are needed. Your SDApin is an array, and CCS811 could be as well:

    Code:
    #define NO_SENSE 2
    #define C_DELAY 50
    
    CCS811 ccs[NO_SENSE];
    const int SDApin[NO_SENSE]={D2, D6};
    ...
    for (int i = 0; i < NO_SENSE; ++i){
       Wire.begin(SDApin[i], SCLpin);
       ccs[i].set_i2cdelay(C_DELAY); 
       ccs[i].begin();
       .......}
    Using a vector really only makes sense in these cases where the number of elements isn't known at compile time - or you need some specific 'feature' of a vector.
    Thanks. I will retry, because I also do understand using vectors also may have an impact on the limited available memory in NodeMCU.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: How To: Prevent duplicate coding

    Unless you need them, IMO I'd avoid using C++ containers and C++ STL (algorithms etc). When I did some Arduino programming a few years ago I basically just used C with some specific Arduino extensions.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  7. #7
    Join Date
    May 2020
    Posts
    4

    Thumbs up Re: How To: Prevent duplicate coding

    Quote Originally Posted by KapakoProf View Post
    Thanks. I will retry, because I also do understand using vectors also may have an impact on the limited available memory in NodeMCU.
    Also works and is even better fitting in the rest of the project coding.
    Learned a lot for you both. Thanks

  8. #8
    Join Date
    Feb 2017
    Posts
    535

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by KapakoProf View Post
    how to compact the code
    Why not simply use functions and structs?

    One function for example could be called setup. It would expect a struct as input. The struct contains all data that is necessary to setup a channel. The function operates on the struct only. It means that by passing different structs to setup, different channels are being setup.

    The algorithm part of the "code" is now present once in the setup function, and the data part of the "code" is present in the structs and there are as many structs as there are channels. It means duplication is minimal.

    The structs must be stored somewhere. There are several standard data structures available for that: arrays, vectors, list, sets, maps etcetera. All will "work", the question is which is most suitable.

    There are more advanced ways of introducing abstractions in programs but using functions is the most fundamental. It belongs to a programming paradigm called procedural programming.
    Last edited by wolle; May 14th, 2020 at 12:41 PM.

  9. #9
    Join Date
    Nov 2018
    Posts
    35

    Re: How To: Prevent duplicate coding

    Duplicated effort from duplicating poster -> https://www.cplusplus.com/forum/beginner/270475/

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,277

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by salem_c View Post
    Duplicated effort from duplicating poster -> https://www.cplusplus.com/forum/beginner/270475/
    If must be rather tedious not to respond constructively but only scour the internet seeking out duplicate posts. Well done, sir.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by Arjay View Post
    If must be rather tedious not to respond constructively but only scour the internet seeking out duplicate posts. Well done, sir.
    He responded on the other site.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,277

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by 2kaud View Post
    He responded on the other site.
    Fantastic, why post here that it's a dupe, then?

  13. #13
    Join Date
    Feb 2017
    Posts
    535

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by Arjay View Post
    Fantastic, why post here that it's a dupe, then?
    So that people here become aware of the double post and can avoid wasting time on something that has already been sorted elsewhere. It's very annoying to find out that your effort was for nothing.

    The problem is that the perspective of the double posting OP is different. It makes sense to start several independent solution processes. It increases the chance of coming closer to an optimal solution. This strategy is used commercially at competitive programming sites like Topcoder,

    https://en.wikipedia.org/wiki/Topcoder

    The difference is that if you post at Topcoder you are aware of the setup and you get paid if you "win" but at an ordinary forum you don't. You do it voluntarily just to help (and maybe get some gratitude for it). So in an ordinary forum situation you tend to dislike double posting because you feel you are being used by a cunning OP.

    This is a dilemma but I would recommend OPs to be open about double posting in order not to piss people off because then they may get no solution at all. In this case the OP indicated double posting but without actually saying so in #3,

    "In the mean time somebody suggested"

    It could have been made clear already at the outset with a link to the other site. I think openness is the best strategy. The OP gets a wider audience for the question and no one feels offended.
    Last edited by wolle; May 19th, 2020 at 03:17 AM.

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,277

    Re: How To: Prevent duplicate coding

    Just because someone has double posted doesn't mean it has been answered elsewhere. At any rate, it amazes that folks have the bandwidth to visit other sites enough to discover when something has been double posted.

  15. #15
    Join Date
    Feb 2017
    Posts
    535

    Re: How To: Prevent duplicate coding

    Quote Originally Posted by Arjay View Post
    Just because someone has double posted doesn't mean it has been answered elsewhere.
    No but if you know about the double post you can better decide if and how you want to reply. I cannot think of any situation where awareness of the existence of a double post would be in the way.

    At any rate, it amazes that folks have the bandwidth to visit other sites enough to discover when something has been double posted.
    You don't need much bandwidth today. There aren't that many programming forums and the overall activity is much slower than it used to be. I used to post at the Sun Java forum in its heyday and there was hundreds of questions each day and hundreds of replies to each from a large community of regular posters. Those were the days...
    Last edited by wolle; May 19th, 2020 at 11:57 AM.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)