element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • 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
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • 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
Personal Blogs
  • Community Hub
  • More
Personal Blogs
NexGen Flight Simuator Flight Simulator 101 or back to college - part 14: Programming Arduino like an adult! Kill the loop()!
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: phoenixcomm
  • Date Created: 9 Apr 2023 7:53 PM Date Created
  • Views 755 views
  • Likes 6 likes
  • Comments 5 comments
  • programing
  • arduino
Related
Recommended

Flight Simulator 101 or back to college - part 14: Programming Arduino like an adult! Kill the loop()!

phoenixcomm
phoenixcomm
9 Apr 2023

Why did I say "Programming Arduino like an adult! Kill the loop()!"?  First, we need to know where this construct came from. A sketch is a variant of the C++ programming language. Hell, wait doesn't C++ have a main() function? Yep, it surely does. Then how and why did we (it) end up with the loop() function? Well, they had this dumb idea, that the language should be easy to master for artists and children.  Well C++ has never been one of the go-to languages as its scoping rules are worse than Java's. Syntactically it looks like C or C++ without classes. So you end up with a hot mess, that uses two (2) new functions. The first is void setup() which is where you stick your libraries and what they call "run once" (this opens the door for a lot of interpretations, and I will not get into that here). The second is a void loop() which should have never seen the light of day as it leads one into very sloppy programming as it redefines main() with a MACRO.
Here is my case for dumping void loop(). Do you see the keyword void which means your program can not return anything usable, like hey we failed, and maybe why.

#define FAIL -1
#define PASS  1
void setup(){
Serial.begin(9600);           // set up Serial library at 9600 bps
}

int main(){

}

int init_ser1()
   byte n = Serial.available();
   if(n !=0 ) {
      char x = Serial.read();
      if(x != '\n') {            // this fails as it did not get what it expected
         rtn = FAIL; }
      else { rtn = PASS; }
      program  }
       }  
    retirm( rtn );  
      }
 

 This is called diagnostic testing. In the example I have two (2) Arduino talking to each other, one is the salve, and the other is the master when the master powers up he listens for the slave he does not hear it in let's say two (2) seconds he now fails and sends an error message via the return() function. In the master, I have a table of error messages and their sender. So now it is a simple matter to do the following things. tell me (human) that the master can't find the slave, print the error to the screen, and or print the error to a log file. or just retry.
In NexGen sim I have about 8 radios, it would be nice to know I one was turned off.. LOL

  • Sign in to reply
  • phoenixcomm
    phoenixcomm over 2 years ago in reply to Jan Cumps

    runs fine I have been doing this for a while and btw this was done in the ARDUINO  IDE 

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

    Why i can overrid the main function?

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

    Indeed, you will get no errors.

    Because with GCC, if a function is defined in a library (e.g. int main() ) , and you provide a source file with the same function signature (e.g. a .ino file with int main(), then the linker will use your implementation. 

    regardless if that function is defined as weak or not.

    In your case, the Arduino's main() will not be called, not will the library call your setup() or loop(). I bet your setup() in your source doesn't run.

    Neither will the Arduino-essential functions  init(); initVariant(); USBDevice.attach();  or serialEventRun(); get called.

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

    jancumps I rewrote this dity and called it test.ino and I got NO errors!

    test.h
    #define FAIL -1
    #define PASS 1
    
    ----------
    
    #include "test.h"
    
    void setup() {
      Serial.begin(9600);  // set up Serial library at 9600 bps
      }
      
     int initserail_1() {
       int rtn;
       if( Serial.available()){
          if( Serial.read() != '\n' ) {
            rtn = FAIL; }
          else {
            rtn = PASS; 
          } return rtn; }}
          
    int init2() {
       int rtn;
       rtn = initserail_1;
       if ( rtn == FAIL ) { 
         rtn = FAIL;
         // write to the log 
         }
       if ( rtn == PASS ){
         rtn = PASS;
         // write to the log 
         }
       return rtn; }
       
    int main() { 
        if (init2 == PASS){ bulk(); }
        else { return FAIL; }}
        
    int bulk(){;} 
      
    
    #include "test.h"
    
    void setup() {
      Serial.begin(9600);  // set up Serial library at 9600 bps
      }
      
     int initserail_1() {
       int rtn;
       if( Serial.available()){
          if( Serial.read() != '\n' ) {
            rtn = FAIL; }
          else {
            rtn = PASS; 
          } return rtn; }}
          
    int init2() {
       int rtn;
       rtn = initserail_1;
       if ( rtn == FAIL ) { 
         rtn = FAIL;
         // write to the log 
         }
       if ( rtn == PASS ){
         rtn = PASS;
         // write to the log 
         }
       return rtn; }
       
    int main() { 
        if (init2 == PASS){ bulk(); }
        else { return FAIL; }}
        
    int bulk(){;} 
      
    
    
    #include "test.h"
    
    void setup() {
      Serial.begin(9600);  // set up Serial library at 9600 bps
      }
      
     int initserail_1() {
       int rtn;
       if( Serial.available()){
          if( Serial.read() != '\n' ) {
            rtn = FAIL; }
          else {
            rtn = PASS; 
          } return rtn; }}
          
    int init2() {
       int rtn;
       rtn = initserail_1;
       if ( rtn == FAIL ) { 
         rtn = FAIL;
         // write to the log 
         }
       if ( rtn == PASS ){
         rtn = PASS;
         // write to the log 
         }
       return rtn; }
       
    int main() { 
        if (init2 == PASS){ bulk(); }
        else { return FAIL; }}
        
    int bulk(){;} 
      

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 2 years ago

    Chris, in Arduino, everything is a class. Each Arduino library is a c++ class.

    The part about loop(), main() and macros is wrong. The Arduino libraries implement main(). And in that main(), they first call setup() once, then continuously loop(). .

    https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/main.cpp

    int main(void)
    {
    init();

    initVariant();

    #if defined(USBCON)
    USBDevice.attach();
    #endif

    setup();

    for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
    }

    return 0;
    }
    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • 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 © 2026 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