Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. C Definition in sketch to configure library

C Definition in sketch to configure library

Scheduled Pinned Locked Moved Development
6 Posts 2 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Robinek70R Offline
    Robinek70R Offline
    Robinek70
    wrote on last edited by
    #1

    Hello,

    I'm trying to write own library.
    I would like to library was configurable from sketch by #define MY_USE_ADC_FILTER similar like configuration MySensors.
    But my library don't see definition from sketch in my .h and .cpp files.

    I don't know how it is done in MySensors library.
    Could someone just explain how to achieve similar behaviour?

    Thanks
    Robert

    mfalkviddM 1 Reply Last reply
    0
    • Robinek70R Robinek70

      Hello,

      I'm trying to write own library.
      I would like to library was configurable from sketch by #define MY_USE_ADC_FILTER similar like configuration MySensors.
      But my library don't see definition from sketch in my .h and .cpp files.

      I don't know how it is done in MySensors library.
      Could someone just explain how to achieve similar behaviour?

      Thanks
      Robert

      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      @Robinek70 are the defines in the sketch before including the library? That should be sufficient. If they already are, could you share your code?

      1 Reply Last reply
      0
      • Robinek70R Offline
        Robinek70R Offline
        Robinek70
        wrote on last edited by Robinek70
        #3

        it is something like below.
        Definitions
        #define MY_USE_ADC_FILTER
        #define MY_ADC_PIN
        are not visible in .h and .cpp.
        I don't know what I'm doing wrong :(

        //test.ino
        #define MY_RADIO_NRF24
        
        #define MY_USE_ADC_FILTER
        #define MY_ADC_PIN
        
        #include <RPlibs.h>
        #include <MySensors.h> 
        ...
        
        //RPlibs.h
        
        #ifndef MY_ADC_PIN
          #defie MY_ADC_PIN  A0
        #endif
        
        #define DT_FILTER  10
        int getAdc();
        
        //RPlibs.cpp
        #include <RPlibs.h>
        static int prevValue ;
        int getAdc() {
          int value = analogRead(MY_ADC_PIN);
          #ifdef MY_USE_ADC_FILTER
              prevValue = (prevValue *DT_FILTER  + value) / (DT_FILTER  + 1);
          #else
              prevValue = value;
          #endif
          return prevValue 
        }
        
        1 Reply Last reply
        0
        • mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          This works for me
          sketch.ino

          #define MY_RADIO_NRF24
          
          #define MY_USE_ADC_FILTER
          #define MY_ADC_PIN
          
          #include "RPlibs.h"
          

          RPlibs.h:

          #ifdef MY_USE_ADC_FILTER
          #error "Define ADC set!"
          #else
          #error "Define ADC not set!"
          #endif
          

          With the define, the build process outputs

          In file included from C:\Users\Micke\AppData\Local\Temp\arduino_modified_sketch_472392\sketch_sep20a.ino:6:0:
          
          RPlibs.h:2: error: #error "Define ADC set!"
          
           #error "Define ADC set!"
          

          and without the define it outputs

          In file included from C:\Users\Micke\AppData\Local\Temp\arduino_modified_sketch_303778\sketch_sep20a.ino:6:0:
          
          RPlibs.h:4: error: #error "Define ADC not set!"
          
           #error "Define ADC not set!"
          
          1 Reply Last reply
          0
          • Robinek70R Offline
            Robinek70R Offline
            Robinek70
            wrote on last edited by
            #5

            it's very strange for me.
            I checked under two environments:
            Arduino 1.6.7 and Visual Studio 2012 with plugin Visual Micro.
            And my result of tests:
            directly under Arduino, .h file see definition from sketch, but .cpp file don't :(
            under VS definition isn't visible in .h and .cpp files.

            1 Reply Last reply
            0
            • Robinek70R Offline
              Robinek70R Offline
              Robinek70
              wrote on last edited by Robinek70
              #6

              full test files

              File in path Arduino/Test/

              //test.ino
              #define MY_RADIO_NRF24
              
              #define MY_USE_ADC_FILTER 
              
              #include <RPlib.h>
              
              void setup() {
              }
              
              void loop() {
                Serial.println(getAdc());
              }
              

              Files in path Arduino/libraries/RPlib/

              //RPlib.h
              #ifndef MY_ADC_PIN
                #define MY_ADC_PIN  A0
              #endif
              
              #define DT_FILTER  10
              
              int getAdc();
              
              //RPlib.cpp
              #include <RPlib.h>
              #include <Arduino.h>
              static int prevValue;
              
              int getAdc() {
                int value = analogRead(MY_ADC_PIN);
                #ifdef MY_USE_ADC_FILTER
              		#error  FILTER
                    prevValue = (prevValue *DT_FILTER  + value) / (DT_FILTER  + 1);
                #else
              	#error  NO FILTER
                    prevValue = value;
                #endif
                return prevValue;
              }
              

              I always got
              #error NO FILTER

              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              13

              Online

              12.1k

              Users

              11.2k

              Topics

              113.4k

              Posts


              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • MySensors
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular