PIR Sensor Triggering a long event

Hi! This is my first post here and I have to say I’m loving my Arduino.

I wrote a cool little code that controls lights and servos and that all works perfectly fine in a regular loop. But the problem comes when I try to trigger it with a PIR sensor. It will only trigger it once which is very frustrating.

I want the code to start every time the motion sensor is triggered, Run until the code is finished then wait to be triggered again.

Every thing seems to be good with the PIR. Everything looks good when I check it out in the serial monitor and the jumper is in the H position.

Please help

I cut out the working part of my code because it is kinda long.

#include <Servo.h>


int aPin = 2;
int bPin = 3;
int cPin = 4;
int dPin = 9;
int ePin = 13;
int fPin = 5;
int gPin = 7;
int hPin = 8;
int iPin = 12;
int jPin = 6;

int motionPin = 10; //where the motion sensor is hooked up
int motionState = LOW; // start assuming there is no motion
int val = 0; // variable for reading motion pin status



int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

Servo myServo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 


void setup() {
 
  pinMode(aPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  pinMode(cPin, OUTPUT);
  pinMode(dPin, OUTPUT);
  pinMode(ePin, OUTPUT);
  pinMode(fPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(hPin, OUTPUT);
  pinMode(iPin, OUTPUT);
  pinMode(jPin, OUTPUT);
  
  myServo.attach(11);  // attaches the servo on pin 11 to the servo object 
  
  pinMode(motionPin, INPUT); // declairing motion sensor as input

}

void loop() {
  
  val = digitalRead(motionPin); //read motion value
  if (val == HIGH) {
  
    
    
    //Begin Working code******************************************************
    
  

//End of Working code***************************

if (motionState ==LOW) {


  motionState = HIGH;
  
  }
} else {
  digitalWrite(aPin,LOW);  /* Basically this line will cause no visible change
                            I just thought I needed to have some thing for it 
                            to do if it's low
                            */
   
   delay(500);
   
  if(motionState == HIGH){
    motionState = LOW;
 

  }
}
}

Thanks

But the problem comes when I try to trigger it with a PIR sensor. It will only trigger it once which is very frustrating.

So, you snipped out all the code that is involved in deciding the value of motionState, crappily indented what was left, and want us to guess what the problem is. Not today, I'm afraid.

I had a good laugh seeing this part

    //Begin Working code******************************************************
    
  

//End of Working code***************************

especially because nothing was in between, you made my day :slight_smile:

Paul is right it is difficult to see if you don't post the whole code.

That said, I'll give you a framework for the loop that should get you started. The variable motionstate will only change on transitions, and the if condition that checks both the new value and the current state prevents that the code is executed twice for the same event.

Give it a try !

void loop() 
{
  val = digitalRead(motionPin); //read motion value
  if (val == HIGH && motionstate == LOW) // new motion detect
  {
    motionState = HIGH; //prevent doing it more than once on this signal
    // do your motion detect things here


  } 
  else 
  {
    motionState = LOW;
    // do other things here


  }
}

robtillaart, Thanks for the help I’ll give that a try.

PaulS, Thanks for the sarcasm. :expressionless:

If you guys really think its necessary I’ll post my full code.

Be warned I’m new so this all probably could have been done much simpler.

#include <Servo.h>


int aPin = 2;
int bPin = 3;
int cPin = 4;
int dPin = 9;
int ePin = 13;
int fPin = 5;
int gPin = 7;
int hPin = 8;
int iPin = 12;
int jPin = 6;

int motionPin = 10; //where the motion sensor is hooked up
int motionState = LOW; // start assuming there is no motion
int val = 0; // variable for reading motion pin status



int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

Servo myServo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 










void setup() {
 
  pinMode(aPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  pinMode(cPin, OUTPUT);
  pinMode(dPin, OUTPUT);
  pinMode(ePin, OUTPUT);
  pinMode(fPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(hPin, OUTPUT);
  pinMode(iPin, OUTPUT);
  pinMode(jPin, OUTPUT);
  
  myServo.attach(11);  // attaches the servo on pin 11 to the servo object 
  
  pinMode(motionPin, INPUT); // declairing motion sensor as input

}

void loop() {
  
  val = digitalRead(motionPin); //read motion value
  if (val == HIGH) {
  
    
    
  //Begin Working code******************************************************
    
  //turn servo
  
  for(pos = 0; pos < 90; pos += 5)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
  
    
  // RING LOOP
  
  digitalWrite(aPin, LOW);
  digitalWrite(bPin, LOW);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(500);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, LOW);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, HIGH);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, HIGH);
  digitalWrite(iPin, HIGH);
  digitalWrite(jPin, LOW);
  delay(300);
  
  digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, HIGH);
  digitalWrite(iPin, HIGH);
  digitalWrite(jPin, LOW);
  delay(500);
  
  
// LED FADE TO MAX

analogWrite(10, brightness);    

  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
    analogWrite(jPin, fadeValue);
    delay(30);
}

delay(2000);


//all blink-----------------------------------------------------------------


  digitalWrite(aPin, LOW);
  digitalWrite(bPin, LOW);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(50);


digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, HIGH);
  digitalWrite(iPin, HIGH);
  digitalWrite(jPin, HIGH);
  delay(50);
  

