Problem with my first sketch

Well I didn’t have much hair before but have less now !!!

I’ve attached the code I’ve written - it does compile but is just a fragment to create one routine at the moment. My main difficulty is lack of knowledge of C/C++. Curly brackets and semi-colons were my bain!!

The project is to measure temperature and humidity from 4 sensors. Rather than wiring to a central point (long wires may well not work!), I’m taking the wee black box to short leads with 3.5mm stereo type connectors. On switch on, led 1 will flash until plug 1 is inserted and the light will go solid showing that the measurements are being taken. When complete, that led will go out and No 2 will flash until the No 2 plug is inserted… . and so on.

So far the sketch is the led flashing and plug sensing function and the bare bones of the main routine.

I ran into the problem that the main routine is a loop() calling the led_flash function, in which I had another loop() to do the flashing - I found I had to abandon this second loop() and use a “do…while”. Does this mean that in any one sketch, there can only be one loop() function ?

Having got a clean compile, I then got this message

core.a(main.cpp.o): In function main': D:\Arduino\arduino-1.0.3\hardware\arduino\cores\arduino/main.cpp:11: undefined reference to setup’

What does this mean please.

Many thanks
Rob

file T_H_sensors attached (I hope)

T_H_Sensors.ino (1.36 KB)

You can only have one function named loop(). But you can have as many other functions all with different names as you want. The main() function your code gets run from will run the one named loop() over and over but there's nothing stopping you from calling other functions from loop()

The error is because main() also calls setup(). So you have to have a function named setup even if it is completely empty.

robgraham:
I ran into the problem that the main routine is a loop() calling the led_flash function, in which I had another loop() to do the flashing - I found I had to abandon this second loop() and use a “do…while”. Does this mean that in any one sketch, there can only be one loop() function ?

You can have lots of loops, but only one function called “loop”.

To make loops you use “for”, “do” or “while”.

Many thanks, guys, for your time in replying to questions now seen as very basic - that should get me sorted out for the moment.

But how the H*** is one meant to know these two facts unless it is stated for loop() and setup() in the Language Reference. I did a bit of C some 15+ years ago and have been programming in Basic on and off, so didn't see that I needed to find and read 'The Instructions' !

OK I had to learn to get the curlies in place, the semi-colons and not capitalise the first letter of words, and it may be obvious now (always in hindsight) about these two Structure words, but half a dozen clear words on each in the Language Reference would have saved me half an evening's frustration, and I'm sure I'm not the first.

Forums like this are here so that those involved can discuss all matters related to a topic, but I know from working on other forums how irritating it can be that posters come to the forums with trivial questions instead of doing a basic search (or using a bit of brain power !) first. In this case the primary and obvious source of the information is lacking what is clearly key information, and that I consider needs correcting.

Rob

So you are unfamiliar with C and C++, what do you actually expect people here to do ? Manually type out
a course in C and C++ programming for you ? How about buying or downloading an appropriate textbook
and reading it.

But how the H*** is one meant to know these two facts unless it is stated for loop() and setup() in the Language Reference

Because they're not part of the language, they're just constructs that abide by the rules of the language.

But how the H*** is one meant to know these two facts unless it is stated for loop() and setup() in the Language Reference.

This is a reasonable question, and here is the answer. The requirement in Arduino for a setup() and loop() function is not part of the C or C++ programming language, it is an aspect of the Arduino programming environment, and it is covered in any of the online elementary tutorials for writing Arduino programs, which you should have looked at.

Note that this is not actually a fundamental requirement for using an Arduino, there are other, more complicated and tricky methods of writing programs and downloading them into the Arduino hardware without using the Arduino IDE.

Any programming system and device will have various requirements that you have to learn to get it to work, which are not part of the programming language itself.

Well, I don’t have much hair either, but anyways. I bought one of those starters pack in January, and all the code seemed to have both setup and loop function, and they are also included in all the examples in Arduino IDE. I figured, ok, these function must already be called from somewhere. The Learning tab gives you the bareminimum example that is required for getting the code up and running. It also explains what the setup and loop are for.

I’m used to starting with examples before getting into the theoretical background, so I didn’t find this a big problem. But if you start from the theoretical background, I guess this kind of stuff could be mentioned elsewhere too (where exactly does it need to be added?). As in, you don’t need a main function of your own, since it’s already there.

C code requires a main function as a starting point, and in Arduino environment it’s found in main.cpp like this:

#include <Arduino.h>

int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

It calls setup(), and then loop() in an infinite for loop. This is always compiled into your sketch. You need to define setup and loop, but only once, because if you had multiple then the compiler wouldn’t know which one to jump into and would report an error.

Ah, sorry, not relevant to this topic, but now I also see what that thing about serialEventRun was about. I didn’t see that before… must be because I just looked up the main function for the first time…

The description here seems pretty clear-cut, particularly the last text paragraph on the page.

OK - you are all making the point that perhaps I didn’t do enough homework before I started, but having looked at all the pages / tutorials you have given, I would still argue that none of them say that you cannot actually leave out setup() - yes, they say that this is where the system goes to start the sketch, and perhaps I should have read into that that regardless of it being an empty function, it still had to be there.

Please remember where I am coming from in terms of background - I certainly read into the replies that you are all either professional programmers or certainly a long way up the learning curve. I am sure I am not the first person to have come to Arduino programming with an amateur background of a bit of programming knowledge. Equally I am sure I’m not atypical in reading a couple of short program scripts from tutorials and library functions and then setting forth using a language reference to guide me through the language application, knowing how to construct ‘ifs’, ‘whiles’, etc.

