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);
}