digitalWrite(aPin, LOW);
  digitalWrite(bPin, LOW);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(50);


digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, HIGH);
  digitalWrite(iPin, HIGH);
  digitalWrite(jPin, HIGH);
  delay(50);
  
digitalWrite(aPin, LOW);
  digitalWrite(bPin, LOW);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, LOW);
  delay(50);



digitalWrite(aPin, HIGH);
  digitalWrite(bPin, HIGH);
  digitalWrite(cPin, HIGH);
  digitalWrite(dPin, HIGH);
  digitalWrite(ePin, HIGH);
  digitalWrite(fPin, HIGH);
  digitalWrite(gPin, HIGH);
  digitalWrite(hPin, HIGH);
  digitalWrite(iPin, HIGH);
  digitalWrite(jPin, HIGH);
  tone(10, 500, 200);
  delay(50);
  
digitalWrite(aPin, LOW);
  digitalWrite(bPin, LOW);
  digitalWrite(cPin, LOW);
  digitalWrite(dPin, LOW);
  digitalWrite(ePin, LOW);
  digitalWrite(fPin, LOW);
  digitalWrite(gPin, LOW);
  digitalWrite(hPin, LOW);
  digitalWrite(iPin, LOW);
  digitalWrite(jPin, HIGH);
  delay(50);
 
  //FADE BLUE LED OUT*******************************
  
  
  analogWrite(10, brightness);    

  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=3) {
    analogWrite(jPin, fadeValue);
    delay(30);
}
delay(200);
  
  //TURN SERVO BACK DOWN SLOWLY*****************************
  
  
  
   for(pos = 90; pos>=1; pos-=1)     // goes from 90 degrees to 0 degrees 
  {                                
    myServo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);
    
  


  }
  
  
  delay(500);

//End of Working code***************************

if (motionState ==LOW) {


  motionState = HIGH;
  
  }
} else {
  digitalWrite(aPin,LOW);  /* Basically this line will cause no visable change
                            I just thought I needed to have some thing for it 
                            to do if it's low
                            */
   myServo.write (0);
   delay(500);
   
  if(motionState == HIGH){
    motionState = LOW;
 

  }
}
}

You really need to read about arrays and for loops.

  analogWrite(10, brightness);

You're writing to a pin you've declared an input.

You're writing to a pin you've declared an input.

Thanks so much for catching that! I had to switch some pins around and forgot to change that (and in a few other spots) :blush:
Its working just how I wanted it to now. :open_mouth:

You really need to read about arrays and for loops.

Yeahhh I figured something like that would help slim my code down. Thanks for the tip.

I had to switch some pins around

That's where arrays will help enormously.