Code for PIR sensor

Hey I have a college assignment so I can't just copy down the code already provided by the Arduino site or any other course. The objective is to write my own code and well, I can't see where my problem is. I just want to sound an alarm, light an LED and print to the PC that motion was detected. Nothing more.

Here's what I have so far

//Global Variables
int RledPin = 13;
int GledPin = 12;
int pirPin = 3;
int val = 0;

void setup()
{
Serial.begin(9600);
pinMode(RledPin, OUTPUT);
pinMode(GledPin, OUTPUT);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW); //start with no motion

//give the sensor 30 seconds to calibrate
Serial.print("Loading");
for(int i = 0; i < 31; i++)
{
Serial.print(".");
delay(1000);
}
Serial.println("Complete");
Serial.println("SENSOR ACTIVE");
}

void loop()
{
val = digitalRead(pirPin); //set PIR value equal to variable val

if(val == HIGH); //When PIR gives a "HIGH" do the following
{
digitalWrite(RledPin, HIGH); // turn red LED on
digitalWrite(GledPin, LOW); // turn green LED off
Serial.println("Motion Detected"); //print motion detected
tone(10, 2200); //2.2KHz sent to pin 10
delay(5000);// delay for 5 seconds
val = LOW; // set val back to LOW

}

if(val == LOW);//When PIR gives a "LOW" do the following
{
digitalWrite(GledPin, HIGH); // turn green LED on
digitalWrite(RledPin, LOW); // turn red LED off
Serial.println("Motion Ended"); //print motion ended
delay(5000); // delay for 5 seconds
}
}

Any help would be greatly appreciated!!!

attatched is my circuit diagram
https://imagizer.imageshack.us/v2/536x667q90/838/hso7.png

Hey I have a college assignment so I can't just copy down the code already provided by the Arduino site or any other course.

Why not,everyone else seems to be doing it. Every day we get several posts that start like this:
I am a newbee. I need a program that does this [program requirements], copied from blackboard].
Can you help me please ? Occasionally they are really bold and write something like this:
"write a program that does this {program requirements]" without any introduction or grovelling as is most common.

digitalWrite(pirPin, LOW); //start with no motion

think this line can screw up the working as pirPin was set as an INPUT.
The pirPin decides if there was motion or not , you cannot set it. (unless you make a MVC state machine and even then)

//give the sensor 30 seconds to calibrate
  Serial.print("Loading");
   for(int i = 0; i < 31; i++)
   {
      Serial.print(".");
      delay(1000);
   }

==> comments not needed if you use meaningfull strings varnames etc

  Serial.print("Callibrating");
   for(int  timeTillStart = 30; timeTillStart  > 0;  timeTillStart-- )
   {
      Serial.print(timeTillStart);
      Serial.print(" ");
      delay(1000);
   }

also this part

    ...
    val = LOW; // set val back to LOW  <<<<<<<< this line makes the second if always true, 
    
  }

  if(val == LOW);//When PIR gives a "LOW" do the following
  {

http://skullbucks.in/projects/arduino/distancesensor.html

Hey I got it working using analogRead
Thanks for your suggestions everyone!
code is as follows:

// Global Varialables 
int pirPin = 3;         // PIR connected to A3
int RledPin = 13;       // red LED connected to pin 13
int GledPin = 12;       // green LED connected to in 12 
int val = 0;            // Varialbe for reading PIR #
unsigned long time = 0; // Variable to count time

void setup()
{
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     // PIR input
  pinMode(RledPin, OUTPUT);   // LED output
  pinMode(GledPin, OUTPUT);   // LED output
  
  // The PIR sensor needs approx 30 seconds to calibrate before use
  Serial.print("Loading");
  for(int i = 0; i < 31; i++)  
  {
      Serial.print(".");
      delay(1000);
  }
    Serial.println("Complete");
    Serial.println("SENSOR ACTIVE");  // The PIR sensor is now calibrated and ready to use
    delay(50);
}

void loop()
{    
     // millis() is a function that starts counting how long the program started in milli seconds [can count to approx 50 days]
     time = millis();                 // assign variable time equal to millis()
     time = time/1000;                // divide time by 1000 to give an answer in seconds
     val = (analogRead(pirPin));      // Assign variable val equal to the same value as the PIR
     if(val < 1000)                   // the PIR gives unreliable reading's thus if it is any less than 1000 do the following...
     {
       digitalWrite(RledPin, HIGH);   // red LED HIGH 
       digitalWrite(GledPin, LOW);    // green LED LOW
       Serial.println("Motion Detected at:");  // print that motion was detected and when
       Serial.println(time);
       Serial.println("Seconds Since Start of Program");
       tone(10, 2200);                // 2.2KHz sent to the PEIZO buzzer connected to PWM pin 10 
       delay(5000);                   // Sound the alarm for 5 seconds 
     }         

     if(val == 1023)                       // if PIR doesn't detect motion then it gives a HIGH
     {       
       digitalWrite(RledPin, LOW);         // red LED LOW
       digitalWrite(GledPin, HIGH);        // green LED HIGH     
       noTone(10);                         // if no motion is being detected then turn off the PEIZO buzzer 
     }
 }

moderator update: added code tags ==> # button above the smileys.

 // Global Varialables 
int pirPin = 3;         // PIR connected to A3
int RledPin = 13;       // red LED connected to pin 13
int GledPin = 12;       // green LED connected to in 12 
int val = 0;            // Varialbe for reading PIR #
unsigned long time = 0; // Variable to count time

void setup()
{
  Serial.begin(9600);
  pinMode(pirPin, INPUT);     // PIR input
  pinMode(RledPin, OUTPUT);   // LED output
  pinMode(GledPin, OUTPUT);   // LED output
  
  // The PIR sensor needs approx 30 seconds to calibrate before use
  Serial.print("Loading");
  for(int i = 0; i < 31; i++)  
  {
      Serial.print(".");
      delay(1000);
  }
    Serial.println("Complete");
    Serial.println("SENSOR ACTIVE");  // The PIR sensor is now calibrated and ready to use
    delay(50);
}

void loop()
{    
     // millis() is a function that starts counting how long the program started in milli seconds [can count to approx 50 days]
     time = millis();                 // assign variable time equal to millis()
     time = time/1000;                // divide time by 1000 to give an answer in seconds
     val = (analogRead(pirPin));      // Assign variable val equal to the same value as the PIR
     if(val < 1000)                   // the PIR gives unreliable reading's thus if it is any less than 1000 do the following...
     {
       digitalWrite(RledPin, HIGH);   // red LED HIGH 
       digitalWrite(GledPin, LOW);    // green LED LOW
       Serial.println("Motion Detected at:");  // print that motion was detected and when
       Serial.println(time);
       Serial.println("Seconds Since Start of Program");
       tone(10, 2200);                // 2.2KHz sent to the PEIZO buzzer connected to PWM pin 10 
       delay(5000);                   // Sound the alarm for 5 seconds 
     }         

     if(val == 1023)                       // if PIR doesn't detect motion then it gives a HIGH
     {       
       digitalWrite(RledPin, LOW);         // red LED LOW
       digitalWrite(GledPin, HIGH);        // green LED HIGH     
       noTone(10);                         // if no motion is being detected then turn off the PEIZO buzzer 
     }
 }
« Last Edit: Today at 11:20:57 pm by RandyLeahy »