Tuesday, September 18, 2018

day 6 lab

const int LED=9; //Red LED on pin 9 (PWM)
const int LIGHT=0; //Lght Sensor on analog pin 0
const int MIN_LIGHT=200; //Minimum expected light value
const int MAX_LIGHT=900; //Maximum Expected Light value
const int PIR=2;//The PIR is connected to pin 2
const int max_thresh = 10; // Maximum threshold
int val = 0; //variable to hold the analog reading
void setup()
{
 pinMode(LED, OUTPUT); //Set LED pin as output
 pinMode (PIR, INPUT);
 Serial.begin(9600); //Start Serial Communication
}
void loop()
{
 val = analogRead(LIGHT); //Read the light sensor
 val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0);//Map the light reading
 val = constrain(val, 0, 255); //Constrain light value
 Serial.println(val); //Print it to the serial port
 delay(500);
 int PIRcount = digitalRead(PIR);
  if (digitalRead(PIR) == LOW && val == 255)
  {
    if( PIRcount < max_thresh);
    {
     digitalWrite(LED,HIGH); //Set the LED On
     delay(400); //Wait for 100 ms
     digitalWrite(LED,LOW); //Set the LED Off
     delay(80); //Wait for 10 second
     PIRcount++;
    }
  }
  else if(digitalRead(PIR) == HIGH)
  {
    digitalWrite(LED,HIGH); //Set the LED on
  }
  else if (digitalRead(PIR) == LOW && val <= 96)
  {
    digitalWrite(LED,LOW); //Set the LED off
  }
 }

No comments:

Post a Comment