Setup & Loop position in sketch

Hi to all,

I have a strange question... ::slight_smile: I'm trying to compile a large sketch (over 850 lines) with new ide 1.6.4 and I've got 2 different results, depending on where i put the Setup & Loop subroutines.

I the first case I put them at the beginning of sketch, just after globals declarations...
In this case compiler stops giving multiple errors about undefined subroutines.
In the second case I put Setup & Loop at the end of the sketch, after all other subroutines.
In this case compiler creates successfully the file for Arduino Uno without any error... ?

I've supposed, up to now :astonished: , that IDE would do some preprocessing, including automatically prototyping of subroutines... Isn't it ?

Just to explain better what happened, the first case was my normal configuration of sketches and, up to know, I've compiled all my other (smaller) programs without problems, including also previous versions of the same program that causes all that. Obviously, I dammned a while before understanding that the problem was the Setup & Loop position.
Also, I could'nt find anywhere here in Arduino site, infos about the position of Setup & Loop...

I've attached 2 screenshot of the compile process...

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

If your sketch is too big, use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your sketch as an attachment, but try code tags first.

I the first case I put them at the beginning of sketch, just after globals declarations...
In this case compiler stops giving multiple errors about undefined subroutines.
In the second case I put Setup & Loop at the end of the sketch, after all other subroutines.
In this case compiler creates successfully the file for Arduino Uno without any error... ?

Can you just reread the bit above, please.
I think you mean.

I the first case I put them at the beginning of sketch, just after globals declarations...
In this case compiler stops compiling and gives multiple errors about undefined subroutines.
In the second case I put Setup & Loop at the end of the sketch, after all other subroutines.
In this case compiler creates successfully the file for Arduino Uno without any error... ?

Sorry semantics.

So a copy of your codes would be helpful.

Tom.. :slight_smile:

Hi TomGeorge 8) & thank You for Your answer,
Yes, of course, the second Code Tag was the correct one... :smiley: , sorry for my english...

Now to Your question... Really You want to pass 850 lines of code, unknown by You, searching for something wrong :fearful: ?? We've already done this more & more times and it's not easy...
Moreover the code seems not to be the problem, because, if that where the problem, it would not compile the second time.

After that... not all the code is mine, so before releasing somewhat, I've to speak with my friend before, asking if we could do that :neutral_face:

So, let us try if we can put something together to reproduce the problem...

If you try to reduce the code to a much simpler example that exhibits the same problem, you'll almost certainly stumble across the real solution to the problem. Whatever the actual problem is, I can assure you it is not the location of setup() and loop()...

Regards,
Ray L.

It sounds like the IDE is getting confused and not creating the prototypes. Is this an .ino or .cpp?

doesn't the error message show that something was called during the set up function that hasn't been defined/declared yet.

somewhere what ever was called could have been used in the program so putting the setup/loop at the end fixs the problem as the compiler reads from top to bottom.

would be interesting to have seen what line 142 was in the code that failed.

this is more of a question that a statement

Just to explain better what happened,

. . . POST YOUR CODE. otherwise, you're just wasting everyone's time.

AWOL:
. . . POST YOUR CODE. otherwise, you're just wasting everyone's time.

HEY HEY HEY ! What's this rude reply ? Are You a Moderator or an Imperator ?

If You try to understand better my post You will notice that we'we just resolved our problem.
I wrote my post just to let Arduino community know, You included, that maybe there is something that did not work correctly in new IDE 1.6.4.

These type of replys let us say "why should we do that ? Let them with their certainties (? is this therm correct ? I'm not sure... :slight_smile: from Google translator) and go ahead with your work"

BUT... because we LIKE this community, WE waste our time, and put toghether 2 example of code that show the problem... They are exactly the same, unless the position of the SETUP routine.
We do our best to check that the codes differs only in SETUP position, now it's up to You to find differences... :smiley: or, Better... To understand Why...

Just another thing... We've noticed during these tries, that when the compiler fails to compile, the error lines showed by compiler report are wrong, about 10 lines more than the correct one to where the error should refer.

I attach the 2 codes because with code </> tags reply becomes too long...

