User Code and User Varibales

This is an open call to any one who has been able to get code working using the user code hooks. HELP!i am trying to get a simple script running that sets pin8 high when its hits a waypoint. i know where i need to call the script and i know where it needs to be placed but im falling down at how to get the APM_config.h working with the UserCode and the UserVaribales.In APM_Config i have:#define CAMERA_TRIGGER_ON_WAYPOINT camera_trigger_on_waypoint();#define CAMERA_TRIGGER 1#define CAMERA_PIN 8#define pinMode(CAMERA_PIN,OUTPUT):then in UserCode i have:#ifdef CAMERA_TRIGGER_ON_WAYPOINTvoid camera_trigger_on_waypoint(){digitalWrite(CAMERA_PIN,HIGH);}#endifand in UserVariables i have#if CAMERA_TRIGGER == 1pinMode(CAMERA_PIN,OUTPUT);#endifThe error that i get is saying that digitalWrite is out of scope.Thanks for any help

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • @Adam 

    How do you get the Arduino functions like pinMode() and digitalWrite() to be recognized in the Ardupilot firmware? Is there a package that needs to be downloaded?

  • Hi @Adam,

    Did you follow the instructions in my previous post, because it should solve your errors.

    I had written the camera code over a year ago. I am sorry, I don't have it with me now.

    The logic in software is simple and is just calling your function for switching high and low pulse with suitable delay. You should use a multimeter at output end to test.
    In hardware, you must use something like a thyristor in between camera and APM to pass high and low pulse.

    There is one of my post here:

    Hope it has helped you a bit.

    • Hi Shyam

      I did but im sorry to say that i need a little more details about what to put where. I cannot find the UseCode.h file do you mean the UserVariables for the custom defines ?
      as for the hardware we have the APM connected to a logic level converter going between it and a raspberry pi
      and i have been following the other post and i can follow the logic its just getting the code to compile with out all the errors
    • Hi Adam,

      It was my mistake, I meant UserVariables.h. Name should not matter, the thing is you put all the #define, etc inside a header file (.h) and use the functionality and functions inside a (.pde or .ino) files. These are the naming conventions for an arduino language. They are just like .cpp files in C++. This is just to stay systematic. The files named with User*** is just for users like us to put their custom code into these files and not elsewhere.

      I hope you get my point now? Good to see that your code is compiled now.



    • Sorry to keep asking the same question's man but this problem is causing me some serious issues.

      This is what i have in the UserCode.pde:

      void camera_trigger_on_waypoint()
      digitalWrite(cameraPin, HIGH);
      digitalWrite(cameraPin, LOW);

      and then this is what i have in the UserVariables.h file:

      #define CAMERA_TRIGGER_ON_WAYPOINT //for code that is to be run to trigger a camera at the waypoints
      #define CAMERA_TRIGGER 1

      #if CAMERA_TRIGGER == 1
      const int cameraPin = 8;
      pinMode(cameraPin, OUTPUT);

      If i have understood you correctly, i need to define the variables (such as the int for the camera pin and the pin mode) in the .h file, like i have above, and then I can use them in the UserCode.pde file ?

      The issue that I have been having from the start is that when i try this in any form i get 

      UserVariables.h:13:8: error: expected constructor, destructor, or type conversion before '(' token
      UserCode.ino: In function 'void camera_trigger_on_waypoint()':
      UserCode.ino:49:31: error: 'digitalWrite' was not declared in this scope

      Ive added the files so that you might be able to replicate the error if you wouldnt mind



    • I think you have been struggling with this for quite some time. 

      Don't define pinMode(cameraPin, OUTPUT); in that header file, it is a function. As I said, MACRO replaces whatever you write during compile time. If you write a function (pinMode(..) ) there, it will replace its contents during compile time and it will (may) fail if you don't have supporting header file for it at that instance of the compile. Get my point?

      Header files define 'What'  and cpp or .pde files define 'how'. So variables can be defined anywhere, however it is good to categorize them into header files (though it is your choice).

      You are asking some basic questions in C++ (or Cpp). There is nothing wrong in asking, however it may be good if you learn a bit more about header and .cpp (i.e. pde, ino, are extensions like cpp, but for Arduino). Have a look at this link and search google a bit.

      Does the code compile 'without' your changes? Which firmware are you using (e.g. Arducopter v3.0.1)? I have a new install on my PC, I will try to compile the code ones its up.



    • Well i had a brain wave and Im not to sure its the way it should be done but it should logically work.

      i have added a call to do_take_picture() in the verify_nav_waypoint() function.

      Now however i have new error, when i upload the firmware to the pilot I now get a solid green light and an inability to connect to anything.

      Anyone know what is causing this and how to fix it ?

      oh and i am using a stock version of the 3.2-rc1 arducopter firmware to test the upload.

  • Developer
    See here for help on building The APM
  • You are defining a MACRO.

    You have an incorrect MACRO declaration. MACRO's are replaced at compile time. You have said:

    #define CAMERA_TRIGGER_ON_WAYPOINT camera_trigger_on_waypoint();

    This would replace this line with contents of the function camera_trigger_on_waypoint() in the header file itself. I am sure you don't want that. When it replaces it is unable to get the reference to digitalWrite, something not available at this line during the compile as it needs to reference an Arduino library. Secondly, don't use semi-colon in MACROs (It may work but not the right practice).

    Use instead,


    2. Put all your custom #define code in UserCode.h. Put your functions and running code in UserCode.pde. Try to be systematic, it will save you hassle with your own code. 

    Hope this helps to solve the error!

This reply was deleted.