# pH Sensor Code Issue

Hey everyone!

So we purchased a pH sensor for our classroom and have finished coding it but keep getting the following error.

AVERAGE_ARRAY was not declared in this scope.

This is my code! HELP!

#define SENSOR A0
#define OFFSET 0.00
#define LED 13
#define SAMPLING_INTERVAL 20
#define PRINT INTERVAL 800
#define ARRAY_LENGTH 40 //times of collection
int PH_ARRAY[ARRAY_LENGTH]; //Store the average value of the sensor feedback
int PH_ARRAY_INDEX=0;
void setup(void)
{
pinMode(LED,OUTPUT); Serial.begin(9600);
Serial.println(“PH SENSOR KIT VOLTAGE TEST!”); //Test the serial monitor
}
void loop(void)
{
static unsigned long SAMPLING_TIME= millis();
static unsigned long PRINT_TIME = millis();
static float VOLTAGE;
if(millis()-SAMPLING_TIME> SAMPLING_INTERVAL)
{
if(PH_ARRAY_INDEX==ARRAY_LENGTH)PH_ARRAY_INDEX=0;
VOLTAGE = AVERAGE_ARRAY(PH_ARRAY,ARRAY_LENGTH)5.0/1024;
SAMPLING_TIME=millis();
}
if(millis() - PRINT_TIME > PRINT_INTERVAL) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print("VOLATGE OUTPUT: ");
Serial.println(VOLTAGE,2);
PRINT_TIME=millis();
}
}
double AVERAGE_ARRAY(int
ARR, int NUMBER){
inti;
int max.min;
double AVG;
long AMOUNT=0;
if(NUMBER<=0){
Serial.println{“ERROR!/n”);
return 0;
}
if(NUMBER<S){ //less than 5, calculated directly statistics
for(i=0;i<NUMBER;i++){
AMOUNT+=ARR*;*

• }*
• AVG= AMOUNT/NUMBER;*
• return AVG;*
• }else{*
• if(ARR[0]<ARR[1]){*
• min= ARR[0];max=ARR[1];*
• }*
• else{*
• min=ARR[1];max=ARR[0];*
• }*
• for(i=2;i<NUMBER;i++){*
_ if(ARR*<min ){_
_
AMOUNT+=min; //arr<min*_
_ min=ARR*;
else{
if(ARR>max){
AMOUNT+=max; //arr>max*

max= AMOUNT+=ARR*;
}else{
AMOUNT+=ARR; //min<=arr<=max*

* }
}//if*

* }//for*
* AVG= (double)AMOUNT/(NUMBER-2);
}//if*

* return AVG;
}*_

It’s called PH_ARRAY, I think.

Edit;scratch that.

Don’t use CAPITALS for function or variable names.
Capitals should be reserved for constants and macros.

Please remember to use code tags when posting code.

`digitalWrite(LED,digitalRead{LED)^1)` Oops

@ctowermpa

Your topic was Moved to it’s current location / section as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.

ctowermpa:
This is my code! HELP!

if you are going to share code, PLEASE make is as easy as possible for other to understand.

you code was full or errors!

I’ve tidied it up and it compiles now.

(compiles, not tested!)

``````#define SENSOR A0
#define OFFSET 0.00
#define LED 13
#define SAMPLING_INTERVAL 20
#define PRINT_INTERVAL 800
#define ARRAY_LENGTH 40  //times of collection
int PH_ARRAY[ARRAY_LENGTH]; //Store the average value of the sensor feedback
int PH_ARRAY_INDEX = 0;

void setup(void)
{
pinMode(LED, OUTPUT); Serial.begin(9600);
Serial.println("PH SENSOR KIT VOLTAGE TEST!");  //Test the serial monitor
}
void loop(void)
{
static unsigned long SAMPLING_TIME = millis();
static unsigned long PRINT_TIME = millis();
static float VOLTAGE;
if (millis() - SAMPLING_TIME > SAMPLING_INTERVAL)
{
if (PH_ARRAY_INDEX == ARRAY_LENGTH)PH_ARRAY_INDEX = 0;
VOLTAGE = AVERAGE_ARRAY(PH_ARRAY, ARRAY_LENGTH) * 5.0 / 1024;
SAMPLING_TIME = millis();
}

if (millis() - PRINT_TIME > PRINT_INTERVAL)//Every 800 milliseconds, print a numerical, convert the state of the LED indicator
{
Serial.print("VOLATGE OUTPUT: ");
Serial.println(VOLTAGE, 2);
PRINT_TIME = millis();
}

}

double AVERAGE_ARRAY(int* ARR, int NUMBER) {
int max, min;
double AVG;
long AMOUNT = 0;

if (NUMBER <= 0) {
Serial.println("ERROR!/n");
return 0;
}

if (NUMBER < 5) { //less than 5, calculated directly statistics
for (int i = 0; i < NUMBER; i++) {
AMOUNT += ARR[i];
}
AVG = AMOUNT / NUMBER;
return AVG;
}
else {
if (ARR[0] < ARR[1]) {
min = ARR[0];
max = ARR[1];
}
else {
min = ARR[1];
max = ARR[0];

for (int i = 2; i < NUMBER; i++) {
if (ARR[i] < min ) {
AMOUNT += min; //arr<min
min = ARR[i];
}
else {
if (ARR[i] > max) {
AMOUNT += max; //arr>max
max =  AMOUNT + ARR[i];
}
else {
AMOUNT += ARR[i]; //min<=arr<=max
}
}//for

AVG = (double)AMOUNT / (NUMBER - 2);
}
}//if
}
return AVG;
}
``````

hope that helps…

Your function named AVERAGE_ARRAY did not exist because it failed to compile.

• In two places, you used '{' where you should have used '('.
• You defined "PRINT" as "INTERVAL 800" instead of defining "PRINT_INTERVAL" as 800.
• There should be a ',', not a '.' between max and min in the declaration.
• You spelled '5' as 'S' in an 'if' statement. The name 'S' is not declared.
• Before you can put in the "else" you have to put the closing '}' of the '{' in the 'if' statement.

Once all of those were fixed I was able to compile your sketch.

Thank you everyone so much!

I'm still in the process of learning Arduino!

ctowermpa: Thank you everyone so much!

I'm still in the process of learning Arduino!

The language is C/C++. You can find references and tutorials all over the Internet.