showOK.ino (8.85 KB)

showBAD.ino (8.91 KB)

Read this, particularly item 6.

I'm a moderator, it's written there on the left, right under my handle.

...mmm,

I've noticed, making my last reply, that the sizes of the 2 codes differs (about 50 bytes) ??

Windows did not tell you about that because normally it only says " size 9Kb", only if You go into files properties You see the exact file size.

Unfortunately it's not possible to make a File Compare because files are different...

We are now looking again to find differences but we could'nt see any up to now...

Why is it so hard to just past your code? Just copy, past and add the code tags...

septillion:
Why is it so hard to just past your code? Just copy, past and add the code tags...

"I attach the 2 codes because with code </> tags reply becomes too long..."

I just answer to this... If You try to copy, paste & add </>, the reply becomes longer than 9000 chars, maximum allowed...

. . . but you're quite happy to make us download over 230kB of image?

The program that will not compile stripped of comments

#define DEBUG_FILES                 // abilita visualizzazione info files 
#define NUM_LEDS                  20  // up to 100 RGB leds, or 300 leds!
#define BUFFER_LEN        NUM_LEDS*3  //  3 bytes x ogni led
#define ESP_RATE                9600  // use this via SoftwareSerial when Debug ...
#define SERIAL_RATE             9600  // use this with normal operations ...
#define FASTLED_PIN                6  // ...
#define SD_CS                     10  // chip select for SD card
#define CICLI                   1000  // numero d cicli (da moltiplicare x volte) per i files senza contenuto
#define TX_PIN                     9  // TX RX pins for esp8266 via Softwareserial
#define RX_PIN                     8
#define WAIT_FILE_TIME         20000  // waiting for file time
#define RXBUFFER_FILE     "RXBUFFER"  // Received data coming from esp8266
#define CAS2INC                    5  // speed of casual changing colors in CASDUE
#define SHIFTINC                  20  // speed of casual changing colors in SHIFT

#ifdef DEBUG_FILES
  #define DEBUG
#endif
#ifdef  DEBUG_CASUALE
  #define DEBUG
#endif
#ifdef  DEBUG_CASDUE
  #define DEBUG
#endif
#ifdef  DEBUG_FUOCO
  #define DEBUG
#endif
#ifdef  DEBUG_SHIFT
  #define DEBUG
#endif
#ifdef  DEBUG_ESP
  #define DEBUG
#endif

#ifdef  DEBUG_ESP
#include <SoftwareSerial.h>
SoftwareSerial esp8266(RX_PIN,TX_PIN);  // RX pin Arduino -> TX pin ESP, TX Arduino -> RX ESP
#endif

#include <FastLED.h>                    // FastLed library for RGB led WS2812B
CRGB leds[NUM_LEDS];                    // array che contiene i valori dei led RGB

#include <SdFat.h>                      // funzioni x gestire SD card, newer SdFat library used
#include <SPI.h>  
SdFat         sd;
File          effettidir;               // contiene la directory "effett18"
File          fileffetti;               // files della directory "effett18"
File          rxFile;                   // RXbuffer for esp8266 incoming data

#include <stdlib.h>

enum {SDCARD, EFFETTI, NODIR, EMPTY, NOFILES, BUFFER, UP, DOWN};  // error codes & others

byte   fileok=0;                // 0=nofiles;  1=nessun file valido;  2=ok
char   nomefile[13]="effetti";  // nome del file nella directory "effetti", usato anche x 'exists' workaround...
char   *nPtr=&nomefile[0];      // nPtr punta al primo char d nomefile
char   extfile[]="   ";         // estensione del nome del file
byte   volte, Nvolte;           // quante volte, e counter, deve essere ripetuto un effetto ("nomefile.XYY")
byte   ritardo;                 // delay tra le righe visualizzate
byte   Rosso[4];                // valori utilizzati per effetti casuali
byte   Verde[4];
byte   Blu[4];
byte   nLed;                    // contatore LED
word   nCy;                     // contatore CICLI
char   c;
unsigned long RXpos;

