PIR sensor to LEDs

I am trying to use a PIR sensor to light up an array of LEDs. I have the LEDs hooked up to 3 different pins. Essentially I would like the LEDs to fade in and out using a code like this

// Output
int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11

// Program variables
int redVal = 255; // Variables to store the values to send to the pins
int greenVal = 1; // Initial values are Red full, Green and Blue off
int blueVal = 1;

int i = 0; // Loop counter
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial

void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
if (DEBUG) { // If we want to see the pin values for debugging…
Serial.begin(9600); // …set up the serial ouput on 0004 style
}
}

// Main program
void loop()
{
i += 1; // Increment counter
if (i < 255) // First phase of fades
{
redVal -= 1; // Red down
greenVal += 1; // Green up
blueVal = 1; // Blue low
}
else if (i < 509) // Second phase of fades
{
redVal = 1; // Red low
greenVal -= 1; // Green down
blueVal += 1; // Blue up
}
else if (i < 763) // Third phase of fades
{
redVal += 1; // Red up
greenVal = 1; // Green low
blueVal -= 1; // Blue down
}
else // Re-set the counter, and start the fades again
{
i = 1;
}

analogWrite(redPin, redVal); // Write current values to LED pins
analogWrite(greenPin, greenVal);
analogWrite(bluePin, blueVal);

if (DEBUG) { // If we want to read the output
DEBUG += 1; // Increment the DEBUG counter
if (DEBUG > 10) // Print every 10 loops
{
DEBUG = 1; // Reset the counter

Serial.print(i); // Serial commands in 0004 style
Serial.print("\t"); // Print a tab
Serial.print(“R:”); // Indicate that output is red value
Serial.print(redVal); // Print red value
Serial.print("\t"); // Print a tab
Serial.print(“G:”); // Repeat for green and blue…
Serial.print(greenVal);
Serial.print("\t");
Serial.print(“B:”);
Serial.println(blueVal); // println, to end with a carriage return
}
}
delay(wait); // Pause for ‘wait’ milliseconds before resuming the loop
}

But I would only like to do so when the PIR sensor senses motion. Like this code…

  • PIR sensor tester
    */

int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as 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
if (pirState == LOW) {
// we have just turned on
Serial.println(“Motion detected!”);
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println(“Motion ended!”);
// We only want to print on the output change, not state
pirState = LOW;
}
}
}

I would like the fade sequence to turn off when motion is absent
I am having a large amount of trouble joining the two codes together…

