Not Declared in Scope

Can’t get rid of the error for “printArray” and “i” being “not declared in scope”. This is occurring in my Void Setup(), and Void Loop() sections “for loop”. I have this type of code working in two other programs but I must have an error that I can not see in this one. I have spent many hours trying to find out what the problem is. The program is using an ultrasonic sensor to detect the presence of an object using median data to filter outlier data points.

EEPROM_Dry_Fire_EWS_Median.ino (6.17 KB)

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

Also, did you know that there is a ‘sort’ in the C standard library (already installed)?

First: post the complete error message, so we don’t have to compile your code again!

printArray is defined after the calling lines, so you have to define it’s prototype before using it:

void printArray(int *, int);
void isort(int *, int);

Put that code part in front of the setup() definition and the first error is gone.

The i variable is out of scope because this line is not correct:

for(int i = 0; i < arraysize; i++); //array pointers go from 0 to 4

The for loop ends exactly at the semicolon, the following code block is executed only once and the definition of “i” isn’t inside that block. Hint: to fix it you just have to remove that semicolon.

Next error:

   digitalWrite(calPin, LOW);}  // when calPin is LOW ('0' state)  
}

The curly brace on the first line of that excerpt is wrong. If you remove it, the code at least compiles.

Thank you for all of your help Shannon. It works now!! :smiley: I will send code in the future how you suggested. I was not aware of the Sort function already in the library. Is there an index of functions in the Library I can use in the future? As you probably figured out, I am just a newbie with the Arduino Nano.

MillerInsMark:
Thank you for all of your help Shannon. It works now!! :smiley: I will send code in the future how you suggested. I was not aware of the Sort function already in the library. Is there an index of functions in the Library I can use in the future? As you probably figured out, I am just a newbie with the Arduino Nano.

Arduino uses AVR GCC:
https://www.nongnu.org/avr-libc/user-manual/modules.html
…and other compilers for non-AVR processors. ‘qsort’ is in <stdlib.h> which is included by default no matter which compiler is used, as far as I know.

pylon:
printArray is defined after the calling lines, so you have to define it's prototype before using it:

void printArray(int *, int);

void isort(int *, int);




Put that code part in front of the setup() definition and the first error is gone.

The Arduino IDE automatically generates function prototypes, so that's not necessary. The prototype generation was broken by the extra closing brace in loop(). Once that's fixed, the sketch will compile without the prototypes.