Go Down

Topic: PIR issue to run sketch (Read 638 times) previous topic - next topic

funnybug

I have been working on an LED project that runs continuously, but I only want it to run when the Parallax PIR senses movement

I thought I could control it using "Do While LED (pin 13) but I haven't been successful

any help?

Code: [Select]
/*
  Simulation of a dragstrip light tree.
*/

int stage1a = 2;
int stage2a = 3;
int stage1b = 4;
int stage2b = 5;
int tree1 =   6;
int tree2 =   7;
int tree3 =   8;
int treetop = 9;
int led = 13;


// the setup routine runs once when you press reset:
void setup() {           
  // initialize the digital pin as an output.
  pinMode(stage1a, OUTPUT);     
  pinMode(stage2a, OUTPUT);     
  pinMode(stage1b, OUTPUT);     
  pinMode(stage2b, OUTPUT);     
  pinMode(tree1, OUTPUT);     
  pinMode(tree2, OUTPUT);     
  pinMode(tree3, OUTPUT);     
  pinMode(treetop, OUTPUT); 
  pinMode(led, OUTPUT);
  randomSeed(analogRead(0));
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, 0);          // delay 15 seconds between runs
  digitalWrite(stage1a, HIGH);
  delay(random(300,2000));    // throw in a random delay for the next staging
  digitalWrite(stage1b, HIGH);
  delay(random(1500,3000));    // throw in a random delay for the next staging
  digitalWrite(stage2b, HIGH);
  delay(random(300,1500));    // throw in a random delay for the next staging
  digitalWrite(stage2a, HIGH);
  delay(500);
  digitalWrite(tree1, HIGH);
  delay(500);
  digitalWrite(tree2, HIGH);
  delay(500);
  digitalWrite(tree3, HIGH);
  delay(500);
  digitalWrite(treetop, HIGH);

/* after the race starts, wait 10 seconds, then turn off all the lights.
  delay(10000); */

  digitalWrite(stage1a, LOW);
  digitalWrite(stage1b, LOW);
  digitalWrite(stage2b, LOW);
  digitalWrite(stage2a, LOW);
  digitalWrite(tree1, LOW);
  digitalWrite(tree2, LOW);
  digitalWrite(tree3, LOW);
  digitalWrite(treetop, LOW);
  digitalWrite(led, 1);
  delay(200);
}
.

Lavan

If I understand correctly, you need to attach a PIR sensor module and have the code run when there is a motion detected?  If yes, define one of the digital pin as input pin and connect the output of the PIR sensor to that digital pin.  when there is a moment, the  output of the PIR sensor goes high.  Use if statement like this
Code: [Select]
if (digitalread(sensorPin) == HIGH)
{
//put your code here
}

else

{
//do nothing or anything that you want to execute
}


hope it helps!

funnybug

#2
Mar 12, 2014, 01:02 pm Last Edit: Mar 12, 2014, 01:06 pm by funnybug Reason: 1
Thanks. I wish it was that easy but I continue to get "error:  expected unqualified-id before 'else"

Code: [Select]
// Simulation of a dragstrip light tree.
 int stage1a = 2;
 int stage2a = 7;
 int stage1b = 3;
 int stage2b = 5;
 int tree1 =   4;
 int tree2 =   6;
 int tree3 =   8;
 int greengo = 10;
 int pirpin = 12;
 int led = 13;

// setup
void setup() {          
 pinMode(stage1a, OUTPUT);    
 pinMode(stage2a, OUTPUT);    
 pinMode(stage1b, OUTPUT);    
 pinMode(stage2b, OUTPUT);    
 pinMode(tree1, OUTPUT);    
 pinMode(tree2, OUTPUT);    
 pinMode(tree3, OUTPUT);    
 pinMode(greengo, OUTPUT);
 pinMode(led, OUTPUT);  
 randomSeed(analogRead(0));
 pinMode(pirpin, INPUT);
 delay(2000);
}

void loop() {
 if (digitalRead(pirpin) == HIGH);
 digitalWrite(stage1a, HIGH);
 delay(random(300,2000));    // throw in a random delay for the next staging
 digitalWrite(stage2a, HIGH);
 delay(random(1500,3000));    // throw in a random delay for the next staging
 digitalWrite(stage1b, HIGH);
 delay(random(300,1500));    // throw in a random delay for the next staging
 digitalWrite(stage2b, HIGH);
 delay(500);
 digitalWrite(tree1, HIGH);
 delay(500);
 digitalWrite(tree1, LOW);
 digitalWrite(tree2, HIGH);
 delay(500);
 digitalWrite(tree2, LOW);
 digitalWrite(tree3, HIGH);
 delay(500);
 digitalWrite(tree3, LOW);
 digitalWrite(greengo, HIGH);
 digitalWrite(stage1a, LOW);
 delay(35);
 digitalWrite(stage1b, LOW);
 delay(71);
 digitalWrite(stage2b, LOW);
 digitalWrite(stage2a, LOW);
 delay(5000);
 digitalWrite(pirpin, LOW);
 digitalWrite(greengo, LOW);
 digitalWrite(led,1);
 delay(100);
 digitalWrite(led,0);
}
else
{
 delay(50000)
}

Lavan

You are missing braces and also need to remove the semicolon at the end of if statement. I have modified your code
Code: [Select]
// Simulation of a dragstrip light tree.
  int stage1a = 2;
  int stage2a = 7;
  int stage1b = 3;
  int stage2b = 5;
  int tree1 =   4;
  int tree2 =   6;
  int tree3 =   8;
  int greengo = 10;
  int pirpin = 12;
  int led = 13;

// setup
void setup() {           
  pinMode(stage1a, OUTPUT);     
  pinMode(stage2a, OUTPUT);     
  pinMode(stage1b, OUTPUT);     
  pinMode(stage2b, OUTPUT);   
  pinMode(tree1, OUTPUT);     
  pinMode(tree2, OUTPUT);     
  pinMode(tree3, OUTPUT);     
  pinMode(greengo, OUTPUT);
  pinMode(led, OUTPUT); 
  randomSeed(analogRead(0));
  pinMode(pirpin, INPUT);
  delay(2000);
}

void loop() {
  if (digitalRead(pirpin) == HIGH)
  {
  digitalWrite(stage1a, HIGH);
  delay(random(300,2000));    // throw in a random delay for the next staging
  digitalWrite(stage2a, HIGH);
  delay(random(1500,3000));    // throw in a random delay for the next staging
  digitalWrite(stage1b, HIGH);
  delay(random(300,1500));    // throw in a random delay for the next staging
  digitalWrite(stage2b, HIGH);
  delay(500);
  digitalWrite(tree1, HIGH);
  delay(500);
  digitalWrite(tree1, LOW);
  digitalWrite(tree2, HIGH);
  delay(500);
  digitalWrite(tree2, LOW);
  digitalWrite(tree3, HIGH);
  delay(500);
  digitalWrite(tree3, LOW);
  digitalWrite(greengo, HIGH);
  digitalWrite(stage1a, LOW);
  delay(35);
  digitalWrite(stage1b, LOW);
  delay(71);
  digitalWrite(stage2b, LOW);
  digitalWrite(stage2a, LOW);
  delay(5000);
  digitalWrite(pirpin, LOW);
  digitalWrite(greengo, LOW);
  digitalWrite(led,1);
  delay(100);
  digitalWrite(led,0);
}

else

{
  delay(50000);
}

}

Go Up