HW 6

PIR AND LIGHT SENSORS, BLINK MODE INCLUDED WITH COUNT



boolean lastPIR = LOW;
boolean currentPIR = LOW;
const int LED = 10;
const int PIR = 2;
const int LIGHT = 0;
int val = 0;
int count = 0;
int x = 0;



boolean debounce(boolean last)
{
  boolean current = digitalRead(PIR); //Read the button state
  if (last != current) //if it's different...
  {
    delay(5); //wait 5ms
    current = digitalRead(PIR); //read it again
  }
  return current; //return the current value
}

\
void setup()
{
  pinMode (LED, OUTPUT);
  pinMode (PIR, INPUT);
  Serial.begin(9600);
}


void loop()
{
  val = analogRead(LIGHT);
  currentPIR = debounce(lastPIR);

  if (digitalRead(PIR) == HIGH)
  {
    digitalWrite (LED, HIGH);
    if (lastPIR == LOW && currentPIR == HIGH) count ++;
  }
  else if (val > 400 && digitalRead(PIR) == LOW)
  {
    digitalWrite (LED, LOW);
  }
  else if (val <= 400 && digitalRead(PIR) == LOW);
  {
    for (x = 0; x < count; x++)
    {
      if (digitalRead(PIR) != LOW || analogRead(LIGHT) > 200) break;
      digitalWrite (LED, HIGH);
      delay(75);
      if (digitalRead(PIR) != LOW || analogRead(LIGHT) > 200) break;
      digitalWrite (LED, LOW);
      delay(75);
      if (x == count - 1) delay(300);
    }
  }
  x = 0;
  lastPIR = currentPIR;
  Serial.println(val);

}

Comments

Popular posts from this blog

HW 1