So, show us what the stitched together code looks like.
(please use the Code (#) button when posting code).
One way of doing it would be to take the stuff in “loop” in your first sketch, put it inside another function, and call that function conditional upon your PIR triggering

Here is my best at stitching the two together-I am getting a large amount of error messages, thanks, I am pretty new to arduino/code

/*
 * PIR sensor tester
 */

int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
int inputPin = 2;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int redVal   = 255; // Variables to store the values to send to the pins
int greenVal = 1;   // Initial values are Red full, Green and Blue off
int blueVal  = 1;

int i = 0;     // Loop counter    
int wait = 50; // 50ms (.05 second) delay; shorten for faster fades
int DEBUG = 0; // DEBUG counter; if set to 1, will write values back via serial


void setup() {
    pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT); 
pinMode(inputPin, INPUT);     // declare sensor as input
  if (DEBUG) {           // If we want to see the pin values for debugging...
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
  i += 1;      // Increment counter
  if (i < 255) // First phase of fades
  {
    redVal   -= 1; // Red down
    greenVal += 1; // Green up
    blueVal   = 1; // Blue low
  }
  else if (i < 509) // Second phase of fades
  {
    redVal    = 1; // Red low
    greenVal -= 1; // Green down
    blueVal  += 1; // Blue up
  } 
  else if (i < 763) // Third phase of fades
  {
    redVal  += 1; // Red up
    greenVal = 1; // Green low
    blueVal -= 1; // Blue down
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  

  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal);  

  if (DEBUG) { // If we want to read the output
    DEBUG += 1;     // Increment the DEBUG counter
    if (DEBUG > 10) // Print every 10 loops
    {
      DEBUG = 1;     // Reset the counter

      Serial.print(i);       // Serial commands in 0004 style
      Serial.print("\t");    // Print a tab
      Serial.print("R:");    // Indicate that output is red value
      Serial.print(redVal);  // Print red value
      Serial.print("\t");    // Print a tab
      Serial.print("G:");    // Repeat for green and blue...
      Serial.print(greenVal);
      Serial.print("\t");    
      Serial.print("B:");    
      Serial.println(blueVal); // println, to end with a carriage return
    }
  }
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}




    if (pirState == LOW) {
      // we have just turned on
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    digitalWrite(redPin, LOW); // turn LED OFF
   digitalWrite(greenPin, LOW);//
   digitalWrite(bluePin, LOW);//

    if (pirState == HIGH){
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

Balancing braces is important. One technique is to use indentation...

void setup() 
{
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);  
  pinMode(bluePin,  OUTPUT);
  pinMode(inputPin, INPUT);     // declare sensor as input
  if (DEBUG)            // If we want to see the pin values for debugging...
  {
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  }

// Uh oh!  Where's the brace to close setup?

setup does not have a closing brace. Add it and the errors should change or disappear.

You need to at least balance the number of opening and closing braces. Cut and paste won't do this for you.

Thanks for the advice, I have switched a few things about the fading code and the PIR code. I have pretty much no experience with nesting loops inside of loops and I am having syntax problems, here is the current code. The conditional “else” at the bottom that reads what the PIR sensor is doing requires an “if” before it- there is an “if” before it but I cannot embed the other loop into the code so that it identifies. here is each loop separate

// Output
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
int whitePin = 5;
int blackPin = 3;

// Program variables
int redVal   = 0; // Variables to store the values to send to the pins
int greenVal = 0;   // Initial values are Red full, Green and Blue off
int blueVal  = 0;
int whiteVal = 10;
int blackVal = 255;

int i = 1;     // Loop counter    
int wait = 100; // 50ms (.05 second) delay; shorten for faster fades


void setup()
{
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT); 
  pinMode(whitePin,  OUTPUT); 
  pinMode(blackPin,  OUTPUT); 
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  }


// Main program
void loop()
{
  i += 1;      // Increment counter
  if (i < 100) // First phase of fades
  {
    redVal   += 1; // Red down
    greenVal  = 0; // Green up
    blueVal   = 0; // Blue low
    whiteVal  = 0;
    blackVal -= 1;
  }
  else if (i < 200) // Second phase of fades
  {
    redVal   -= 1; // Red low
    greenVal += 1; // Green down
    blueVal   = 0; // Blue up
    whiteVal  = 0;
    blackVal  = 0;
  } 
  else if (i < 300) // Third phase of fades
  {
    redVal   = 1; // Red up
    greenVal -= 1; // Green low
    blueVal  += 1; // Blue down
    whiteVal  = 0;
    blackVal  = 0;
  }
    else if (i < 400) // Third phase of fades
  {
    redVal   = 0; // Red up
    greenVal = 0; // Green low
    blueVal -= 1; // Blue down
    whiteVal += 1;
    blackVal = 0;
  }  
  else if (i < 500) // Third phase of fades
  {
    redVal   = 0; // Red up
    greenVal = 0; // Green low
    blueVal  = 0; // Blue down
    whiteVal -= 1;
    blackVal  += 1;
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  

  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal);  
  analogWrite(whitePin, whiteVal);
  analogWrite(blackPin, blackVal);

 
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}

This is to be embedded into this

int sensorValue;
int ledPin = 13;
void setup()

{

  Serial.begin(9600);

  pinMode(0, INPUT);
  pinMode(ledPin, OUTPUT);      // declare LED as output

}



void loop()

{

  sensorValue = analogRead(0);
  Serial.println(sensorValue, DEC);
  
  {if (sensorValue > 0)    ;     
  digitalWrite (ledPin, HIGH);
  
  else  (sensorValue = 0);
  digitalWrite (ledPin, LOW);
  delay (5000)}
  
}

And this, is what I have going

//Reads an analog input on Analog in 0, prints the result as an ASCII-formatted decimal value.



int sensorValue;
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
int whitePin = 5;
int blackPin = 3;

// Program variables
int redVal   = 0; // Variables to store the values to send to the pins
int greenVal = 0;   // Initial values are Red full, Green and Blue off
int blueVal  = 0;
int whiteVal = 10;
int blackVal = 255;

int i = 1;     // Loop counter    
int wait = 100; // 50ms (.05 second) delay; shorten for faster fades

void setup()
{  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT); 
  pinMode(whitePin,  OUTPUT); 
  pinMode(blackPin,  OUTPUT); 
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  pinMode(0, INPUT);

}



void loop()

{

  sensorValue = analogRead(0);
  Serial.println(sensorValue, DEC);
  
  {if (sensorValue > 0);     
{
  i += 1;      // Increment counter
  if (i < 100) // First phase of fades
  {
    redVal   += 1; // Red down
    greenVal  = 0; // Green up
    blueVal   = 0; // Blue low
    whiteVal  = 0;
    blackVal -= 1;
  }
  else if (i < 200) // Second phase of fades
  {
    redVal   -= 1; // Red low
    greenVal += 1; // Green down
    blueVal   = 0; // Blue up
    whiteVal  = 0;
    blackVal  = 0;
  } 
  else if (i < 300) // Third phase of fades
  {
    redVal   = 1; // Red up
    greenVal -= 1; // Green low
    blueVal  += 1; // Blue down
    whiteVal  = 0;
    blackVal  = 0;
  }
    else if (i < 400) // Third phase of fades
  {
    redVal   = 0; // Red up
    greenVal = 0; // Green low
    blueVal -= 1; // Blue down
    whiteVal += 1;
    blackVal = 0;
  }  
  else if (i < 500) // Third phase of fades
  {
    redVal   = 0; // Red up
    greenVal = 0; // Green low
    blueVal  = 0; // Blue down
    whiteVal -= 1;
    blackVal  += 1;
  }
  else // Re-set the counter, and start the fades again
  {
    i = 1;
  }  

  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal); 
  analogWrite(bluePin,  blueVal);  
  analogWrite(whitePin, whiteVal);
  analogWrite(blackPin, blackVal);

 
  delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
}
  
  
  else  (sensorValue = 0);

  delay (5000)}
  
}
{if (sensorValue > 0);

This statement does need a { in front of it.

The ; at the end means that if sensorValue is greater than 0, do nothing.

I doubt that that is your intent. It also explains why an else later is causing you problems.

Thanks to all for the responses and help. I am still having a bit of trouble with the integration and only can light up 2 of the 5 LEDs (numbers 2 and 3 in the array). I believe it may have to do with the counter but I am not sure

int sensorValue;
int redPin   = 9;   // Red LED,   connected to digital pin 9
int greenPin = 10;  // Green LED, connected to digital pin 10
int bluePin  = 11;  // Blue LED,  connected to digital pin 11
int whitePin = 5;
int blackPin = 3;
 
// Program variables
int redVal   = 0; // Variables to store the values to send to the pins
int greenVal = 0;   // Initial values are Red full, Green and Blue off
int blueVal  = 0;
int whiteVal = 10;
int blackVal = 255;
 
int i = 1;     // Loop counter   
int wait = 550; // 50ms (.05 second) delay; shorten for faster fades
 
void setup()
{  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);  
  pinMode(bluePin,  OUTPUT);
  pinMode(whitePin,  OUTPUT);
  pinMode(blackPin,  OUTPUT);
    Serial.begin(9600);  // ...set up the serial ouput on 0004 style
  pinMode(0, INPUT);
 
}
 
 
 
void loop()
 {
  {sensorValue = analogRead(0);
  Serial.println(sensorValue, DEC);
  delay (500);
     
  }

  if (i = 1 and sensorValue > 0) 
     i += 1; // Increment counter
  
  
 
  if (i < 10) // First phase of fades
  {
    redVal   += 1; // Red down
    greenVal  = 0; // Green up
    blueVal   = 0; // Blue low
    whiteVal  = 0;
    blackVal -= 1;
  }
  else if (i < 20) // Second phase of fades
  {
    redVal   -= 1; // Red low
    greenVal += 1; // Green down
    blueVal   = 0; // Blue up
    whiteVal  = 0;
    blackVal  = 0;
  }
  else if (i < 30) // Third phase of fades
  {
    redVal   = 1; // Red up
    greenVal -= 1; // Green low
    blueVal  += 1; // Blue down
    whiteVal  = 0;
    blackVal  = 0;
  }
    else if (i < 40) // Third phase of fades
  {
    redVal   = 0; // Red up
    greenVal = 0; // Green low
    blueVal -= 1; // Blue down
    whiteVal += 1;
    blackVal = 0;
  } 
  else if (i < 50) // Third phase of fades
  {
    redVal   = 0; // Red up
    greenVal = 0; // Green low
    blueVal  = 0; // Blue down
    whiteVal -= 1;
    blackVal  += 1;
  }
  
 
   if  (i > 500)// Re-set the counter, and start the fades again
  { 

    i = 1;
    delay(wait); // Pause for 'wait' milliseconds before resuming the loop
  }  
  analogWrite(redPin,   redVal);   // Write current values to LED pins
  analogWrite(greenPin, greenVal);
  analogWrite(bluePin,  blueVal); 
  analogWrite(whitePin, whiteVal);
  analogWrite(blackPin, blackVal);
  
 }

If you really do have a PIR connected to analog pin 0, you have a misunderstanding of PIRs. Is that what you have?

Look at the sequence of blocks and steps you have defined, ignoring for the moment what happens within each block.

You read a sensor value (incorrectly, I think). Then, you twiddle your thumbs for half a second. Then, you change the values of some variables, depending on how many times through the loop and whether the sensor reported HIGH or LOW, as long as that value is less than 50 or greater than 500. Do you have any idea what values you are getting from the sensor? What happens for i between 50 and 500? Once you set the values, you turn the lights on, and loop again (no delay).

As a result, no fading will ever occur. Presuming that the sensor returns HIGH, the lights will be turned on at varying light levels faster than you can see if they are changing.

If the sensor returns LOW, the lights stay at whatever level they were at when the sensor was last HIGH.

If you tryuly do have a PIR, it is (effectively) off or on. On when there is motion in the room, off if there is not. It is not instantaneous, so you can't hold still and expect to influence the on or off state of the sensor. As long as you are in the veicinity of it, and you must be to tell if the LEDs are on or off, the sensor will return HIGH. Since it is an on/off switch, an analogRead is not required. A digital pin and digitalRead is sufficient.

For now, comment out the sensor stuff. Write down what you want to happen all the time. Make the LEDs fade according to that description (hint: fading means that the light level changes slowly; i.e. there must be a pause at each discrete level).

Then, figure out what you want to have happen when the sensor goes HIGH. Should it resume fading, or start over again?

Also, figure out what should happen if the sensor goes LOW. Should it stop fading, and turn off all the lights, or just freeze with the lights on.

If you need help getting the fading bit to work, post the description you wrote down.