Problem with if statements in program

Hi guys,

I am new to this an have a small problem with my program.
My project is making a trail camera using a Canon 1100D SLR camera. I use arduino UNO, a PIR sensor and a relay shield to wake up the camera, focus and capture.

I manage to do this, but the problems starts when i put a microswitch in the loop. My plan is to make different programs (regarding how many pictures to be taken and how long time between) with the flip of different switches.

With switch one flipped it works just fine, but when i flip switch 2 it only runs once. (only one switch is closed at the time). By running once I mean that it doesnt start again the next time i activate the PIR sensor. I use pull down resistors 10K to ground on both switches.

I used a tutorial on PIR sensors as a starting point.

Hope anyone is generous enough to help me out with my problem.

Best regards,
Marcus

/////////////////////////////
//CANON TRAIL CAMERA
//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;        

//the time when the sensor outputs a low impulse
long unsigned int lowIn;         

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000;  

boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 3;    //the digital pin connected to the PIR sensor's output
int focusPin = 4;    //the digital pin connected to the camera`s focus (Jackplug)
int shutterPin = 5;    //the digital pin connected to the camera`s shutter (Jackplug)
int switch1Pin = 6;    //the digital pin connected to the microswitch pin #1
int switch2Pin = 7;    //the digital pin connected to the microswitch pin #2


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(switch1Pin, INPUT);
  pinMode(switch2Pin, INPUT);
  pinMode(focusPin, OUTPUT);
  pinMode(shutterPin, OUTPUT);
  digitalWrite(pirPin, LOW);


  //give the sensor some time to calibrate
  Serial.print("calibrating sensor ");
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
  }

////////////////////////////
//LOOP
void loop(){

  
    
       
     if(digitalRead(switch1Pin) == HIGH && digitalRead(pirPin) == HIGH){   //Program#1
       digitalWrite(focusPin, HIGH);   
       delay(200);
       digitalWrite(focusPin, LOW);
       delay(1000);
       digitalWrite(focusPin, HIGH);
       delay(1000);
       digitalWrite(shutterPin, HIGH);
       delay(200);
       digitalWrite(focusPin, LOW);
       digitalWrite(shutterPin, LOW);
       delay(15000);
       
       
     if(digitalRead(switch2Pin) == HIGH && digitalRead(pirPin) == HIGH){   //Program#2
       digitalWrite(focusPin, HIGH);   
       delay(200);
       digitalWrite(focusPin, LOW);
       delay(200);
       digitalWrite(focusPin, HIGH);
       delay(200);
       digitalWrite(focusPin, LOW);
       delay(15000);

       

       
  
       
       if(lockLow){  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){
       digitalWrite(focusPin, LOW);       
       digitalWrite(shutterPin, LOW);
      
       
       
       

       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }}}

Your code needs formatting. Aside from that, program#1 encapsulates everything, even program#2. So how can program#2 run if program#1 is not?. Check your closing brackets for program#1, and get rid of one of the closing brackets at the end of the code, you have 4, where you only need 3.

if(digitalRead(switch1Pin) == HIGH && digitalRead(pirPin) == HIGH) //Program#1
{
digitalWrite(focusPin, HIGH);
delay(200);
digitalWrite(focusPin, LOW);
delay(1000);
digitalWrite(focusPin, HIGH);
delay(1000);
digitalWrite(shutterPin, HIGH);
delay(200);
digitalWrite(focusPin, LOW);
digitalWrite(shutterPin, LOW);
delay(15000);
}

Thank you so much!:slight_smile: works like a charm..

Should have tough of that my self, but I didnt so THANK YOU!