Variable Reverts Value set within If statement

Hey guys,

I am still a newb to arduino programming so hopefully someone can help me out.

To give a little background, the code triggers a camera and after so many triggers it activates a windshield wash/wipe system.

Here is the code:

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//*User Config Section*//
//Note: All time is set in Milliseconds 
const int pictureDelay = 10000;  //delay between pictures being taken for test code

const int triggerDelay = 0;  //Time between sensor trigger and camera capture

const int washDelay = 250;   //How long wash pump is turned on for

const int washToWipe = 4000; //How long before the wiper is activated after the pump stops

const int wiperDelay = 6000; //How long the wipe is activated for. Note: Wiper takes 2-3 seconds to complete 1 full cycle.

const int triggerCount = 3;  //How many pictures are taken before the wash wipe system activates

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


const int sensorPin = 2;
const int camPin = 4;
const int washPin = 7;
const int wipePin = 8;
int currentCount;

void setup(){
  Serial.begin(9600);                 //start serial connection
  pinMode(sensorPin, INPUT_PULLUP);
  pinMode(camPin, OUTPUT);
  pinMode(washPin, OUTPUT);           
  pinMode(wipePin, OUTPUT);
  digitalWrite(camPin, LOW);
  digitalWrite(washPin, LOW);
  digitalWrite(wipePin, LOW);
  
  currentCount = triggerCount;        //Set current count to user configured trigger count
  
  
}

void loop(){
 
      Serial.print("count at beginning of forever loop = ");
      Serial.println(currentCount);
      
      delay(triggerDelay);                           //User set delay time for camera capture
      
      Serial.print("Sensor Activated");
      Serial.println();
      
      digitalWrite(camPin, HIGH);                    //Triggers Camera
      
      Serial.print("Camera Triggered");
      Serial.println();
      
      delay(500);                                    //Keeps camera trigger on for half a second
      digitalWrite(camPin, LOW);                     //Turns camera trigger off   
      
      Serial.print("count before subrtraction = ");
      Serial.println(currentCount);
      
      currentCount = currentCount - 1;        //Subtract 1 from temp count 
      
      Serial.print("Count after subtractions = ");
      Serial.println(currentCount);
      
     if(currentCount == 0)                              //Once temp count reaches 0 activate wash wipe system
     {
      digitalWrite(washPin, HIGH);                   //Activates Wash Pump
      
      Serial.print("Wash Triggered");
      Serial.println();
      
      delay(washDelay);                              //User set delay for how long wash pump is activated
      digitalWrite(washPin, LOW);                    //Turns wash pump off
      delay(washToWipe);                             //User set delay for time between when the pump stops and when the wiper activates
      digitalWrite(wipePin, HIGH);                   //Activates Wiper
      
      Serial.print("Wiper Trigered");
      Serial.println();
      
      delay(wiperDelay);                             //User set delay for how long wiper is activated
      digitalWrite(wipePin, LOW);                    //Turns wiper off
      
      int currentCount = triggerCount;
      
      Serial.print("Count inside of if statement = ");
      Serial.println(currentCount);
     }
    
   digitalWrite(camPin, LOW);                        //Keeps Camera trigger deactivated when the sensor is not triggered
   digitalWrite(washPin, LOW);                       //Keeps Wash trigger deactivated when the sensor is not triggered
   digitalWrite(wipePin, LOW);                      //Keeps Wiper trigger deactivated when the sensor is not triggered

   Serial.print("Count After if statement = ");
   Serial.println(currentCount);
 
   delay(pictureDelay);
 
}

(note: the original code uses a sensor to activate the camera. This program has most of the sensor code removed but that is the reason for references to sensors in the comments)

The problem I am having is with the "currentCount" variable. "currentCount" is set to "triggerCount" at the beginning of the code. Each time a photo is taken, "currentCount" decreases its number by "1". Once current count reaches "0" it enters the IF statement. At the end of the IF statement, "currentCount" is set back to "triggerCount" and process starts over again.

The problem comes after the if statement. In the current case, after "currentCount" is set to "triggerCount" in the if statement, the value for "currentCount" is "3". For some reason though, as soon as the if statement ends, "currentCount" reverts back to the value "0".

Here is the Serial Monitor Output:

count at beginning of forever loop = 3
Sensor Activated
Camera Triggered
count before subrtraction = 3
Count after subtractions = 2
Count After if statement = 2
count at beginning of forever loop = 2
Sensor Activated
Camera Triggered
count before subrtraction = 2
Count after subtractions = 1
Count After if statement = 1
count at beginning of forever loop = 1
Sensor Activated
Camera Triggered
count before subrtraction = 1
Count after subtractions = 0
Wash Triggered
Wiper Trigered

Count inside of if statement = 3
Count After if statement = 0

count at beginning of forever loop = 0
Sensor Activated
Camera Triggered
count before subrtraction = 0
Count after subtractions = -1
Count After if statement = -1
count at beginning of forever loop = -1
Sensor Activated
Camera Triggered
count before subrtraction = -1
Count after subtractions = -2
Count After if statement = -2

these two messages specifically:

Count inside of if statement = 3
Count After if statement = 0

What am I doing wrong?

Also I am very open to any suggestions on improving the code.

Thanks so much for any help!
-Scott

You are creating new instances of variables named currentCount, remove the 'int' from:

int currentCount = triggerCount;

Otherwise this variable receives the value and dies when the if ends and leaves the global currentCount empty.

Read up on variable scope:
EDIT: Variable Scope in C++

pYro_65 that fixed the issue! Thanks so much for pointing it out, I don't know how I didn't catch it. Also thanks for the tutorial!

No worries, this sort of error appears frequently here.