PIR to control Multiple LED sequences

Hi,

I am new to Arduino and coding, I found this on Instructables: http://www.instructables.com/id/Make-Your-Own-LED-Illuminated-Laser-Cut-Box/?ALLSTEPS

I am trying to add code so that the light box is turned on by a PIR sensor, the code I have "compiles" but I fear the bit that I have added for the first " digitalWrite(GREEN1, HIGH);" will only turn on the green LED, rather than the whole "Loop".
I hope my terminology is correct and I give thanks in advance if someone would have a look, and if I am wrong point me in the right direction.
I have not done the wiring circuit yet as I was hoping to manage the code first.

Thanks.

long Rvalue1, Gvalue1, Bvalue1 ;   // Leaf PWM value
long Rvalue2, Gvalue2, Bvalue2 ;   // Tree PWM value
long Rvalue3, Gvalue3, Bvalue3 ;   // Bird PWM value
long Rvalue4;                      // Heart PWM value
 
// Assigning LED's to pins
int inputPin = 14;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;
int GREEN1 = 7;
int BLUE1 = 5;
int RED1 = 6;
int GREEN2 = 4;
int BLUE2 = 2;
int RED2 = 3;
int GREEN3 = 10;
int BLUE3 = 8;
int RED3 = 9;
int RED4 = 12;

float i = 0;    // HeartLoop counter
float i2 = 0;   // TreeLeafLoop counter
float i3 = 0;   // BirdLoop counter



void setup()
{
  pinMode(inputPin, INPUT);     // declare sensor as input
  pinMode(GREEN1, OUTPUT);
  pinMode(BLUE1, OUTPUT);
  pinMode(RED1, OUTPUT);
  pinMode(GREEN2, OUTPUT);
  pinMode(BLUE2, OUTPUT);
  pinMode(RED2, OUTPUT);
  pinMode(GREEN3, OUTPUT);
  pinMode(BLUE3, OUTPUT);
  pinMode(RED3, OUTPUT);
  pinMode(RED4, OUTPUT);
}

void loop()
{
  /* The speed of program depends on how heavy the code is, 
  for this reason I made that tempo of every loop can be changed individually. */
  
  HeartLoop(1.5);       //  tempo of the heart cycle ( the higher the slower )
  BirdLoop(10.0);       //  tempo of the bird cycle ( the higher the slower )
  TreeLeafLoop(1.0);    //  tempo of the tree/leaf cycle ( the higher the slower )
    val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(GREEN1, 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(GREEN1, 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;
    }
  }
}


void TreeLeafLoop(float Ttempo) {

  if ( i2 < 130000 ) {    
    i2 += 1 / Ttempo;
  }
  else {
    i2 = 0;
  }

  // ________ Spring/Summer cycle _________
  if (i2 < 5100) {
    // Leaf PWM values
    Rvalue1 = i2 / 34;     // updates PWM value every i2. Max PWM value after 5100 steps is 5100/34=150 
    Gvalue1 = i2 / 30;
    Bvalue1 = 0;
    // Tree PWM values
    Rvalue2 = 150 - (i2 / 102);
    Gvalue2 = 150 - (i2 / 42);
    Bvalue2 = 150 - (i2 / 34);
  }
  if ( 10200 > i2 && i2 >= 5100) {
    Rvalue1 = 150 - (i2 - 5100) / 34;
    Gvalue1 = 170;
 
    Rvalue2 = 100;
    Gvalue2 = 29;
    Bvalue2 = 0;  
    
  }

  if ( 50000 > i2 && i2 >= 10200) {
    Gvalue1 = 170;
    Rvalue1 = 0;
  }
  // ________ Autumn cycle _________
  if ( 55100 > i2 && i2 >= 50000) {
    Gvalue1 = 170;
    Rvalue1 = (i2 - 50000) / 34 ;
  }


  if ( 60200 > i2 && i2 >= 55100) {
    Gvalue1 = 170;
    Rvalue1 = 150;
  }

  if ( 65300 > i2 && i2 >= 60200) {
    Gvalue1 = 170 - ((i2 - 60200) / 34);
    Rvalue1 = 150;
   
    Rvalue2 = 100 - ((i2 - 60200) / 102);
    Gvalue2 = 29 - ((i2 - 60200) / 300);
  }


  if ( 70000 > i2 && i2 >= 65300) {
    Gvalue1 = 16;
    Rvalue1 = 150;
    
    Rvalue2 = 50;
    Gvalue2 = 12;
  }
  // ________ Winter cycle _________

  if ( 75100 > i2 && i2 >= 70000) {
    Gvalue1 = 16 + (i2 - 70000) / 38;
    Rvalue1 = 150;
    Bvalue1 = (i2 - 70000) / 34;
    
    Rvalue2 = 100 + ((i2 - 70000) / 102);
    Gvalue2 = 29 + ((i2 - 70000) / 42);
    Bvalue2 = (i2 - 70000) / 34;
    
  }


  if ( 90000 > i2 && i2 >= 75100) {
    Gvalue1 = 150;
    Rvalue1 = 150;
    Bvalue1 = 150;
    
    Rvalue2 = 150;
    Gvalue2 = 150;
    Bvalue2 = 150;
  }


  if ( 95100 > i2 && i2 >= 90000) {
    Gvalue1 = 150 - (i2 - 90000) / 34;
    Rvalue1 = 150 - (i2 - 90000) / 34;
    Bvalue1 = 150 - (i2 - 90000) / 34;
  }


  if ( 130000 > i2 && i2 >= 95100) {
    Gvalue1 = 0;
    Rvalue1 = 0;
    Bvalue1 = 0;
  }


  analogWrite(RED1, Rvalue1);           // sets the value (range from 0 to 255)
  analogWrite(GREEN1, Gvalue1);           // sets the value (range from 0 to 255)
  analogWrite(BLUE1, Bvalue1);           // sets the value (range from 0 to 255)
  analogWrite(RED2, Rvalue2);           // sets the value (range from 0 to 255)
  analogWrite(GREEN2, Gvalue2);           // sets the value (range from 0 to 255)
  analogWrite(BLUE2, Bvalue2);           // sets the value (range from 0 to 255)  
  

}
void BirdLoop(float Btempo) {

  if ( i3 < 3060 ) {
    i3 += 1 / Btempo;
  }
  else {
    i3 = 0;
  }


  if (i3 < 510) {
    Rvalue3 = 20 + i3 / 6;
    Gvalue3 = 20;
    Bvalue3 = 105 - i3 / 6;
  }

  if ( 1020 > i3 && i3 >= 510) {
    Rvalue3 = 105;
    Gvalue3 = 20;
    Bvalue3 = 20;
  }

  if ( 1530 > i3 && i3 >= 1020) {
    Rvalue3 = 105 - (i3 - 1020) / 6;           
    Gvalue3 = 20 + (i3 - 1020) / 6;
    Bvalue3 = 20;
  }

  if ( 2040 > i3 && i3 >= 1530) {
    Rvalue3 = 20;
    Gvalue3 = 105;
    Bvalue3 = 20;
  }

  if ( 2550 > i3 && i3 >= 2040) {
    Rvalue3 = 20 ;
    Gvalue3 = 105 - (i3 - 2040) / 6;
    Bvalue3 = 20 + (i3 - 2040) / 6;
  }

  if ( 3060 > i3 && i3 >= 2550) {
    Rvalue3 = 20 ;
    Gvalue3 = 20;
    Bvalue3 = 105;
  }


  analogWrite(RED3, Rvalue3);           // sets the value (range from 0 to 255)
  analogWrite(GREEN3, Gvalue3);           // sets the value (range from 0 to 255)
  analogWrite(BLUE3, Bvalue3);           // sets the value (range from 0 to 255)

}


