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 Hi, I need some hellp with my code structure.
  • 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
  • State Verified Answer
  • Replies 38 replies
  • Subscribers 384 subscribers
  • Views 5052 views
  • Users 0 members are here
Related

Hi, I need some hellp with my code structure.

phoenixcomm
phoenixcomm over 2 years ago

for you that don't know I grew up writing C code. So my Code Base has multiple files currently working on my Landing Gear for my sim. 

I have the following files in the LandingGear directory:
LandingGear.ino
isr1.c through isr5.c 
lamps.c 
and landingGear.h

so in lamps.c  i have my code for the lamp test. 

void lampTest( int state ){
for( int count = 0; count < 10; count++;) {
digitalWrite( Lamps.pin[count] ); }
}

it is called from isr4 (isr4.c)

it is also  declared in landingGear.h


#define TRUE 1
#define FALSE -1
#define ON 1
#define OFF 0

/************************** Prototypes *****************************/

void lampTest( int state );
void lamp( Lamps.pin[count], int state );
void blink( Lamps.pin[count] );

typedef struct Lamps{
char name[];
char name[];
lamp} Lamps[] = {
{22, "RIGHT", "RED"}, {23, "NOSE", "RED"}, {24, "LEFT", "RED"},
{25, "RIGHT", "GREEN"}, {26, "NOSE", "GREEN"}, {27, "LEFT", "GREEN"},
{28, "WARNnose", "GREEN"}, {29, "WARNgear, "ORANGE"}, {30, "WARNnoseDis, "BLUE"}, {31, "WARNskid", "RED"}};

and pucks with this isr4.c:2:12: error: ‘ON’ undeclared (first use in this function)    lampTest( ON );

cam anybody give me a straight answer why this happens???

~~  thanks C Harrison

  • Sign in to reply
  • Cancel

Top Replies

  • shabaz
    shabaz over 2 years ago +8
    Hi Cris, The following rules extremely strong guidelines I think apply to your specific scenarios: (1) For every .c file apart from main.c, create a header file with the same name. Otherwise, it's…
  • shabaz
    shabaz over 2 years ago in reply to phoenixcomm +5
    Hi Cris, This is what's causing the problems, because there will be corner-cases (sometimes more often than not) where things will break down and not compile (as you have seen) unless those guidelines…
  • ntewinkel
    ntewinkel over 2 years ago in reply to shabaz +3
    What shabaz said ^ It can be tempting to cut corners by skipping coding best practices, but then you often run into issues that cost you so much more time and frustration later.
  • ntewinkel
    0 ntewinkel over 2 years ago in reply to phoenixcomm

    The way you’re doing it, every time a file includes that .h file, it tries to create a new instance of the lamps[] array.

    That’s fine if you only include it once from one file, but as soon as other files need to know about the array or the struct, you either don’t get the definition or you redefine the array.

    It gets even more tricky to keep track of it all when files include header files that include other header files.

    I’m guessing some compilers just don’t allow that in header files due to those reasons. You might be able to get away with it by adding some compiler flags, but why not just follow the best practices and have code that plays nice everywhere.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 2 years ago in reply to phoenixcomm
    phoenixcomm said:

    and again in lamps.c


    #include "include/landingGear.h"
    #include "include/lamps.h"

    phoenixcomm said:

    and I include it in (lamps.h)

    #include "include/landingGear.h"

    Because you included landingGear.h in lamps.h you are essentially doing the following in lamps.c:

    #include "include/landingGear.h“
    #include "include/landingGear.h"
    // + other stuff from lamps.h

     

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • phoenixcomm
    0 phoenixcomm over 2 years ago in reply to ntewinkel

     ntewinkel then you end up with a mess of unreadable code (toilet roll).  

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

    Hi Cris,

    The C++ guru comments equally apply to C. According to this website, the Motor Industry Software Reliability manual also states exactly what is being suggested, and this applies specifically to C programming: 

    image

    If you define a variable (which is an object) without static and outside of a function, in a header file, then to adhere to that rule, you'd also need an extern declaration in a header file too, i.e. you'd need to #include another header file in the header file! Currently your code does not do that either, but it wouldn't be a good way anyway.

    In contrast, the more normal approach which meets that rule, is to define in a C file and then to #include a file containing extern where needed.

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

    Just as an idea, if you don't mind uploading your files (it doesn't have to be all of them if you don't wish to, it can be just the relevant ones) I can make the changes to meet the suggestions, and then you'll be able to see it in a familiar setting, since you'll recognize your code and the changes.

    If you're unhappy with the changes, you could always just use your original code, so there's no damage done, since you'll always have your original local copy.

    An easy way to share the code:

    (1) At the replit.com website, create a free account, and then select Create a Repl (it may be somewhere in the center of the webpage). Give it a name:

    image

    (2) Click on the three dots icon, and then select Upload folder, and select your code. It will automatically upload it all.

    image

    (3) This is what it looks like with the code uploaded. It is possible for you (or anyone else) to inspect each file, and make any changes.

    image

    (4) To allow others to edit your code, click on Invite on the right side, and then you can click as shown.

    image

    Anyway, this is just an idea, not essential, but could help change things maybe.

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

     shabazjust a side note. I meet him years ago while I was in NJ. at his about  C with Classes, sort of a preprocessor at Princeton, We all thought it was a joke. If you want to write with classes use Java as that is what it was designed to do. While  Don Knuth did in fact win the ACM Turning Award (74) and his series "The Art of Computer Programming" ( must-reads! alas, BS did not, nor did the ass from Apple. In my humble opinion, Brian W. Kernighan and Dennis M. Ritchie(83)  {AMC Turring Award} did more by developing  C and Unix than anybody in the history of computing. While working at Inderdata I installed 8/32 #1 at Bell labs where I meet them both. 

    also, I noted that in one example of typedef a struct  stack exchange: 

    typedef struct {
      unsigned char current;
      unsigned char start;
      unsigned char target;
      unsigned long startTime;
      unsigned int duration;
    } led;
    This i beleave is an ilegale contruction as 'struct' is a key or reserved word

    I just hung it on my server: http://nexgen-simulations.com/E14/NexGen:%20Hacking/landingGear/

    ~~ CAH
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Jan Cumps
    0 Jan Cumps over 2 years ago in reply to phoenixcomm
    phoenixcomm said:
    This i beleave is an ilegale contruction as 'struct' is a key or reserved word

    It is a legal construct. You assign a type to that particular struct definition.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • michaelkellett
    0 michaelkellett over 2 years ago in reply to phoenixcomm

    C++ pre-dates Java.

    "C++ ..... created by Danish computer scientist Bjarne Stroustrup. First released in 1985 as an extension of the C programming language"

    "Java was originally developed by James Gosling at Sun Microsystems. It was released in May 1995 as a "

    Both quotes form Wikipedia:

    en.wikipedia.org/.../Java_(programming_language)

    https://en.wikipedia.org/wiki/C++

    SO if you wanted to write with classes in the  before 1995 then Java wasn't an option.

    There were other options:

    https://en.wikipedia.org/wiki/Object-oriented_programming

    but C++ was a brave (and in many ways successful) attempt to bring Oop and C together.

    C itself was not entirely original work:

    BCPL -> B -> C

    Wikipedia has good articles on the history of all of them.

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Andrew J
    0 Andrew J over 2 years ago in reply to Jan Cumps

    What's a "q type" Jan?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Andrew J
    0 Andrew J over 2 years ago in reply to michaelkellett

    Smalltalk ruled!!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • 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