void setup() { 
  pinMode(10, OUTPUT);                                     // pin 10 MUST be set as output for SD, even if not used
  if (!sd.begin(SD_CS, SPI_HALF_SPEED)) {                  // check if SD present. If not STOP
     err(SDCARD);                  
  }
  if (!sd.exists(nomefile)) {                              // workaround for exist(F("effetti")) not working...
     err(EFFETTI);
  } else {
     effettidir = sd.open(nomefile);                       // workaround for open(F("effetti")) not working...
     if (!effettidir.isDirectory()) {
        err(NODIR);
     }
  }
  rxFile=sd.open(RXBUFFER_FILE, FILE_WRITE);               // check RXbuffer file. If not STOP
  if (!rxFile) { 
     err(BUFFER); 
  } else {
     rxFile.close();
  }
  FastLED.addLeds<NEOPIXEL, FASTLED_PIN>(leds, NUM_LEDS);  // FastLed library initialize...
#ifdef  DEBUG_ESP
  esp8266.begin(ESP_RATE);
#endif   
#ifdef DEBUG
  Serial.begin(SERIAL_RATE);
  Serial.println(F("FontanaWeb 2.0 (c) Andrea Molteni 2015 :)"));  // Serial wellcome...
#endif 

void loop() { 

}
boolean RXfind(String toFind) { 
 
}

unsigned int ParseEsp() { 
  
} 

void ImpostaInc () { 
 
}
void Show (unsigned int rit) { 
 
}
void err(const byte errtype) { 
 
}

Hi UKHeliBob, and thank You for Your reply :slight_smile: ,

but what did You mean ?

I've take a copy of Your code and tried to compile but result still remain the same...

but what did You mean ?

I think he meant "you could have done that, but chose to post images and attachments".
That's just my interpretation.

Sorry AWOL, but I can't understand Your replies,

What's the problem here ? It's a problem for You to download 230Kb of images ? What sort of old ADSL did You have ? Your portable device cannot store temporarily Mbytes of data ? I don't think so... Such devices are no more on sale since, I think, 10 years...
Or Your trouble is that Arduino.cc cannot store such BIG images ??
While, in the meanwhile, Youtube for example, stores GB of data x second ??
So... What's the real problem ??

My intention is to show here that maybe something did not work correctly in IDE, or only in mine. I'm not an expert, so I can't understand what's wrong, and that's why I'm writing here...

Read this, particularly item 6.

AWOL:
6. Getting help on the forum

There are quite a few experienced people on the forum anxious to help you, and help you get as much as you can out of your Arduino. You can help them do that by making helpful posts:

Make an informative subject description, not "help me, I'm a noob", nor something in all capitals. Try to avoid saying "urgent". That's your problem, not ours.
Describe your problem in detail.
If it relates to an electronics part (chip or board), give the exact part number and preferably a link to the data sheet.
Describe how you have connected things like switches. Are they wired to ground? Or +5V? Are there pull-up or pull-down resistors? Post a circuit if there is doubt.
Post your complete sketch (program code)! If you don't you waste time while people ask you to do that.
When you post your code put it between ... tags. You can do that by hitting the </> button above the posting area.
If you get an error, post the error (copy and paste). Not just "I got an error".
With coding problems, if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code.
If you have debugging information in your sketch, post your debugging displays.
Describe what you expected to happen, and what actually happened. Not just "it doesn't work".
If possible, describe what you are really trying to do, not what you think might work. For example "I am trying to turn on an aquarium heater and pump at the same time", not "how do I break out of an interrupt?".
Don't double-post (cross-post). Your question will be noticed. If you post it in multiple places you will just annoy people who might otherwise have answered.

Just the first one is wrong... "Getting help on the forum" I've just solved my problem...

Where is the point : "can I be helpful to someone here in the forum" ?

but what did You mean ?

As AWOL guessed I meant that by stripping out the large comment blocks I was able to fit the code into a post here, which you could have done. With the code in code tags it is easy to copy/paste it into the IDE, compile it and see what happens. Apart from removing the comment blocks I made no changes so did not expect it to compile. In any case I do not have all the libraries that it uses.