I want to control LED stripes with PIR

Hello, Im a noobie and have only done easy projects, i have just started with the arduino.

I want to make my LED Strip to turn on whenever the PIR inputs a high value and to run the loop for a certain period of time and then turn them all off until PIR senses movement again and so on.

I have a 12V LED strip and heres the code im using for a rainbow effect, I want to run that loop that makes a rainbow effect for a period of time (lets say 10 minutes) and then turn off until it senses movement again.

Also if you have codes for cool effects for this strip please let me know. Thanks in advance.

#define REDPIN 6
#define GREENPIN 9
#define BLUEPIN 5
 
#define FADESPEED 15     
 
void setup() {
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
 
 
void loop() {
  int r, g, b;
 
  for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 

  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 

  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 

  for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 

  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  // fade from teal to blue
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
   }
}

try this website

Hi diego,

welcome to the forum and congrats for posting the code as code-section.
You have asked a somehow concrete question. You seem to want to learn programming.
So go a little deeper into it. What is your guessing how detecting a HIGH-signal on an IO-Pin looks like as code?

This will make start you think and analyse how the coding is done. Yoour guess doesn't have to work.
The purpose is just make you start thinking and maybe reading in tutorials.

I recommend reading this tutorial

Arduino Programming Course
https://startingelectronics.org/software/arduino/learn-to-program-course/

Arduino Programming Course

It is easy to understand and a good mixture between writing about important concepts
and get you going.

best regards Stefan

StefanL38:
Hi diego,

welcome to the forum and congrats for posting the code as code-section.
You have asked a somehow concrete question. You seem to want to learn programming.
So go a little deeper into it. What is your guessing how detecting a HIGH-signal on an IO-Pin looks like as code?

This will make start you think and analyse how the coding is done. Yoour guess doesn't have to work.
The purpose is just make you start thinking and maybe reading in tutorials.

I recommend reading this tutorial

Arduino Programming Course
Arduino Programming Course Contents

Arduino Programming Course

It is easy to understand and a good mixture between writing about important concepts
and get you going.

best regards Stefan

Thank you so much, I will try both links and check everything that has to do with it, and yes, I do want to learn programming.

I didnt explain this on the post but thing is, I tried using "If" statements before my code, so that I could use PIR value as a condition for a function but somehow it didn't work :frowning: I dont know if the "if" statement doesnt work before "for" statement and that's what i have doubts about, right now, Im trying to set up the code again but this time with an IR receiver and a transmitter (some old dvd controller i found) instead of the PIR so i have the option to switch between modes, Ill try again using the "if" statement for each HEX code received from the IR sensor and i'll post the code here.

benmadi:
try this website
Tweaking4All.com - Arduino - LEDStrip effects for NeoPixel and FastLED

I went to the website but the codes are for LEDs with integrated microcontroller (i dont really know if thats how you call it but data comes from one single pin), what i mean is, I have a 12V LED strip with 3 channels and not one :frowning: so I need to individually control each color value as in single LEDs.

So this is what my code looks like now:

#include <IRremote.h>

#define REDPIN 6
#define GREENPIN 9
#define BLUEPIN 5
 
#define FADESPEED 15     // make this higher to slow down

int togglestate = 0;
int rainbow = 0;
const int RECV_PIN = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup() {
  pinMode(REDPIN, OUTPUT);
  irrecv.enableIRIn();
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
 
 
void loop() {
    int r, g, b;
   if (irrecv.decode(&results)){
    switch(results.value){
      case 0x80FFF807:
      if(togglestate==0){
        analogWrite(REDPIN, 255);
        analogWrite(BLUEPIN, 255);
        analogWrite(GREENPIN, 255);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

        case 0x80FF00FF:
       if(rainbow==0){
        for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
 
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
   }
rainbow=1;
       }
       else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        rainbow=0;
       }
       break; 
    }
    irrecv.resume();
   }
}

I can turn on and off with the first function but for case #2 which is kind of a rainbow effect, it does start when I press the button assigned but the loop stops and does not repear, I tried then changing if on second case for a while but when checking for errors it says "expectec } before else and the } is there, so idk :frowning:

Hi diego,

I copy & pasted your code and it compiled.

There is a reason why parts of the code ar eindented. It makes it much easier to see which lines of code belong to which part that embraces these lines.

