Hello everyone! The goal of my project was to make a working PIR sensor blink an LED light when it senses something. I don't exactly know what is wrong with my code. I have tried two ways, if you comment, please comment by first telling me which code you are looking at. Thank you.
CODE ONE
int ledPin = 11; // LED
int inputPin = 2; // input pin for the PIR sensor
int pirState = LOW; // we start assuming no motion is detected
int val = 0; // variable for reading the status
void setup() {
pinMode(ledPin, OUTPUT); // the LED is output
pinMode(inputPin, INPUT); // the sensor is 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
Serial.println("Motion Detected!");
else {
digitalWrite(ledPin, LOW); // turn LED OFF
(pirState = LOW);
Serial.println("Motion is gone!");
}
}
CODE TWO
// This is an introduction to all the variables
int ledPin = 11; // LED
int pirPin = 2; // input pin for the PIR sensor
int pirStat = LOW; // we start assuming no motion is detected
int pirState = 0; // variable for reading the status, also the value
void setup() {
pinMode(ledPin, OUTPUT); // the LED is output
pinMode(pirPin, INPUT); // the sensor is input
}
void loop(){
pirState = digitalRead(pirPin); // read input value
if (pirState == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirStat == HIGH){ // if the PIR state is high, it switches on the LED and then turns low again
digitalWrite(ledPin, HIGH);
pirStat = LOW;
}
}
}
Thanks everyone!!
P.S. Can someone tell me what is and Analog to digital converter and how to use it in code?
Hello everyone! The goal of my project was to make a working PIR sensor blink an LED light when it senses something. I am going to use this regarding the six feet rule. I don't exactly know what is wrong with my code. I have tried two ways, if you comment, please comment by first telling me which code you are looking at. Thank you. (by the way, notice the difference between pirStat and pirState)
CODE ONE
int ledPin = 11; // LED
int inputPin = 2; // input pin for the PIR sensor
int pirState = LOW; // we start assuming no motion is detected
int val = 0; // variable for reading the status
void setup() {
pinMode(ledPin, OUTPUT); // the LED is output
pinMode(inputPin, INPUT); // the sensor is 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
Serial.println("Motion Detected!");
else {
digitalWrite(ledPin, LOW); // turn LED OFF
(pirState = LOW);
Serial.println("Motion is gone!");
}
}
CODE TWO
// This is an introduction to all the variables
int ledPin = 11; // LED
int pirPin = 2; // input pin for the PIR sensor
int pirStat = LOW; // we start assuming no motion is detected
int pirState = 0; // variable for reading the status, also the value
void setup() {
pinMode(ledPin, OUTPUT); // the LED is output
pinMode(pirPin, INPUT); // the sensor is input
}
void loop(){
pirState = digitalRead(pirPin); // read input value
if (pirState == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirStat == HIGH){ // if the PIR state is high, it switches on the LED and then turns low again
digitalWrite(ledPin, HIGH);
pirStat = LOW;
}
}
}
Thanks everyone!!
P.S. Can someone tell me what is and Analog to digital converter and how to use it in code?
what does cross post mean?
Duplicate topics merged
Why did you start a second topic on the same subject, particularly as it was in a completely inappropriate section of the forum ?
Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting will result in a timeout from the forum.
In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
adharshnaren987654321forumuno:
what does cross post mean?
It means posting the same question multiple times in different parts of the forum.
This wastes time.
Don't do it.
I am so sorry, didn't realize that happened. I must have made a mistake! Thanks for letting me know, it won't happen again.
adharshnaren987654321forumuno:
Hello everyone! The goal of my project was to make a working PIR sensor blink an LED light when it senses something. I don't exactly know what is wrong with my code. I have tried two ways, if you comment, please comment by first telling me which code you are looking at. Thank you.
CODE ONE
int ledPin = 11; // LED
int inputPin = 2; // input pin for the PIR sensor
int pirState = LOW; // we start assuming no motion is detected
int val = 0; // variable for reading the status
void setup() {
pinMode(ledPin, OUTPUT); // the LED is output
pinMode(inputPin, INPUT); // the sensor is 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
Serial.println("Motion Detected!");
else {
digitalWrite(ledPin, LOW); // turn LED OFF
(pirState = LOW);
Serial.println("Motion is gone!");
}
}
CODE TWO
// This is an introduction to all the variables
int ledPin = 11; // LED
int pirPin = 2; // input pin for the PIR sensor
int pirStat = LOW; // we start assuming no motion is detected
int pirState = 0; // variable for reading the status, also the value
void setup() {
pinMode(ledPin, OUTPUT); // the LED is output
pinMode(pirPin, INPUT); // the sensor is input
}
void loop(){
pirState = digitalRead(pirPin); // read input value
if (pirState == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirStat == HIGH){ // if the PIR state is high, it switches on the LED and then turns low again
digitalWrite(ledPin, HIGH);
pirStat = LOW;
}
}
}
Thanks everyone!!
P.S. Can someone tell me what is and Analog to digital converter and how to use it in code?
An analog to digital converter returns a value from 0 to 1023 when it compares the value from your PIR circuit to a standard ( usually the 5 volt value you are powering the Arduino with.
But, before we go any further, you need to tell us what you are expecting when you write "when it senses something"? The PIR will always be sensing something and will you will always get some value between 0 and 1023. What actually are you looking for?
Paul