Interrupt Programming

This is example code for discussion. (not for the critique of methods)

ISR (ADC_vect)
 {
  byte low, high;

  low = ADCL;
  high = ADCH;

  adcReading = (high << 8) | low;
  adcDone = true;
 }  // end of ADC_vect


void loop()
 {
  int x = 0;

  x=x+1;
  // and do lots of other stuff
 }

What happens to the value of x when the program returns to loop from the interrupt? Will it be set to 0 or remain at the last previous value?

Does x need to declared as static in order to maintain its previous value when the execution returns after processing the ISR?

That interrupt doesn't touch x at all. The interrupt doesn't have any effect on x. x will behave just as it would if the interrupt weren't even there.

Delta_G: That interrupt doesn't touch x at all. The interrupt doesn't have any effect on x. x will behave just as it would if the interrupt weren't even there.

So, as a local variable, x will be set to 0 every time loop() is called by main().

There is no main() in the arduino IDE. ?

From main.cpp

See where it calls setup and loop.

int main(void)
{
    init();

    initVariant();

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

    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
        
    return 0;
}

Delta_G: ``` int main(void) { init();

initVariant();

if defined(USBCON)

USBDevice.attach();

endif

setup();

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

As a side note, how does this get compiled in with the program written in the IDE?

Just like it would be compiled in any other C++ environment. The IDE adds to your code when you compile it to include this header along with a bunch of other stuff when it includes Arduino.h.

Delta_G: From main.cpp

See where it calls setup and loop.

int main(void)
{
 init();

initVariant();

if defined(USBCON)

USBDevice.attach();

endif

setup();     for (;;) { loop(); if (serialEventRun) serialEventRun(); }         return 0; }

I love the libraries and community support, which is why I support the Arduinos; but I hate the hidden behind the scenes stuff like this.

Delta_G: Just like it would be compiled in any other C++ environment. The IDE adds to your code when you compile it to include this header along with a bunch of other stuff when it includes Arduino.h.

Not just like any other. This is my first time in 20+ years of programming that I have not declared main(). In all of my past IDEs for C/C++, [u]I[/u] declare main(). You don't in the Arduino IDE, and perhaps others, but I don't know of or use them.

This must be why loop() feels like it is acting as a subroutine call...because it is.

adwsystems: I love the libraries and community support, which is why I support the Arduinos; but I hate the hidden behind the scenes stuff like this.

You don't have to use it if you don't like it. Take the libraries you want and the gcc compiler and other tools and build your own environment. This stuff is hidden so people who don't know how to do that can still make their leds blink.

adwsystems: Not just like any other. This is my first time in 20+ years of programming that I have not declared main(). In all of my past IDEs for C/C++, [u]I[/u] declare main(). You don't in the Arduino IDE, and perhaps others, but I don't know of or use them.

It is COMPILED just like in any other C++ environment, which is what you asked. It is not WRITTEN just like any other which is not what you asked.

Yes, in any other environment you would have one header that has your main function in it. That's exactly what they've done here. They have a header with main in it. In any other environment your main would call other functions that you write. That's the same here, main calls setup and loop and init etc. The only difference is that you don't have to write it all the time. But even that is the case sometimes. I often have related C++ projects that are all using the same main function and differ only in what the functions it calls do.

Delta_G: You don't have to use it if you don't like it. Take the libraries you want and the gcc compiler and other tools and build your own environment. This stuff is hidden so people who don't know how to do that can still make their leds blink.

It is COMPILED just like in any other C++ environment, which is what you asked. It is not WRITTEN just like any other which is not what you asked.

Yes, in any other environment you would have one header that has your main function in it. That's exactly what they've done here. They have a header with main in it. In any other environment your main would call other functions that you write. That's the same here, main calls setup and loop and init etc. The only difference is that you don't have to write it all the time. But even that is the case sometimes. I often have related C++ projects that are all using the same main function and differ only in what the functions it calls do.

We are wondering off into semantics now. I don't have any header files with subroutines. The programming standards to which I have to abide, work from the main program file (containing the main() routine) out. So you know where it is and where you are going.

I worked through software product analysis for purchasing for nearly 10 "arduino-style" packages (community support packages with libraries and hardware modules available). You would be astounded by how far in front the Adruino community is over the "competitors". When situations like this arise, the question is: is it due to the gcc compiler/architecture or due to the wrapper. So far it has been the Arduino IDE wrapper, and rarely is the information to differentiate readily available.

It's sort of a conundrum for me. I really like to understand exactly how the project is built. Especially if I'm tracing my way through multiple layers of #include files to get to the declaration of a variable or function. On the other hand, I have zero interest in maintaining tool chains, using CLIs, maintaining .ini files, or writing my own make files like I did in the 80's and early 90's. As far as I'm concerned, we're well into the 21st century and if it can't be done via a GUI, I'm not interested. So, I live with the obfuscation of the Arduino IDE.

Another reason I stick with Arduino is for when I have compilation problems. I can post my code to various forums and literally thousands of other people can look at it using an almost identical environment.

The above notwithstanding, the most valuable tool that I've acquired since diving into the Arduino ecosystem a little less than a year ago is Agent Ransack. It has been extremely helpful in figuring out exactly what's getting pulled in when I compile. Also great for understanding complex libraries.

Again, if you don't like the Arduino IDE then nobody is making you use it. I don't. Though I do still tend to use that precanned main function and write setup and loop somewhere. I don't have to though and neither do you. The IDE is just one piece of the Arduino environment. You can use all the rest of it with any other IDE or GUI that you want to use. You can even go all command line and do it all by hand like we did in the old days.

I guess what I'm getting at is that I hear and understand your complaint, but what do you want done about it? Do you want to uncover all those things in the Arduino IDE? That ain't gonna happen because it's for the beginners. It hides those things so people can learn to code before they have to learn to build a build environment. If you feel like you've moved beyond that stuff then quit complaining that the kiddie toys are too simple and move on up to something like Eclipse where you have a little more control.

Delta_G, as long as you follow what I'm saying then we're cool. I have no desire to take a step back (to command line). I have moved way beyond it, but since I'm not being paid for (most of) the Arduino projects I undertake, time is of the essence. Doing everything myself is why I opted for the Arduino route. Unfortunately, some items, such as the one we have discussed here, should documented in an 'advanced reference section'. This is not the first time I have run into an issue due to the 'layout' of the Arduino IDE product or things being done behind the scenes. I don't believe it will be the last either. As long as the folks that are more in the know remember when responding to a question, is the person is asking because they don't know, and is especially true for the more advanced questions. (obviously this does not apply to questions that are answered in the reference section or asking for someone to write their program for them)

You would be surprised where I am finding these 'kiddie toys' being used. I have found them in some very manly applications in industry.

You've got a point that the documentation is a little lean. It is a community effort after all. Would you be so kind as to write some documentation of the kind you'd like to see? I'm sure there's a way to get it added to the site.

I know this stuff is out there. When I started with the Arduino several years ago I remember having a lot of the same questions and through a little diligent research was able to learn this stuff myself. So I know it can be done. I don't remember where I found it all, but it was all done using Google and this site so it can't be that hard to find.

I have heard some stories about the early Arduino compiler. I'm partly sorry I wasn't there and partly glad I didn't have to suffer through it. Based on the product evaluations I performed, I can say the Arduino group is close to a magnitude ahead of the competitors, in product availability, accessories (modules) available, compiler and support. Basically across the board. In no way am I sorry I chose the Arduino package.

adwsystems: In no way am I sorry I chose the Arduino package.

See that's the point I'm trying to make to you. You can have all the things you say you like about it and not ever have to use the IDE which you seem to be less than happy with. You can keep the Arduino libraries, the boards, the support, everything and never have to touch the IDE again.

The only reason I ever use the IDE these days is burning bootloaders (one button ease) and for answering questions on the forum related to it. Yet I'm writing tons of code to run on the Arduino, mostly using Eclipse these days.

Delta_G: See that's the point I'm trying to make to you. You can have all the things you say you like about it and not ever have to use the IDE which you seem to be less than happy with.

By "Arduino package", I speak of the hardware (processor board) and the IDE. It is a package deal, hardware and compiler. There is no "You can have all the things you say you like about [the IDE] and not ever have to use the IDE which you seem to be less than happy with." That doesn't make sense. That was the purpose of going the Arduino route.

I have a few folks that know I work with the Arduino set, they opted for other packages and are asking how it would be done in the Arduino. My usual answer is a combination of there's a shield and/or library for that. Done. Meanwhile they are working to develop one or the other, because there is hardware with not such a good library or a library for a chip, but no breakout board or shield on the market.

You still don't understand. What I am telling you is absolutely true. I do it every day. You can use all the same shields, all the same libraries, all the same everything. All the IDE really is is a vehicle to get the compilation done and to do things for you like add in function prototypes and certain includes. If you move over to Eclipse or something you don't have to rewrite or redevelop any libraries. You use exactly the same code. The only difference would be that you'd have to write your own function prototypes and include Arduino.h at the top of your files. You would download the IDE to get all the core files and just use them with any other means of getting it compiled that you want to. Your idea that if you want to use all the cool stuff that Arduino comes with means you have to use their IDE is just plain mistaken. All the IDE is is a (very) basic text editor and it has the ability to call some scripts to trigger the compile process. There's none of that which can't be done with any text editor you like and any other means of telling gcc to do its thing. There's absolutely nothing that the IDE uses that you can't use yourself.

Skipping a few steps, what are you using to program your arduinos?