Losing value for integer

I am fairly new to creating Arduino sketches, so would appreciate some help. The part of the sketch in question is the following:

if (AREF>AMIN && PS==LOW && En==LOW){
Serial.print("Anode 1=");
Serial.print(AMPR1);
Serial.println("     ");
Serial.print("Anode 2=");
Serial.print(AMPR2);
Serial.println("     ");
the same series of prints is repeated several more times.
PS++;
}

The intention with this sketch was to get the print to occur to the serial monitor once using the integer PS to limit the passes through the if statement; however, the print repeats over and over. I inserted Serial.print(PS) in several places in the code and found that the value for PS incremented and was seen as 1 at the end of the if statement and at the end of the void loop(), but at the beginning of the void loop() the value for PS returned to 0. I removed any code that might reset the value for PS to 0 without any effect - still continues to serial print continuously. Any ideas?

Please look at this...

the same series of prints is repeated several more times.
}

Didn't the compiler barf on that?

Where is PS declared? Inside loop() or as a global? If you had posted your entire sketch, we would know and be better able to help

In reply to blh64, the following is the entire sketch:
loat AMPS1 = 0; //Current calculated from measured value.
float AMPS2 = 0;
float AMPS3 = 0;
float AMPS4 = 0;
float AMPS5 = 0;
float AMPS6 = 0;
float AMPS7 = 0;
float AMPS8 = 0;
float AMPS9 = 0;
float AMPS10 = 0;
float AMPS11 = 0;
float AMPS12 = 0;
float AMPS13 = 0;
float AMPS14 = 0;
float AMPS15 = 0;
float AMPS16 = 0;

int AMPR1 = 0; //Current rounded to nearest whole number.
int AMPR2 = 0;
int AMPR3 = 0;
int AMPR4 = 0;
int AMPR5 = 0;
int AMPR6 = 0;
int AMPR7 = 0;
int AMPR8 = 0;
int AMPR9 = 0;
int AMPR10 = 0;
int AMPR11 = 0;
int AMPR12 = 0;
int AMPR13 = 0;
int AMPR14 = 0;
int AMPR15 = 0;
int AMPR16 = 0;

const int A = 50; //Current sensor range value. (0-50 amps)
const int AMIN = 1023/A;
int PS;

int PC;

void setup() {
//Program//
//The reference anode must be defined for each system as the anode current value which is used to determine the point of peak current and the period of data acquisition.
//The reference anode in this program is Anode 4.

// put your setup code here, to run once:
Serial.begin(9600);

int AREF = 0; //Defines the anode current measurement that will be used as the refeence current. Anode current must be greater than to continue data acquisition.
int En = 0; //Calls for data acquisition.
int AnodeI = 0; //Pre-acquistion data to initialize data acquisition above reference anode current of 1 amp.
int PS = 0; //Value to ensure 1 print per rectifier cycle.

pinMode (A0, INPUT);
pinMode (A1, INPUT);
pinMode (A2, INPUT);
pinMode (A3, INPUT);
pinMode (A4, INPUT);
pinMode (A5, INPUT);
pinMode (A6, INPUT);
pinMode (A7, INPUT);
pinMode (A8, INPUT);
pinMode (A9, INPUT);
pinMode (A10, INPUT);
pinMode (A11, INPUT);
pinMode (A12, INPUT);
pinMode (A13, INPUT);
pinMode (A14, INPUT);
pinMode (A15, INPUT);
}