void HeartLoop(float Htempo) {

  if ( i < 4700 ) {
    i += 1 / Htempo;
  }
  else {
    i = 0;
  }

  if (i < 2000) {
    Rvalue4 = 45 + (exp(sin((3000 + i) / 2000.0 * 3.14)) - 0.37) * 89.35;      // modified e^sin(x) function
  }

  if ( 2200 > i && i >= 2000) {
    Rvalue4 = 120;
  }
  if ( 2600 > i && i >= 2200) {
    Rvalue4 = 255;
  }
  if ( 2700 > i && i >= 2600) {
    Rvalue4 = 150;
  }
  if (i > 2700) {
    Rvalue4 = 45 + (exp(sin((2300 + i) / 2000.0 * 3.14)) - 0.37) * 89.35;
  }
  analogWrite(RED4, Rvalue4);
}

I think that you want the light box to turn on and run the show if motion is detected by the PIR. If a person walks up to the panel, and the show turns on, what do you want to happen if the person doesn't move? And later, they move away and motion is again detected? Do you want to detect the presence of an observer, or the movement of an observer? If its presence and not movement, you may want a different sensor.

I would recommend that you get a PIR and begin coding to understand how the sensor works, and what you want the behaviour of your system to be. I'm not sure if the little piece of PIR code you patched in really does what you want.

The construction of the box and possibly creating something different than the current show seems a far larger task than figuring out how to turn it on and off. I'd focus on that.

Here is one way run the loop when the sensor triggers. Loop will stop when movement stops.

Create a new boolean variable runShow, and inititialize it as false.

EDIT: Instead of this new variable, you could use pirState in the conditional.

Then your loop will look like this

void loop()
{
  /* The speed of program depends on how heavy the code is,
    for this reason I made that tempo of every loop can be changed individually. */

static boolean runShow = false; 
 
if (runShow == true)
  {
    HeartLoop(1.5);       //  tempo of the heart cycle ( the higher the slower )
    BirdLoop(10.0);       //  tempo of the bird cycle ( the higher the slower )
    TreeLeafLoop(1.0);    //  tempo of the tree/leaf cycle ( the higher the slower )
  }

  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    // digitalWrite(GREEN1, HIGH);  // turn LED ON
    if (pirState == LOW) {
      // we have just turned on
      runShow = true;
      Serial.println("Motion detected!");
      // We only want to print on the output change, not state
      pirState = HIGH;
    }
  } else {
    //digitalWrite(GREEN1, LOW); // turn LED OFF
    if (pirState == HIGH) {
      // we have just turned off
      runShow = false;
      Serial.println("Motion ended!");
      //  We only want to print on the output change, not state
      pirState = LOW;
    }
  }
}

Completing one full show, that is until the completion of the longest element, the TreeLeafLoop, will require some additional logic, but it should be pretty straightforward to add another boolean variable showComplete, and only turn off the show off when it is true.

Thank You Cattledog,

Thanks for your input and your code,

I understand what you mean about "detect the presence of an observer, or the movement of an observer". That is something that I had not thought about, I assumed that the PIR would remain active, play the show and then turn off until the next time it was triggered.

I have just received the PIR sensor in today's mail delivery and I am still waiting on some of the other components to arrive, the light box has been laser cut, that said I appreciate your comment about learning more about how the sensor works, the coding and the way I would like the behavior of the system to function.

Thanks again