Now I would like someone can point me to a reference that is obvious to consult, that says that you MUST have setup(), even if it contains nothing. I am reasonably intelligent and if my thinking was that if there was nothing to set up then the function could be omitted, and I won’t be the first nor the last to do so.

I would also make the same challenge as far as loop() is concerned. The Language Reference says “the loop() function does precisely what its name suggests, and loops consecutively” - so then why shouldn’t I think that I can loop() in the main function and in the called functions? If the ‘system’ constrains one to only one use, then it should tell me so.

If this forum wants numpties like me to continue coming to ask the same question and you having to answer it over and over again, but for the want of very few words in the Language Reference, you will carry on doing so.

Rob

The Language Reference says "the loop() function does precisely what its name suggests, and loops consecutively" - so then why shouldn't I think that I can loop() in the main function and in the called functions?

Because you can only define a function name once in any sketch otherwise how would the program know which one to call ? In that respect loop() and setup() are no different to any other function.

UKHeliBob:
Because you can only define a function name once in any sketch otherwise how would the program know which one to call ? In that respect loop() and setup() are no different to any other function.

That is the good answer for loop() though the Language Reference has it under 'Structure', and perhaps one of the weaknesses in the semantics is the use of 'function' in the definitions rather than 'Function' with a capital, particularly as the Reference does list 'Functions'. It's the answer I should have realised from 'Function' being the equivalent of 'Sub-routine' - but that is hindsight.

But then I come back to the point I made earlier, that saying so in the definition of 'loop()' in the Language Reference would cost nothing and help the beginner who is the only one ever to go there.

Rob

That is the good answer for loop() though the Language Reference has it under 'Structure

That's because the function "loop" is part of the structure of a sketch, but it is not part of the language.

In the same way, I would not say "subroutine" and function" are the same thing, rather that a function is a type of subroutine. ( or, perhaps more aptly, that a function is a subroutine with a type)

robgraham:
Now I would like someone can point me to a reference that is obvious to consult, that says that you MUST have setup(), even if it contains nothing. I am reasonably intelligent and if my thinking was that if there was nothing to set up then the function could be omitted, and I won't be the first nor the last to do so.

michinyon:
The description here seems pretty clear-cut, particularly the last text paragraph on the page.

http://arduino.cc/en/Tutorial/Sketch

From the last paragraph at that link.

There are two special functions that are a part of every Arduino sketch: setup() and loop(). The setup() is called once, when the sketch starts. It's a good place to do setup tasks like setting pin modes or initializing libraries. The loop() function is called over and over and is heart of most sketches. You need to include both functions in your sketch, even if you don't need them for anything.

robgraham:
I would also make the same challenge as far as loop() is concerned. The Language Reference says "the loop() function does precisely what its name suggests, and loops consecutively" - so then why shouldn't I think that I can loop() in the main function and in the called functions? If the 'system' constrains one to only one use, then it should tell me so.

This is the first time I've heard someone think that. There are a lot of one-off complaints about what should be included in the documentation, and including all of them would result in someone having to go through a textbook just to get started.

That is the good answer for loop() though the Language Reference has it under 'Structure'

Isn't that the point though ? loop() and setup() are part of the structure of the language.

robgraham:
But then I come back to the point I made earlier, that saying so in the definition of 'loop()' in the Language Reference would cost nothing and help the beginner who is the only one ever to go there.

You've got a point, and being someone who came from a programming background, but not the Arduino (years ago), when I looked at sketches I wondered:

  • Where is "main"?
  • What do "setup" and "loop" do?

The answer as I now know is that the "simplified" the environment by providing their own "main" which calls "init", then "setup" and then "loop" repeatedly.

I suppose this simplifies it for beginners, and without that you would have to teach everyone to call "init" otherwise timers and stuff like that would not be set up properly.

I'm leaving this thread now, bruised by some of the comments and somewhat chastened. Having raised the question of just where it said so, I didn't expect the comments from most of the contributors, with possibly too much knowledge of the subject, to be so lacking in support for someone starting out, some particularly which I found harsh and critical.

Whereas my questions have been answered this time, I hope I can get through the rest of my project without having to return here; I will perhaps find another forum with users who aren't software gurus as I feel you all are.

One of the biggest failings of technical forums is the ability of the contributors to think down to the level of the original poster - they have all been there and now know it all. I've been on both sides of the fence and this has been a bad example of being on no-knowledge side for me.

One of the biggest failings of technical forums is the ability of the contributors to think down to the level of the original poster

it's a technical forum, not a classroom, and we're not all teachers.

I will perhaps find another forum with users who aren't software gurus as I feel you all are.

Perhaps you will.

robgraham:
I hope I can get through the rest of my project without having to return here; I will perhaps find another forum with users who aren't software gurus as I feel you all are.

I'm sorry you feel that way, and hope you find a friendlier forum. Bear in mind though that your second post in this thread took a rather aggressive tone:

robgraham:
But how the H*** is one meant to know these two facts ...

A lot of us answer question after question here, many of which are well-covered in the tutorials and example files. That's OK, I suppose, not everyone reads them. But when posters start to complain about how badly everything is run, it can tend to get peoples' backs up.

We are unpaid volunteers you know, and the documentation is, as far as I know, not open to us to edit.