//BEGINNING OF LOOP//
void loop() {
// put your main code here, to run repeatedly:
Serial.print("Again PS=");
Serial.println(PS);
int StpPt; //Causes print to monitor only once per cycle.
int AREF;
int En;
int AnodeI;
int PS;
int Anode1; //Anode(x) contains the value for the anode current.
int Anode2;
int Anode3;
int Anode4; //Provides an initial reference anode current value equal to the equivalent of ~1 amp, which will permit data acquisition (> AREF).
int Anode5;
int Anode6;
int Anode7;
int Anode8;
int Anode9;
int Anode10;
int Anode11;
int Anode12;
int Anode13;
int Anode14;
int Anode15;
int Anode16;

if (En == 0)
{
int AnodeI = analogRead (A3); //AnodeI is only used to initialize the reading of anode currents. Once the reference anode current exceeds 1 amp, AnodeI is no longer used for the remainder of the cycle.
AREF = 25;
};

//If reference anode current value is above ~1 amp, initialize anolog read of all anodes by setting Enable=1.//
if (AnodeI > AMIN)
{
En = 1;
PS = 0; //Place "PS" reset here.
}
else
{
En = 0;
};
delay(5000);
//Data acquistion occurs if Enable is high and the reference anode current (in this case anode 4) continues to rise.//
if (AREF < Anode4 && En == 1){

//AREF = Anode4
delay(750); //0.75 second delay between analog reads.
int Anode1 = analogRead (A0); //Anode(x) contains the 0-1023 value for the anode current.
int Anode2 = analogRead (A1);
int Anode3 = analogRead (A2);
int Anode4 = analogRead (A3);
int Anode5 = analogRead (A4);
int Anode6 = analogRead (A5);
int Anode7 = analogRead (A6);
int Anode8 = analogRead (A7);
int Anode9 = analogRead (A8);
int Anode10 = analogRead (A9);
int Anode11 = analogRead (A10);
int Anode12 = analogRead (A11);
int Anode13 = analogRead (A12);
int Anode14 = analogRead (A13);
int Anode15 = analogRead (A14);
int Anode16 = analogRead (A15);
};

Serial.print("AREF=");
Serial.println(AREF);
Serial.print("Anode4=");
Serial.println(Anode4);
Serial.print("AMIN=");
Serial.println(AMIN);

if (AREF>AMIN && PS==0 && En==LOW){

float AMPS1 = Anode1 * (A/1023); //Changes measured value (0-1023) to current (0-50 amps).
float AMPS2 = Anode2 * (A/1023);
float AMPS3 = Anode3 * (A/1023);
float AMPS4 = Anode4 * (A/1023);
float AMPS5 = Anode5 * (A/1023);
float AMPS6 = Anode6 * (A/1023);
float AMPS7 = Anode7 * (A/1023);
float AMPS8 = Anode8 * (A/1023);
float AMPS9 = Anode9 * (A/1023);
float AMPS10 = Anode10 * (A/1023);
float AMPS11 = Anode11 * (A/1023);
float AMPS12 = Anode12 * (A/1023);
float AMPS13 = Anode13 * (A/1023);
float AMPS14 = Anode14 * (A/1023);
float AMPS15 = Anode15 * (A/1023);
float AMPS16 = Anode16 * (A/1023);
int AMPR1 = AMPS1 + .5; //Rounds current to nearest whole number and converts current value to an interger.
int AMPR2 = AMPS2 + .5;
int AMPR3 = AMPS3 + .5;
int AMPR4 = AMPS4 + .5;
int AMPR5 = AMPS5 + .5;
int AMPR6 = AMPS6 + .5;
int AMPR7 = AMPS7 + .5;
int AMPR8 = AMPS8 + .5;
int AMPR9 = AMPS9 + .5;
int AMPR10 = AMPS10 + .5;
int AMPR11 = AMPS11 + .5;
int AMPR12 = AMPS12 + .5;
int AMPR13 = AMPS13 + .5;
int AMPR14 = AMPS14 + .5;
int AMPR15 = AMPS15 + .5;
int AMPR16 = AMPS16 + .5;

Serial.print("Anode 1 = ");
Serial.print(AMPR1);
Serial.print(" ");
Serial.print("Anode 2 = ");
Serial.print(AMPR2);
Serial.print(" ");
Serial.print("Anode 3 = ");
Serial.print(AMPR3);
Serial.print(" ");
Serial.print("Anode 4 = ");
Serial.println(AMPR4);

Serial.print("Anode 5 = ");
Serial.print(AMPR5);
Serial.print(" ");
Serial.print("Anode 6 = ");
Serial.print(AMPR6);
Serial.print(" ");
Serial.print("Anode 7 = ");
Serial.print(AMPR7);
Serial.print(" ");
Serial.print("Anode 8 = ");
Serial.println(AMPR8);

Serial.print("Anode 9 = ");
Serial.print(AMPR9);
Serial.print(" ");
Serial.print("Anode 10 = ");
Serial.print(AMPR10);
Serial.print(" ");
Serial.print("Anode 11 = ");
Serial.print(AMPR11);
Serial.print(" ");
Serial.print("Anode 12 = ");
Serial.println(AMPR12);

Serial.print("Anode 13 = ");
Serial.print(AMPR13);
Serial.print(" ");
Serial.print("Anode 14 = ");
Serial.print(AMPR14);
Serial.print(" ");
Serial.print("Anode 15 = ");
Serial.print(AMPR15);
Serial.print(" ");
Serial.print("Anode 16 = ");
Serial.print(AMPR16);
PS++;

}

Serial.println("En=");
Serial.println(En);
//Once cycle has ended, reset all variables and values, except for reference anode value, which is set to AMIN.//
if(En == LOW)
{

int Anode1 = 0;
int Anode2 = 0;
int Anode3 = 0;
int Anode4 = AMIN; // **AMIN Anode(x) compared to AREF for data acquisition. Anode(x) current initialized to a value representing 1 amp for first pass.
int Anode5 = 0;
int Anode6 = 0;
int Anode7 = 0;
int Anode8 = 0;
int Anode9 = 0;
int Anode10 = 0;
int Anode11 = 0;
int Anode12 = 0;
int Anode13 = 0;
int Anode15 = 0;
int Anode16 = 0;

int AMPR1 = 0; //Current rounded to nearest whole number.
int AMPR2 = 0;
int AMPR3 = 0;
int AMPR4 = 0; //**int AMPR4 = 0
int AMPR5 = 0;
int AMPR6 = 0;
int AMPR7 = 0;
int AMPR8 = 0;
int AMPR9 = 0;
int AMPR10 = 0;
int AMPR11 = 0;
int AMPR12 = 0;
int AMPR13 = 0;
int AMPR14 = 0;
int AMPR15 = 0;
int AMPR16 = 0;

int AnodeI = 0;

int En = 0;

}

Serial.print("PS=");
Serial.println(PS);

}

In reply to Danois90, I will look at your link.

I must add Serial.print(s) of AREF, AMIN, Anode4 and PS have been inserted into the code for troubleshooting.

That isn't going to work, is it?

Too long,too wrong.

Please remember to use code tags when posting code

WOW! :open_mouth: Have you ever heard of arrays? You code could be decimated to almost nothing if you would use arrays..

"PS" is declared inside "loop()" that is why it is reset for each iteration of the loop method. Declare it as global and your problem is solved.

Thank you for the suggestion, I will give that a try.

Danois90
Thanks, your suggestion corrected the problem. I guess I have something to learn about where to declarations. Again, thank you.

You are re-declaring your variables inside of a function so it masks your global declarations.

This is a declaration

int Varaible = 3;

This is the use of that variable
[/code]
Varaible = 45 + 4/2;
[/code]

Do not put the type (e.g. 'int', 'float', etc.) before the variable or you are declaring a new variable. The compiler will keep them separate, but humans will be confused and post questions to Forums.