You simple press Ctrl-T in the Arduino-IDE to make your code standard indented.

You are using else statements related to the case-statements. I have looked up the reference but it says nothing about else in relation to switch case
So you shouldn't use else statements in this way.
You seem to try to switch off all LEDs in the "else" case
try it with the default-statement
see here switch...case - Arduino Reference

I was wrong teh elses belong to if-statements and are OK

As a general hint. Change one thing at a time and then test again.

With
/* begin of block comment

end of block comment */

you can comment out code temporalily through outcommenting.
Then test the reduced code and if it works add for example only one case-statement.

If you put the opening curled bracktet "{" in the same line as the statement this has an advantage:

If the corresponding closing curled bracket is many lines below and you put the cursor just behind the bracket
in the first line of the IDE-Editor the corresponding line is shown in a yellow popup-field. this makes it much easier to check if the opening and closing brackets are at the right place.

best regards Stefan

Thank you so much guys, I really appreciate it. So at the end i ended up with the LED strip working with a random IR remote and made many color combinations and my code ended up like this:

#include <IRremote.h>

#define REDPIN 6
#define GREENPIN 9
#define BLUEPIN 5
 
#define FADESPEED 15     // make this higher to slow down

int togglestate = 0;
int rainbow = 0;
const int RECV_PIN = 4;

IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup() {
  pinMode(REDPIN, OUTPUT);
  irrecv.enableIRIn();
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);
}
 
 
void loop() {
    int r, g, b;
   if (irrecv.decode(&results)){
    switch(results.value){
      case 0x80FFF807:
      if(togglestate==0){
        analogWrite(REDPIN, 255);
        analogWrite(BLUEPIN, 255);
        analogWrite(GREENPIN, 255);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

case 0x80FF807F:
      if(togglestate==0){
        analogWrite(REDPIN, 255);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

      case 0x80FF40BF:
      if(togglestate==0){
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 255);
        analogWrite(GREENPIN, 0);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

      case 0x80FFC03F:
      if(togglestate==0){
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 255);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

   case 0x80FF20DF:
      if(togglestate==0){
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 100);
        analogWrite(GREENPIN, 255);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

case 0x80FFA05F:
      if(togglestate==0){
        analogWrite(REDPIN, 221);
        analogWrite(BLUEPIN, 160);
        analogWrite(GREENPIN, 221);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;
case 0x80FF609F:
      if(togglestate==0){
        analogWrite(REDPIN, 218);
        analogWrite(BLUEPIN, 112);
        analogWrite(GREENPIN, 214);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

      case 0x80FFE01F:
      if(togglestate==0){
        analogWrite(REDPIN, 255);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 255);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

      case 0x80FF10EF:
      if(togglestate==0){
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 204);
        analogWrite(GREENPIN, 204);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;

      case 0x80FF906F:
      if(togglestate==0){
        analogWrite(REDPIN, 153);
        analogWrite(BLUEPIN, 51);
        analogWrite(GREENPIN, 255);
        togglestate=1;
      }
      else {
        analogWrite(REDPIN, 0);
        analogWrite(BLUEPIN, 0);
        analogWrite(GREENPIN, 0);
        togglestate=0;
      }
      break;
      
        case 0x80FF00FF:
       if(rainbow==0){
        rainbow=1;
       }
       else {
        rainbow=0;
       }
       while(rainbow==1){
        for (r = 0; r < 256; r++) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  for (b = 255; b > 0; b--) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
  for (g = 0; g < 256; g++) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
  } 
for (r = 255; r > 0; r--) { 
    analogWrite(REDPIN, r);
    delay(FADESPEED);
  } 
  
  for (b = 0; b < 256; b++) { 
    analogWrite(BLUEPIN, b);
    delay(FADESPEED);
  } 
 
  for (g = 255; g > 0; g--) { 
    analogWrite(GREENPIN, g);
    delay(FADESPEED);
   }
       } 
       break;
    }
    irrecv.resume();
   }
}

however the rainbow effect I put is on case 0x80FF00FF and i had to put a while so that the rainbow effect repeats over and over, however, everytime i enter into that loop I cant change “rainbow” from 1 back to 0 :frowning: so i cant exit that loop with the press of any buttom, to leave i have to restart the arduino but im thinking of using my recently made apple homekit devide with ESP8266 and use a relay with it so I can turn it on and off with apple’s Siri.