Multi-tab function not declared in scope

This question is coming from the angle of human readability over compiler readability

Wish this was just a spelling error, I have functions in separate .ino files that get declared as "out of scope" when I try to use them.
Putting them in the main .ino file in before setup and loop makes them work fine.

Putting them before setup and loop is all well and good if I just wanted to get things to work, but I also want my code to be organized in a way that makes it more readable. Putting ancillary functions before the meat and potatoes would seem to be a confusing story to me.

Have been trying to figure out what make these functions so different that they escape scope into outerspace when other functions are working fine. Has anyone run into this before?

I have functions in separate .ino files that get declared as "out of scope" when I try to use them.

Try to use them where?

Zip up your folder with the tabs in it that show the error and attache it, using the additional options triangle at the bottom of the reply box.

When I use the "pressEvent()" function in the main loop

surprisingly the functions that depend on it in the same file fail to throw errors

bluefruitTesting.zip (6.3 KB)

Sorry this one is out of my skill set.
The function pressEvent has something wrong with it that I can not find. If you remove that from the if statement then it does not choke on mesPrint() which is defined in the other tabs.

Maybe one of the software experts can look at this?

#line 1 "bluefruitTesting.ino"
//bluefruitTesting

#include "Arduino.h"
void setup();
void loop();
void blueUp();
void mesPrint(char input[]);
void bluePrint(char input);
void printabletest();
void prTest2();
void abstratedTest();
void nonprinting();
void altkeystest();
void iphonekeyboard();
void rawkeytest();
void rawmousetest();
void mouseCommand(uint8_t buttons, uint8_t x, uint8_t y);
void buttonUp();
void holdForButton();
byte buttonInterupt();
void expectSerial();
void upSerial();
void endLine();
void prtHeader(char header[]);
void printHeading(float thisHeading);
void printXyz(int eventx, int eventy, int eventz);
void space();
void printRange(int maxV, int minV, int res);
void prSensorInfo(char nam[], int ver, int id);
void uT();
void rad();
void ms2();
#line 3

The prototype isn't showing up in the auto-generated *.cpp file. Why, I don't know.It might be that the Arduino IDE doesn't like the default parameter in the function definition. When I make this change:

byte pressEvent()
{
  pressEvent( LOW );
}

byte pressEvent(boolean type)
{//checks for a debounced button press event
  static unsigned long time = millis();
  static boolean timingState = false;
  ...
}

And split it into two functions with different parameter lists, it compiles just fine. The behavior should be identical too.

I'm starting to really hate that "helpful" auto-prototyper.

Ok, got it,

Sketch uses 4,646 bytes (14%) of program storage space. Maximum is 32,256 bytes.

Only use 1 .ino, treat the rest of the files as valid C++ ( header file with prototypes, add libraries to sketch as well, include other headers ), and use .cpp instead of .ino ( except main sketch )

bluefruitTesting.zip (5.86 KB)

I needed to add:-

  void space();

to main.h to get it to compile.

1 Like

Ah, yes. I did not save the IDE files before packing up the zip.

Another crappy IDE quirk, I prefer to have an IDE autosave on compile.

Yeah, it definitely has to do with the default function parameter.

This sketch doesn't compile: (test not declared in this scope)

void setup()
{
  Serial.begin(19200);
}

void loop()
{
  Serial.println(test() );
  delay(100);
}

unsigned char test( unsigned char a = 1 )
{
  return a;
}

But this one does:

void setup()
{
  Serial.begin(19200);
}

void loop()
{
  Serial.println(test() );
  delay(100);
}

unsigned char test()
{
  return test(1);
}

unsigned char test( unsigned char a )
{
  return a;
}

Yes, default params can only be on declarations or on a function without a prototype, so just specify your own prototype to avoid a generated one.

Are you still using multiple .ino files or did you see the changes I made in the attached zip above.

@pYro_56, @jiggy-Ninja, Thank you! I actually just learned about default parameters because of basic programing ignorance. Was having this issue in another program in progress. Checking again I see the same thing was going on. Had a hard time drawing the correlation before because it was working in the original program I used it in. Difference was in the other program it was only used through other functions in the same file, avoiding the scope error.

@pyRo_65 I did see the changes compile for me with void space() added. Why do only some functions need to be specified in main.h as opposed to all of them?

Reinforces the idea that I should just learn how to make my own libraries. After all the reason I separate into different files is develop so sort of modularity between my programs. 'pressEvent()' would benefit from being part of a class so multiple buttons could be defined as separate objects anyhow.

I apologize for the amount of redundant code in these files, as one might gather, its cut and paste from other projects being worked on. To avoid reinventing the wheel every time something needs to be tested.

Still kinda lost on the "why" of this issue, but I'm glad to understand "what" the issue is.

also curious why
#include <SoftwareSerial.h>

needs to be in the bluefruitTesting.ino as well as in bluecom.cpp?

looks like it will need to be done this way with any libraries to be added ?

zip your files up and post them as they currently are. Can't answer your question without a current sketch to go on.

As things are coded now the original problem would be non-existent. At least visibly, because of surePress();.

Notice if expectSerial(); is uncommented it throws a scope error. Seems to me that any function that is used needs to be specified in main.h right?
Which is fine with me, wanted to create a "function index" for readability anyway.

Still curious why #include <SoftwareSerial.h> needs to be in two places, If it has to be in bluefruitTesting.ino then blueUp(); is completely redundant. In fact blueUp(); only currently exist to remove it from the main .ino file.

bluefruitTesting2.zip (5.08 KB)

Because the compiler is linear ( top to bottom only ). If the function you use is declared after the code you are using or in a different file, you need a prototype to allow the compiler to know the missing functions.

Not all are in main.h because they are above the code that uses them.