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 4195 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…
Parents
  • 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
Reply
  • 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
Children
No Data
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