Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Using a PIR with LE...
 
Share:
Notifications
Clear all

[Solved] Using a PIR with LED Effects

8 Posts
2 Users
0 Reactions
2,221 Views
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  

This is a continuation of the questions Sébastien had under the LED Effects article;

...
#define BUTTON 2
byte selectedEffect=0;

int pirPin = 4; //PIR Out pin
int pirStatus = 0; // PIR Status

void setup()
{
  pinMode(pirPin, INPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  pinMode(2,INPUT_PULLUP);  // internal pull-up resistor
  attachInterrupt (digitalPinToInterrupt (BUTTON), changeEffect, CHANGE); // pressed
}
//  REPLACE FROM HERE 
void loop() { 
  pirStatus = digitalRead(pirPin);
  if(pirStatus == HIGH) {
  EEPROM.get(0,selectedEffect); 
 if(selectedEffect>18) { 
    selectedEffect=0;
    EEPROM.put(0,0); 
    }

switch(selectedEffect) {

...

case 18 : {
                // meteorRain - Color (red, green, blue), meteor size, trail decay, random trail decay (true/false), speed delay 
                meteorRain(0xff,0xff,0xff,10, 64, true, 30);
                break;
              }
  }
  }
  else
  {
   strip.clear();
   strip.show();
     }
}

 


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  

From Sébastien's post;

 

I tried the add the code above using the FastLED library and all the effect with the button, but I had some error messages like “changeEffect’ was not declared in this scope”, but it was working before I added the PIR code… 

You said that FastLED was better than Adafruit_Neopixel, so I would like to have the Fast_LED working, but I found a way to add the PIR in the Neopixel code and it seems to work.

(Code moved to this forum post)

At least with this method, it seems to works even if I don’t really know what I did. 

I will still dig to make the FastLED working.

Just another question, how can I manage the time that the LEDs are ON? I have two types of PIR sensor, one with the delay and sensitivity screws like this (but to find the right time is quite annoying) and one without those screws like this but it stays ON only some seconds. I tried to add a delay(), like when I had only one LED, but now the delay was acting on the effect itself (like turning one LED ON every 10 seconds, which is not making the fire effect as cool as before).


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  

Hi Sébastien!

Posted by: @hans

error messages like “changeEffect’ was not declared in this scope”

This error quite often indicates that you have either a big chunk of code missing, or unbalanced accolades.
This can make a piece of code become invisible or in the wrong scope (missing a "}" would include this in the wrong function).

The unbalanced accolades can be easiest found by marking what the end-accolade ( } ) belongs to, something like the code below (example marked blue).
The unbalanced accolades can also be detected when formatting code, this is why I often place the opening accolade ( { ) on a new line, so that the closing accolade will be at a visually similar position (example marked red). 

void loop() { 
  pirStatus = digitalRead(pirPin);
  if(pirStatus == HIGH) 
{
EEPROM.get(0,selectedEffect); if(selectedEffect>18)
{ selectedEffect=0; EEPROM.put(0,0); } // if selected switch(selectedEffect)
{ ... case 18 : { // meteorRain - Color (red, green, blue), meteor size, trail decay, random trail decay (true/false), speed delay meteorRain(0xff,0xff,0xff,10, 64, true, 30); break; } // case 18 } // switch
} // if pirStatus else { strip.clear(); strip.show(); } // else if pirStatus }

From what I can see, the void loop() code seems OK. However since not all the code is listed here, it may still contain an accolade issue. 

What could also be the case, is that the code after void loop() is missing. See if the changeEffect function exists in your code:

void changeEffect() {
  if (digitalRead (BUTTON) == HIGH) {
    selectedEffect++;
    EEPROM.put(0, selectedEffect);
    asm volatile ("  jmp 0");
  }
}

 Feel free to post the full code here (ideally as an attachment since the forum seems to screw up code segments that have a "<" in it, placing a space after all "<" symbols will fix this).


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  

Coming to your other question;

Posted by: @hans

Just another question, how can I manage the time that the LEDs are ON? I have two types of PIR sensor, one with the delay and sensitivity screws like this (but to find the right time is quite annoying) and one without those screws like this but it stays ON only some seconds. I tried to add a delay(), like when I had only one LED, but now the delay was acting on the effect itself (like turning one LED ON every 10 seconds, which is not making the fire effect as cool as before).

Using delay is probably not the best option as it "locks" the code for that timeframe, and triggering an interrupt would break that as well.

I would actually use a method where we keep the effect going for a certain time after the PIR went to LOW.
So we set a variable to the time, each time when the PIR was last HIGH.
If the current time minus the stored time is less than x seconds, we keep the effect going.

Caution: when using interrupts the way I did in the LED Effects, the "reset" may actually reset the millis() value as well!
Unfortunately, an Arduino does not have a real time clock (RTC) build in. Just a timer that starts "ticking" when the Arduino starts.

Something like this:

#define PIRdelay 10000 // milliseconds, so 10000 = 10 seconds
unsigned long PIRtriggerTime = 0;

...

void loop() {
  pirStatus = digitalRead(pirPin);
  if( pirStatus == HIGH) 
{
    PIRtriggerTime = millis();
  }

if( millis() - PIRtriggerTime < PIRdelay)
{
    EEPROM.get(0,selectedEffect);
    if(selectedEffect>18)
    {  
      selectedEffect=0;
      EEPROM.put(0,0);
    } // if selected

switch(selectedEffect)
{
...

So, whenever the PIR is HIGH, we set PIRtriggerTime to the current millis value (runtime since Arduino started).
If the current time, minus the time the PIR was last triggered, is smaller than the desired delay (PIRdelay) then show the effect.
Else (Current Time - PIR last triggered > desired delay) show a black LED strip. 


   
ReplyQuote
(@deadman45)
New Member
Joined: 4 years ago
Posts: 2
 

@hans

Hi,

 

Thank you, it works fine now. I think it was an accolade issue. I did not figure out which one exactly, but it is ok now.

And the time code above is working fine too.

 

Now I "just" have to build the frame of the mirror...

 

Again thanks a lot for everything. You save my project. 👍 

 

Sébastien

This post was modified 4 years ago by deadman45

   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  
Posted by: @deadman45

it is ok now

Awesome!! 

Glad it worked out!
Please feel free to post your project code/pictures/video when done (if you'd like to) 😊 


   
ReplyQuote
(@deadman45)
New Member
Joined: 4 years ago
Posts: 2
 

@hans

Hello Hans,

 

Finally I am done with this smart infinite mirror project. Here is the final result.

 

The 2 PIR sensors are hidden in the eyes of the skull.

 

And here is the effect in video.

Smart Infinite Mirror

I have two Arduino Nanos, one for the LEDs of the infinite mirror and one for the LEDs on the sides of the frame, and I added also two tactile buttons to change the effects on the LEDs. The two Arduinos are connected to the same PIR sensor, the other PIR sensor is connected to the Raspberry Pi.

I let all the effect, but the Halloween Eyes effect which was not giving something good.

 

Again, thanks a lot for your help. 👍

 

Sébastien


   
ReplyQuote
 Hans
(@hans)
Famed Member Admin
Joined: 11 years ago
Posts: 2728
Topic starter  

Hi Sébastien!

Oh wow, that looks really good! Well done! (and: thank you for sharing the pictures of your project). 👍 

Glad I could be of some help 😊 


   
ReplyQuote
Share: