Hi clever people
Im doing an Halloween project where I need item to be lit up with a (fast) flashing led, like strobelight, activated by a PIR sensor.
I have a code for PIR to power on led (code 1) and a code for the led to flash fast (code 2)
But I cant merge them so when the pir detects motion, the led flashes fast as code 2 for xx-seconds.
Can some of you help with that?
It a Nano by the way.
Thanks
Code 1:
int led = 2; // LED pin
int sensor = 9; // PIR sensor pin
int state = LOW; // by default, no motion detected
int val = 0; // variable for sensor value
void setup() {
pinMode(led, OUTPUT); // set led pin as output
pinMode(sensor, INPUT); // set sensor pin as input
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) {
digitalWrite(led, HIGH); // turn LED ON
delay(5000); // delay in milliseconds
if (state == LOW) {
state = HIGH;
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(1000); // delay in milliseconds
if (state == HIGH){
state = LOW;
}
}
}```
--------------------------------------------------------------------
Code 2:
int led = 2;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // time to wait
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(75); // time to wait
Check out the Blink Without Delay example in the IDE (File->examples->02.digital->Blink Without Delay) to learn how to get rid of those delay() functions and track elapsed time without blocking your code.
Then, every time through loop(), if your PIR is active, reset your startTime and begin flashing. If PIR is not active and elasped time is greater than your threshhold, stop flashing.
I found this code and modified it to my needs - The LED flashes about 15 times when Nano is powered on, but after that not at motion.
When opening "serial monitor" the led flashes 3 times fast, and it shows "motion detected", but the LED does not flash after that, but it says motion detected.
Can you guide me in som direction?
Led + is to nano 13
Led - is to gnd at second pin (same row as led +)
PIR + to nano 5v
PIR - to nano gnd at fourth oin (same row as D2-12)
PIR signal is to D5
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 200;
int PIR_output=5; //OUTPUT OF PIR SENSOR
int led=13; //LED PIN
void setup(){
pinMode(PIR_output, INPUT); //SETTING PIT OUTPUT AS ARDUINO INPUT
pinMode(led, OUTPUT); //SETTING LED AS OUTPUT
Serial.begin(9600); //serial commm to pc
}
void loop() {
if(digitalRead(5) == HIGH) //READING DATA FROM PIR
{
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
//SAVE THE LAST TIME YOU BLINKED LED
previousMillis =currentMillis;
// IF THE LED IS OFF TURN ON AND VICE VERSA
if (ledState ==LOW)
ledState ==HIGH;
else
ledState = LOW;
// SET THE LED WITH THE LEDSTATE OF THE VARIABLE
; digitalWrite(led, ledState);
}
Serial.println("motion detected");
}
else {
digitalWrite(13,LOW); //SETTING LED TO LOW
Serial.println("scanning");
}
}
You should get into the habit of auto formatting your code (Ctrl-T) so all your braces line up, etc. It makes the flow of the code easier to read.
This
if (ledState == LOW)
ledState == HIGH;
is two comparison operations. == is comarison, = is assignment
im so new to this, so I dont understand what you are saying
But, I figured it out with this code (i merged two codes).
My final (i think) question, is:
Now its only blinking for the time adjusted on the PIR sensor - can I put in a line, so I in th e code can control how long it should be activated for? Its pretty hard to adjust the PIR to 7 seconds on the screw.
int led = 2; // LED pin
int sensor = 9; // PIR sensor pin
int state = LOW; // by default, no motion detected
int val = 0; // variable for sensor value
void setup() {
pinMode(led, OUTPUT); // set led pin as output
pinMode(sensor, INPUT); // set sensor pin as input
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(75); // wait for a second
if (state == LOW) {
state = HIGH;
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(1000); // delay in milliseconds
if (state == HIGH){
state = LOW;
}
}
}
You will have to keep track of elapsed time. Look at the Blink Without Delay example in the IDE (File->examples->02.digital->blink without delay)
Something like this will blink once triggered and continue to blink for 3 seconds after it is no longer triggered.
const int led = 2; // LED pin
const int sensor = 9; // PIR sensor pin
unsigned long startTime, periodTime;
const unsigned long blinkDuration = 3000; // total time to blink after PIR triggered
const unsigned long blinkPeriod = 100; // blink time
void setup() {
pinMode(led, OUTPUT); // set led pin as output
pinMode(sensor, INPUT); // set sensor pin as input
}
void loop() {
int val = digitalRead(sensor); // read sensor value
if (val == HIGH) {
// PIR triggered so start or keep blinking
startTime = millis();
}
if ( millis() - startTime >= blinkDuration ) {
// time to stop
digitalWrite(led, LOW);
startTime = 0; // indicate no more blinking
}
if ( startTime && millis() - periodTime >= blinkPeriod ) {
// time to toggle led
digitalWrite(led, !digitalRead(led));
}
}