Can some one please help me I don't know much about coding I'm very new to it an I'm trying to make a PIR sensor blink a led 3 times when the motion sensor is triggered
Welcome to ArduinoLand....
Best tutorial I've seen for PIR is this one at adafruit. I think it's a good tutorial, because it covers the idea of detecting "new" motion as opposed to "existing" motion.
You'll see that code turns on a LED whenever the PIR is active, ie whenever there is motion. But if you look closely you'll see it only prints the message "Motion detected" when the PIR has only just activated (as distinct from being still activated since the last time through loop()).
So have a look at that and think about where and how to do your blink.... hint: you want to blink on "new" motion not "existing" motion.
thank you very much that code was helpful and the hint as well this is what i got
/*
- PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
but i can only make it blink 4 to 6 time i thought putting
digitalWrite(ledPin, HIGH); // turn LED ON
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
3 time would work but didn't only putting it once made it blink close to what i wanted can i get another hint please or did i even get the first hint correct?
wait i got the first hint but i can't figure out how to make the last blink not stay on so long meaning the first 2 are on off on off an the last if on for like 5 seconds then off
/*
- PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
Hey well done on getting that hint that you need to be a bit lower in the code where the "motion detected" message was.... that's exactly what I was getting at.
I think the new problem is that the existing code uses this line:
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (pirState == LOW) {
.... to put the LED on as long as the PIR is active, so it stays on after you're done blinking. The PIR probably takes that 5s or so to reset wheb motion has stopped. Probably just zap that line out?
Couple of other things:
-
Have a look at for loops to see how to compress repeated lines
-
Look at BlinkWithoutDelay for future use
-
Select your code and hit the </> icon above the
to put code in a box when you post.
/*
* PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
//This line was deleted and moved further down--------------------****
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}