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
  • phoenixcomm
    phoenixcomm over 2 years ago

    TO ALL this is my take on everything.: 

    it was a typeo.. should have been RST_2 12

    this one is rather small but my code for my sim has hundreds of #defines. 

    here is the new file with IDE 2.0.3

    //Pin Names
    #define RST_1 13
    #define RST_2 12
    #define CE_1 11
    #define CE_2 10
    #define CLK_1 9
    #define CLK_2 8
    #define RD_1 7
    #define RD_2 6
    #define SCL 3
    #define SDA 2
    #define txPin 1
    #define rxPin 0

    //Pin Modes
    pinMode(rxPin, INPUT);
    pinMode(txPin, OUTPUT);
    // Digital Ports
    pinMode(RST_1, OUTPUT);
    pinMode(RST_2, OUTPUT);
    pinMode(CE_1, OUTPUT);
    pinMode(CE_2, OUTPUT);
    pinMode(CLK_1, OUTPUT);
    pinMode(CLK_2, OUTPUT);
    pinMode(RD_1, OUTPUT);
    pinMode(RD_2, OUTPUT);


    here are the first few lines of the .ino file
    #include <SoftwareSerial.h>
    #include <Wire.h>
    // #include "Adafruit_MCP23017.h"
    #include "pins.h"
    #include "Fuel.LoadSelector.h"
    BTW a few things to know ---- 

    #define is supported by the language.  it takes no ram space and the pre-processor changes the define name to the defined value. Once you #define it, it a value that can be used anywhere in the code body. 
    also ntewinkel the thing that is called 'void setup()' is very nutty. ie prohibition of executable code.  it should have been called init() and most likely have a return! in this chuck of code I must have rs-232 link up before I can continue.  part of the problem is with C++ which this is written in has the most rigid scoping rules. so for shits and giggles, my code base looks like this which fails and bitches about scoping rules of rxPin & txPin and other wonderfulness. :

    #include <SoftwareSerial.h>
    #include <Wire.h>
    #include "pins.h" // remember this is GLOBAL where all of my pins are not the pinMode() 

    int init(){} // Now this is where the pinModes() live.
    int initSerial(){
    int ret= 0;
    Serial.begin(9600);
    SoftwareSerial(rxPin, txPin); //rx & tx are defined in pins.h
    Serial.begin(9600);
    while (serial.availbe() > ) {
    char buffer = serialread();
    if (buffer = 0x0a ) ret = 1;
    else ret = -1;
    break;}
    return( ret ) ; }

    AND SO IT GOES.. 

    the supper crud about loop() and setup()  are macros see this page and are NOT required. Amen. 

    BTW It looks a if my code works when you use the eclipse tools with the AVR tool chain. 

    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. !!!!!

    AFTERMATH I really want to see the "railroad tracks" for this language!!!

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

    Everyone does things differently, if it were me, I would make the code look different. This example is in one file, but of course it should be split up into several files if desired (particularly for large programs).

    image

    Here is a main loop:

    image

    To see it more clearly, the code above is pasted at the following link because the Code-pasting tool on Verint seems broken: https://controlc.com/0f9abc38

    The video showing the guy reducing program space, is a very bad idea unless program space and speed is so tight. He has made the program unreadable, and it will only work on a particular microcontroller. If he used an ARM based microcontroller with the Arduino IDE, he would find his code totally broken because ARM devices do not have that PORTB that AVR chips do. Similarly, all his timings would be broken because he is no longer using the Arduino library functions such as the delay() function.

    It's up to the user to decide if they wish to use the Arduino library functions or not, that's the beauty of most programming languages, use whichever libraries you wish, or do it from scratch like he has done. His code size decreased but it got progressively more ugly. How many users would know that with the particular pin register he used, then it toggles the output. That's so specific to that microcontroller. 

    I'd be happy for any scrutinization by anyone. There are many ways to improve it, but I think it's fairly simple-to-follow, and a lot clearer than a lot of ugly Arduino code that people sometimes put together. Deleting the setup() and loop(functions) and replacing with main() and init() is something of a personal choice. I don't normally use Arduino IDE and so would write my own main() function, but equally I don't see it as an issue to use the setup() and loop() because that seems a simple way to gently push beginners to at least consider what code needs to run one-time, and what needs to run repeatedly, and separate it out for them, hence why the IDE creates those functions as placeholders.

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

    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 readable, debug-able, maintainable, portable code over tiny improvements in speed and space. That's especially important if you ever want to pass that work onto the next programmer - you can't be promoted if you're irreplaceable Wink

    You do have one tiny typo in the setup for the input pins, accessing out_pin instead of in_pin Slight smile

    I don't understand what the advantage might be, when using the Arduino IDE, of using main() instead of setup() and loop(). If that's the preference, then maybe the Arduino IDE isn't the right tool to use.

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

    Hi Nico,

    Thanks for spotting that! I totally agree with that, if you're using main() anyway, and removing all Arduino library functions, then there's hardly (if any) benefit left using the Arduino IDE.

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

    after you #define varname value there was no need for the next two lines of code and you now officiated the varnames with

    const char outpint[ varname, .....]

    const char input[ varname, ......] \

    and btw here is my testSerial()

    int testSerial() {
    int retValue = 0;  
    /* this function sets up and tests the serial port for communications. 
     * Returns -1 FAILURE or 1 SUCCESS
     * currently I will force return of 1; until I get the Mega working as this processor starts first and must send a 'Line-Feed' Sequence. 
     */
     
    Serial.begin(19200);
    while(Serial.available() > 0) {
        if ( Serial.read() == 0x0a ) {
          retValue = 1;
          break; }
        else {
          retValue = -1;         
        }}
    return retValue; }

    also when I include my canned messages

    // Messages array
    typedef struct messages {
      // displayNumber could be 0: Display_1, 1: Display_2, 2:Both Displays
      int displayNumber;
      char* data[9];} 
      MESSAGES[] = {
      {0, "Init" },
      {1, "Pls Wait"}, 
      {0, "InitComm"},.....}

    I get this nutty crud... should work in C

    Compilation error: typedef 'MESSAGES' is initialized (use decltype instead)

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

    dont worry about the typedef I solved it a little different.

    // Messages array
    typedef  struct  {
      // displayNumber could be 0: Display_1, 1: Display_2, 2:Both Displays
      int displayNumber;
      char* data[9];
      } message; 
    
      message Messages[] = {

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

    It's up to the programmer, I won't always use an array, but generally I try to if possible, because it's less lines of code and (to me) looks neater than a whole list of PinMode (sometimes followed a whole load of digitalWrite messages to set all the outputs initially low or high or whatever). Anything in arrays is more efficient in terms of lines of code usually. The arrays are a tiny amount of Flash space, compared to a lot of pinMode() function calls.

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

    Great, glad it's sorted!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • shabaz
    shabaz over 2 years ago in reply to phoenixcomm

    (I know it's resolved, but just in case anyone is wondering how such types of problems can be identified)

    One way is with the IDE. I have CLion installed, and it immediately identifies the issue as the user types the code. It puts a red squiggle underneath the text, and hovering over it provides the information. Other IDEs may do it too, but the default Arduino IDE tends to wait until compilation, to let the compiler figure it out. Productivity is increased a lot when the IDE is examining everything as it is typed.

    image

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

    (I know it's resolved, but just in case anyone is wondering how such types of problems can be identified)

    One way is with the IDE. I have CLion installed, and it immediately identifies the issue as the user types the code. It puts a red squiggle underneath the text, and hovering over it provides the information. Other IDEs may do it too, but the default Arduino IDE tends to wait until compilation, to let the compiler figure it out. Productivity is increased a lot when the IDE is examining everything as it is typed.

    image

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

    yes I thought it work but I fixed it with this:

    / Messages array
    typedef  struct  {
      // displayNumber could be 0: Display_1, 1: Display_2, 2:Both Displays
      int displayNumber;
      char* data[9];
      } message; 
      
      message Messages[] = {
      {0, "Init" },
      {1, "Pls Wait"}, 
      {0, "InitComm"},

    • Cancel
    • Vote Up +1 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