Count number of servo / PIR activations via blinking led

Hello Gang,
My first post and first project using the Arduino Uno R3.
My simple project consists of a servo and a PIR IMS detector. My sketch and project are working just fine.

Now comes my dilemma. I would like to count the number of times the servo/PIR has been activated via a flashing LED. One flash for once, two for two activations and so on.

After numerous searches I am unable to find the additional sketch I need mostly due to my lack of knowledge on what the function would be referred to as.

Any help for an old newb that knows barely enough to be dangerous would be greatly appreciated.

Schematic.jpg

You must post your code if you expect to get help.

I'm not clear whether you want the Arduino to cause the led to flash or count how many times it flashes. If the latter, what will cause the LED to flash?

...R

Robin2:
You must post your code if you expect to get help.

I'm not clear whether you want the Arduino to cause the led to flash or count how many times it flashes. If the latter, what will cause the LED to flash?

...R

Thanks for getting back to me Robin...
I would like the Arduino to flash an LED for every time the PIR activates the servo via the UnoR3.
One blink for one activation two blinks for two activations etc...Hope that helps and Thanks!

Sketch

/* This code sweeps a servo from 0 degrees to 180 when the PIR sensor detects motion.
    

 #include <Servo.h>

 Servo myservo;  //creates a servo object
                          //a maximum of eight servo objects can be created

 int pos = 0;        //variable to store servo position

 //amount of time we give the sensor to calibrate(10-60 secs according to the datasheet)

 int calibrationTime = 10;

 //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 = 12;            //digital pin connected to the PIR's output
 int pirPos = 13;           //connects to the PIR's 5V pin

 void setup(){
   myservo.attach(4);    //attaches servo to pin 4
   Serial.begin(9600);    //begins serial communication
   pinMode(pirPin, INPUT);
   pinMode(pirPos, OUTPUT);
   digitalWrite(pirPos, HIGH);

   //give the sensor time to calibrate
   Serial.println("calibrating sensor ");
   for(int i = 0; i < calibrationTime; i++){
     Serial.print(calibrationTime - i);
     Serial.print("-");
     delay(1000);
   }
   Serial.println();
   Serial.println("done");
  
   //while making this Instructable, I had some issues with the PIR's output
   //going HIGH immediately after calibrating
   //this waits until the PIR's output is low before ending setup
   while (digitalRead(pirPin) == HIGH) {
     delay(500);
     Serial.print(".");     
   }
   Serial.print("SENSOR ACTIVE");
 }

 void loop(){

   if(digitalRead(pirPin) == HIGH){  //if the PIR output is HIGH, turn servo

     /*turns servo from 0 to 180 degrees and back
     it does this by increasing the variable "pos" by 1 every 5 milliseconds until it hits 180
     and setting the servo's position in degrees to "pos" every 5 milliseconds
     it then does it in reverse to have it go back
     to learn more about this, google "for loops"
     to change the amount of degrees the servo turns, change the number 180 to the number of degrees you want it to turn
     **/
     for(pos = 0; pos < 180; pos += 1)  //goes from 0 to 180 degrees
     {                                                 //in steps of one degree
       myservo.write(pos);                   //tells servo to go to position in variable "pos"
       delay(2);                                   //waits for the servo to reach the position
     }
     for(pos = 180; pos>=1; pos-=1)    //goes from 180 to 0 degrees
     {                               
       myservo.write(pos);                  //to make the servo go faster, decrease the time in delays for
       delay(0);                                  //to make it go slower, increase the number.
     }
    
     if(lockLow){ 
       //makes sure we wait for a transition to LOW before 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){      

     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 can assume the motion has stopped
     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);
     }
   }
 }

Not clear to me what you expect of the flashing led....

You mean just flash it once every time the servo is activated?- so that if by the end of eternity it has been activated 9 times, the led will have flashed 9 times too?

Or, that if you look at the system you will tell at a glance that it's activated say 5 times becasue the led will be flashing in bursts of 5?

Btw, your code will be

much
easier
  {
     to read
  }

... if you select it and hit the scroll icon just above the :neutral_face: smiley.

BuzzGold:
I would like the Arduino to flash an LED for every time the PIR activates the servo via the UnoR3.
One blink for one activation two blinks for two activations etc...Hope that helps and Thanks!

You have a program line

if(digitalRead(pirPin) == HIGH){

. If you create a function that makes your LED blink you can just call it immediately after that line and before the rest of the code in that IF section.

 if(digitalRead(pirPin) == HIGH){ 
    blinkLed();

The code to blink the LED could be like this

void blinkLed() {
   if (ledOn == false) {
       digitalWrite(ledPin, HIGH);
       startLedMillis = millis();
       ledOn = true;
   }
   else {
      if (millis() - startLedMillis >= blinlIntervalMillis) {
           digitalWrite(ledPin, LOW);
           ledOn = false;
      }
   }
}

And please post your code in code tags so we can read it easily.

...R

JimboZA:
Not clear to me what you expect of the flashing led....

Or, that if you look at the system you will tell at a glance that it's activated say 5 times becasue the led will be flashing in bursts of 5?

Yes 5 flashes = 5 activations

Thanks guys and sorry about the sketch...