Molight from Natalie Lew on Vimeo.
Our Code:
//WHICH PINS INTERFACE WITH THE RELAY AND THE LIGHTBULBS
int ledPin1 = 12;
int ledPin2 = 11;
int ledPin3 =10;
int ledPin4 = 9;
//WHICH PINS INTERFACE WITH THE PIR SENSORS
int inputPin1 = 7;
int inputPin2 = 6;
int inputPin3 = 5;
int inputPin4 = 4;
//THE STARTING STATE OF THE PIR SENSORS (NO MOTION)
int pirState1 = LOW;
int pirState2 = LOW;
int pirState3 = LOW;
int pirState4 = LOW;
//MORE STUFFFFDFDFDFD
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
//PULSES FOR SINGLE ON
boolean pulse1;
boolean pulse2;
boolean pulse3;
boolean pulse4;
boolean zoneOneOccupied;
boolean zoneTwoOccupied;
boolean zoneThreeOccupied;
boolean zoneFourOccupied;
int timeAllowed = 10000; // ten seconds in millis()
unsigned long timer;
boolean timeExpired;
unsigned long startDelayTimer;
void setup() {
Serial.begin(9600);
//LIGHBULB OUTPUTS
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
//SENSOR INTPUTS
pinMode(inputPin1, INPUT);
pinMode(inputPin2, INPUT);
pinMode(inputPin3, INPUT);
pinMode(inputPin4, INPUT);
boolean zoneOneOccupied = false;
boolean zoneTwoOccupied = false;
boolean zoneThreeOccupied = false;
boolean zoneFourOccupied = false;
}
void loop() {
Serial.print("millis is ");
Serial.println(millis());
Serial.print("timer is ");
Serial.println(timer);
///////////////////////////////
//CHECKING ALL OF THE SENSORS//
///////////////////////////////
//ZONE ONE ACTIVITY
val1 = digitalRead(inputPin1);
if (val1 == HIGH) {
if (pirState1 == LOW) {
pirState1 = HIGH;
pulse1 = true;
}
} else {
if (pirState1 == HIGH) {
pirState1 = LOW;
}
}
//pirState1 = digitalRead(inputPin1);
//ZONE TWO ACTIVITY
val2 = digitalRead(inputPin2);
if (val2 == HIGH) {
if (pirState2 == LOW) {
pirState2 = HIGH;
pulse2 = true;
}
} else {
if (pirState2 == HIGH) {
pirState2 = LOW;
}
}
//ZONE THREE ACTIVITY
val3 = digitalRead(inputPin3);
if (val3 == HIGH) {
if (pirState3 == LOW) {
pirState3 = HIGH;
pulse3 = true;
}
} else {
if (pirState3 == HIGH) {
pirState3 = LOW;
}
}
//ZONE FOUR ACTIVITY
val4 = digitalRead(inputPin4);
if (val4 == HIGH) {
if (pirState4 == LOW) {
pirState4 = HIGH;
pulse4 = true;
}
} else {
if (pirState4 == HIGH) {
pirState4 = LOW;
}
}
///////////////////////////////////////
//ACTING ON THE OUTPUT OF THE SENSORS//
///////////////////////////////////////
//ZONE ONE
if(pulse1) {
pulse1 = false;
Serial.println("one");
if(!zoneOneOccupied) {
timer = millis();
timeExpired = false;
digitalWrite(ledPin1, HIGH);
zoneOneOccupied = true;
zoneTwoOccupied = false;
zoneThreeOccupied = false;
zoneFourOccupied = false;
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
//delay(timeToDelay);
}
}
if(!zoneOneOccupied && !timeExpired) {
digitalWrite(ledPin1, LOW);
}
//ZONE TWO
if(pulse2) {
pulse2 = false;
Serial.println(" two");
if(!zoneTwoOccupied) {
timer = millis();
timeExpired = false;
digitalWrite(ledPin2, HIGH);
zoneTwoOccupied = true;
zoneOneOccupied = false;
zoneThreeOccupied = false;
zoneFourOccupied = false;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
//delay(timeToDelay);
}
}
if(!zoneTwoOccupied && !timeExpired) {
digitalWrite(ledPin2, LOW);
}
//ZONE THREE
if(pulse3) {
pulse3 = false;
Serial.println(" three");
if(!zoneThreeOccupied) {
timer = millis();
timeExpired = false;
digitalWrite(ledPin3, HIGH);
zoneThreeOccupied = true;
zoneOneOccupied = false;
zoneTwoOccupied = false;
zoneFourOccupied = false;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin4, LOW);
//delay(timeToDelay);
}
}
if(!zoneThreeOccupied && !timeExpired) {
digitalWrite(ledPin3, LOW);
}
//ZONE FOUR
if(pulse4) {
pulse4 = false;
Serial.println(" four");
if(!zoneFourOccupied) {
timeExpired = false;
timer = millis();
digitalWrite(ledPin4, HIGH);
zoneFourOccupied = true;
zoneOneOccupied = false;
zoneTwoOccupied = false;
zoneThreeOccupied = false;
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
//delay(timeToDelay);
}
}
if(!zoneFourOccupied && !timeExpired) {
digitalWrite(ledPin4, LOW);
}
///////////////////////////////////////////////
//WHEN PEOPLE SPEND TOO MUCH TIME IN ONE ZONE//
///////////////////////////////////////////////
if (millis() - timer > timeAllowed && timer > 0) {
if(!timeExpired) {
startDelayTimer = millis();
}
timeExpired = true;
Serial.println(startDelayTimer);
}
if(timeExpired) {
Serial.println("time expired");
if(startDelayTimer < 1) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
}
if( millis() - startDelayTimer < 500) {
digitalWrite(ledPin1, HIGH);
} else if(millis() - startDelayTimer < 1000) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
} else if(millis() - startDelayTimer < 1500) {
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
} else if(millis() - startDelayTimer < 2000) {
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
} else {
digitalWrite(ledPin4, LOW);
startDelayTimer = millis();
}
}
}
No comments:
Post a Comment