LED's motion sensor

The code above, what happens is, when motion is detected, it sends it into a loop, it says "MOTION DETECTED" 2 times, turns on the lights, then say "NO MOTION DETECTED" 2 times, and turns them off, this continues on and on

Sometimes, it works fine, but it's like, if I stand still, it will work and turn off, but then when I move it just starts flashing. I think it's because of the delay in the sensor, I'm not too sure. How can I get it so that if a person moves their arm once, it turns on until that person has removed their arm and it stops detecting motion.

You should post code by using code-tags
There is an automatic function for doing this inthe Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy to forum"
  3. paste clipboard into write-window of a posting

It is not clear to me what you describe.

Almost every motion-sensors has a delay at least of a few seconds while the signal-output is HIGH
then falls back to LOW.

So can you please describe much more precise and in much more detail what you actually see and how it differs from what you want to achive

What does flashing mean to times blinking like you coded it? or something else?

best regards Stefan

You were asked to read the forum guidelines in reply #6 of this thread:

Now would be a good time.

Hello Stefan,

Thanks for your reply, apologies about the issues with my message, I’m quite new to the forum.

Here is the code:

// Motion sensor led's programmed by myself \\
// Version: BETA \\
// Creation date: 26/12/2020 \\
// Info: N/A \\


#include <FastLED.h>


#define LED_PIN 5
#define NUM_LEDS 150
#define BRIGHTNESS 0 // MAXIMUM IS 255 -- default is 65
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];


bool activated = "";


int ledPin = 13; // LED
int pirPin = 7; // PIR Out pin
int pirStat = 0; // PIR status


void setup() {
  //delay(3000); // power-up safety delay -- Uneeded as of now


  FastLED.addLeds < LED_TYPE, LED_PIN, COLOR_ORDER > (leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(0);
  FastLED.show();


  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
  Serial.begin(9600);


  delay(500); // ORIGINAL IS 1000


}


void loop() {
  pirStat = digitalRead(pirPin);
  if (pirStat == HIGH) { // if motion detected
    activated = true;
    Serial.println("Motion Detected");
    Serial.println(activated);
    delay(500);
  } else {
    if (pirStat == LOW) { // if no motion detected
      activated = false;
      Serial.println("No motion detected");
      Serial.println(activated);
      delay(500);
    }
  }


  if (activated == true) {
    FastLED.setBrightness(65);
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(255, 192, 203); // 50, 255, 20
    }
    FastLED.show();
  } else {
    if (activated == false) {
      FastLED.setBrightness(0);
      FastLED.show();
    }
  }
}

So, my main goal is as follows:

The Motion Sensor (specific model: HC-SR501) is set to a distance of about 3 metres and a delay of 3 seconds. My goal is to turn on the LED strip, when the value is read as true, this value is set to true when the motion detector detects “HIGH”, the value is set to false when the motion sensor is set to “LOW”. These values are then handled separately, below in the code, it checks if the value is True, then it sets the lights on, and if it is false, it turns the lights on.

My current issue right now is the lights blink, they come on for about 1 second, then turn off for one second again. I don’t know if the motion sensor is reading something wrong but where I get it to print, it says “MOTION DETECTED” followed by the value true, it prints this 2 times then says “NO MOTION DETECTED” followed by the value false, this loop continues leading the lights to blink on and off.

The flashing is the lights going on and off, the goal is to just have it so that if I hover my hand over the sensor, the lights turn on until the sensor outputs as LOW (no motion detected) then the lights turn off.

Apologies for the misunderstanding, completely my fault.
Best regards, Unseen

Hello, aarg.

Sorry for the issues with my previous message, I never realised the last post had gotten another message on it. I have taken your advice and read through the link posted (this topic) and have made sure to make the necessary changes. Apologies for my previous errors.

Kind Regards,
Unseen

WTH?

bool activated = "";

Have you tested the LED logic separately from the PIR by just toggling the input pin with a jumper?

Here is your loop() function with the redundancy removed to make it easier to follow:

void loop() {
  pirStat = digitalRead(pirPin);
  if (pirStat == HIGH) { // if motion detected
    activated = true;
    Serial.println("Motion Detected");
  } else {
    activated = false;
    Serial.println("No motion detected");
  }
  Serial.println(activated);
  delay(500);

  if (activated == true) {
    FastLED.setBrightness(65);
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CRGB(255, 192, 203); // 50, 255, 20
    }
  } else {
    FastLED.setBrightness(0);
  }
  FastLED.show();
}

Hello,

bool activated = "";

The reason I had this is because I wasn't sure if there is any other Boolean value you could have instead of true, or false. In other programming languages there can be like null or nil, but I don't know if they have that in c++. I was wondering how could I set it to nothing so that it gets altered true or false by the code below instead of having true or false set as the value.

I wasn't sure if there is any other Boolean value you could have instead of true, or false

There isn't

I was wondering how could I set it to nothing so that it gets altered true or false by the code below instead of having true or false set as the value.

If you are going to change the value what does it matter what its current value is ?

Surely a boolean is either true or false by definition. If you don't care what its value is then don't test it

Did it occur to you to look it up? There are quite a few online C++ references. If you're wondering about something, finding out is infinitely preferable to guessing.

I think, FastLED.setBrightness() only sets a scaling factor for set functions like fill_solid(). If you populate the buffer directly as you did, it won't have any effect. It doesn't control the strip brightness directly, either. AFAIK, I could be wrong.

Anyway, you should remove the PIR from the equation for testing.

Also, how are you powering the strip? Have you run some simple LED test sketches to ensure that the strip is adequately powered and fully working?

The strip gets powered by a 5V power supply, as said in the requirements, I used a External Power supply to give the led's 5 volts, in order to run correctly.