First project help.

Ok, I'm new to arduino, for my first project(the reason i got into arduino) was to have a Parallax PIR tell the arduino 328 to send a sequence of pulses(you will see in code) to a 5v reed relay that opens and closes a switch on a little camera i pulled out of a DVR pen. I have an led that indicates when the board is pulsing and triggers the relay from pin 13. PIR is in digital 3 and was working with a regular "blink when movement test".

I would like if upon movement the switch was to be triggered in sequence to turn on(3sec), switch to video mode(2sec), start recording(.25sec) for a few minutes then if no movement, stop recording(.25sec), then power off(4sec).

With the modded code i was able to get the PIR to warm up and calibrate, then it automatically runs the sequence (without motion) then upon completion the board/PIR send random pulses which screws everything up when i want it to turn off and wait for more movement.

I got this code from ladyada.com, chopping it up and butchering it. I realize yall cant do it for me but a few pointers would be great even if its pointing out some good sources for me to learn the code.

kinda a big roject for a beginner ::slight_smile:

/////////////////////////////
//VARS            
                   
int calibrationTime = 30;        //the time we give the sensor to calibrate (10-60 secs according to the datasheet)    
long unsigned int lowIn;         //the time when the sensor outputs a low impulse        
long unsigned int pause = 5000;  //the amount of milliseconds the sensor has to be low 
                                 //before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;  

int pirPin = 3;                  //the digital pin connected to the PIR sensor's output
int ledPin = 13;


/////////////////////////////
//SETUP
void setup(){
  Serial.begin(9600);
  pinMode(pirPin, INPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(pirPin, LOW);
                                                           
  Serial.print("calibrating sensor ");              //give the sensor some time to calibrate
    for(int i = 0; i < calibrationTime; i++){
      Serial.print(".");
      delay(1000);
      }
    Serial.println(" done");
    Serial.println("SENSOR ACTIVE");
    delay(50);
    digitalWrite(ledPin, HIGH);
    delay(1000);
    digitalWrite(ledPin, LOW);
  }

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

     if(digitalRead(pirPin) == HIGH){            //recording sequence
       digitalWrite(ledPin, HIGH);
       delay(3000);  //On
       digitalWrite(ledPin, LOW);
       delay(500);
       digitalWrite(ledPin, HIGH);
       delay(2000);  //video mode
       digitalWrite(ledPin, LOW);
       delay(500);
       digitalWrite(ledPin, HIGH);
       delay(250);  //start recording
       digitalWrite(ledPin, LOW);
       delay(120000); //record
       digitalWrite(pirPin, LOW); 
              
       if(lockLow){  
         
         lockLow = false;                    //makes sure we wait for a transition to LOW before any further output is made:
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }

     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, HIGH);            //the led visualizes the sensors output pin state
       delay(250); //stop record
       digitalWrite(ledPin, LOW);
       delay(500);
       digitalWrite(ledPin, HIGH);
       delay(4000); //off
       digitalWrite(ledPin,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(!lockLow && millis() - lowIn > pause){  //if the sensor is low for more than the given pause, 
                                                  //we assume that no more motion is going to happen               
           lockLow = true;                        //makes sure this block of code is only executed again after a new motion sequence has been detected  
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }

Thanks for putting up with my crap :-/

What you've described is called a "state machine".

For your project, you may have states like these...

  Idle - Arduino is waiting for motion
  TurningOn - motion was detected; turn on the camera (3s)
  SwitchToVideo - change the camera to video mode (2s)
  StartRecording - turn on the recorder (1/4s)
  Recording - leave the camera recording (2m)
  MovementCheck - continue recording until no motion is detected
  StopRecording - turn off the recorder (1/4s)
  PowerOff - turn the power off (4s)

For each state, you define the conditions necessary to transition to other states. For example, if the state machine is in the Idle state and motion is detected, a transition to TurningOn occurs.

Using Google, you can find many implementations of state machines. I believe there is even some stuff in the Playground. This is a very high-level description...

You could read up on grafcet or sequence programming on plc 's
Basicly you have a bunch of flip flop elements in a row with a conditional transition to the flip flop. If the next flip flop gets active it resets the previous.

If you'd like: Arduino Playground - FiniteStateMachine Library