element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum more Arduino silliness.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 23 replies
  • Subscribers 385 subscribers
  • Views 4192 views
  • Users 0 members are here
  • arduino IDE 2.0.3
Related

more Arduino silliness.

phoenixcomm
phoenixcomm over 2 years ago

As a C programmer and learning Java,  I like to keep things VERY SIMPLE!!!

so normally I would have my file pins.h which would include a bunch of #defines like RST_1 13, etc. This now keeps me sane when trying to use it and stops me from wondering if is it this pin or that pin. (for amplification please see it below.  

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

So in the same header file I also want my pinMode statements which now can use my mnemonics rather than the stupid pin number.  this now is hitting close to 20-30 lines. 

#define RST_1 13
#define RST_1 12
pinMode(RST_1, OUTPUT);
pinMode(RST_2, OUTPUT);
BUT THE IDE tosses errors like  error: expected constructor, destructor, or type conversion before '(' token pinMode (RST_1, OUTPUT);

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 2 years ago +4
    Hi Cris, It might be because the first line is defining RST_1, but the second line is defining it a second time, i.e. it states RST_1 on the second line too, where it should be RST_2 I guess. However…
  • baldengineer
    baldengineer over 2 years ago in reply to phoenixcomm +4
    phoenixcomm said: THEREFORE either the definition of pinMode(pin, mode); LIES where pin MUST be a value. which I don't believe THEN AND I DO BELEIVE the #define is not being substituted at compile time…
  • ntewinkel
    ntewinkel over 2 years ago in reply to shabaz +4
    shabaz , that looks great. I like your organized programming style. I totally agree on the readability of the code - it's not often we're stuck with the tiniest of chips anymore, so I too will choose…
  • shabaz
    shabaz over 2 years ago

    Hi Cris,

    It might be because the first line is defining RST_1, but the second line is defining it a second time, i.e. it states RST_1 on the second line too, where it should be RST_2 I guess.

    However, the error text doesn't help as much as it could.

    Incidentally, some IDEs will detect this as it is typed, and flag it as an error before compilation. With CLion, if I try to do that, I see this:

    image

    It automatically shades the second 'BOB' to indicate an issue. If I hover over it, then it states the reason, sometimes in a better way than a compiler:

    image

    I guess Arduino code could be typed in CLion first, and then copy-pasted into the Arduino IDE, or perhaps it allows a different editor to be used.

    CLion is not free unfortunately though, so this isn't an option always, but just mentioning it in case there are other IDEs with this sort of capability too, that people may be aware of.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 2 years ago

    Love Abbott & Costello!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to shabaz

    I believe NexGen is trying to find a convient way to set the pinmode in a header file, Im not sure of the answer...

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • ntewinkel
    ntewinkel over 2 years ago in reply to scottiebabe

    Right, you can't (or at least shouldn't) be running executable code in a header file.

    So in addition to the RST_1 being defined twice, maybe the goal is to also #define the pinmode() calls to reduce it down?

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • ntewinkel
    ntewinkel over 2 years ago

    Hi phoenixcomm ,

    As shabaz pointed out, RST_1 is double defined, but in addition, if this is all in the .h file, then you shouldn't have the pinMode calls directly there.

    If the goal is to shorten the calls into a define, you'd have to #define it the same way, something like:
    #define SETUP_RST_1 pinMode(RST_1, OUTPUT)

    and then in setup() you'd call it as follows:

    setup() {
       SETUP_RST_1;
    }

    However, I don't think that really cleans up your setup() function much, and it's a bit hokey. And you end up typing more stuff overall.
    I would recommend not using #define for that purpose. Maybe use a second C file to hold the setup commands? Or just put those pinMode functions down lower in the main sketch file in a separate function. Then your setup function would look more like:

    setup() {
       setup_all_pins();
    }

    (I also recommend a better name - one that matches the purpose of those pins a bit better.)

    If you like even cleaner code, I think object oriented might be worth a look. Encapsulate all the functionality of a given thing into a separate file, and then from the main file just initialize it and call functions on it that do only what you need in the main sketch.

    For example, consider my thermistors sample. The attached zip file contains the sketch (for a Wemos D1 Mini), with the Thermistor object.
    I keep the thermistor object in the separate Thermistor.cpp and Thermistor.h files, and in the main sketch I just set up each thermistor with a single line as follows:

                //    pin, R_nom, Nom_temp, Beta, divider_resistor
    Thermistor therm6(A0, 5000, 25.0, 3892, 10000);

    And to grab the temperature in the loop() of the sketch, it’s also just one line:
       float temp6 = therm6.getTemperature();

    And it’s easy to repeat as often as you like - each is a separate object that takes care of its own variables, its own setup, its own pin handling, its own math... so you don’t even have to keep all of that straight in your main sketch - ie, no arrays of numbers upon arrays of numbers for all of their settings. No carrying of extra functions to do all the thermistor-specific math. Once you have the object built and tested, the rest is just neatly tucked out of sight. And reusability becomes trivial - just copy and paste the object files.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 2 years ago

    its a typo.

    #define RST_1 13
    #define RST_2 12
    pinMode(RST_1, OUTPUT);
    pinMode(RST_2, OUTPUT);
    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to scottiebabe

    Oh, I missed that bit of the question. I can't think of better options than what Nico suggests either. Either a nice setup() or init() type of function, or the OO method with each defined class implementing its own initialization code. Technically the latter in C++ allows the code to reside in the header file if that's really desired, but whether that is good practice or not for this scenario, is another issue (personally I would put it in the C file).

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • Andrew J
    Andrew J over 2 years ago

    Ntewinkel is right.  Another way of looking at it, depending upon where in your C file you include that header, is you are trying to run code outside of a function, whether that is setup(), loop(), main(), anoFunction().  You can put code in an include file, along with the pin definitions as long as the include statement is placed inside a function.  HOWEVER, you must be aware of the ordering of code when the compiler processes all this: if it finds a reference to RST_1 before it has been included it will throw an error.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • scottiebabe
    scottiebabe over 2 years ago in reply to shabaz

    That's what I normally do, though the my setup function ends up being a monster having to deal with input versus output, pullups, analog select, peripheral mapping, interrupts, clocks, etc etc but the Arduino environment takes care of most of that for you. Microchip provides structs with bitwise access for all their peripherals the same could be done on any mcu platform for example

    typedef struct tagPORTABITS {
      uint16_t RA0:1;
      uint16_t RA1:1;
      uint16_t RA2:1;
      uint16_t RA3:1;
      uint16_t RA4:1;
    } PORTABITS;
    extern volatile PORTABITS PORTAbits __attribute__((__sfr__));

    /* PORTA */
    #define _RA0 PORTAbits.RA0
    #define _RA1 PORTAbits.RA1

    So to write to a gpio pin,

    _LATA0 = 1; // of course you can add a custom define for _LATA0...

    If its an input you would read from the port input, if its an output you would write to the output latch

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to Andrew J

    andrew j yes but the #define #typedef and the such are handled be the PRE-PROCESSOR which means If my defines are in global space EVERYBODY see them.  I understand compiles / interpreters very well as I have written a few in my time. pleas see my next comment you'll like it.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube