lab 6
Connect your PIR sensor and your CdS cell(light sensor).
The PIR and CdS sensors will control the LED behavior as follows:
When the PIR is triggered the LED is on.
When the CdS is uncovered and the PIR sensor is untriggered, the LED is off.
When the CdS cell is covered and the PIR sensor is off, the LED blinks.
To earn a 4 on this project your program can keep count of the number of time the PIR has been triggered and blink that number of times when it is in blink mode.
Code:
//Automatic Nightlight
const int RLED = 11; //Red LED on pin 11 (PWM)
const int GLED = 10;
const int BLED = 9;
const int LIGHT = 0; //Lght Sensor on analog pin 0
const int MOTION = 1;
const int BUTTON = 2;
const int MIN_LIGHT = 100; //Minimum expected light value
const int MAX_LIGHT = 500; //Maximum Expected Light value
int val = 0;
int val1 = 0;//variable to hold the analog reading
int valt = 0;
int count = 0;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean debounce(boolean last, int button)
{
boolean current = digitalRead(button); //Read the button state
if (last != current) //if it's different...
{
delay(10); //wait 5ms
current = digitalRead(button); //read it again
}
return current; //return the current value
}
void setup()
{
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);//Set LED pin as output
pinMode(BUTTON, INPUT);
Serial.begin(115200);
}
void loop()
{
Serial.println(count);
val = analogRead(LIGHT); //Read the light sensor
val1 = analogRead(MOTION);
val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0);//Map the light reading
val1 = map(val1, 0, 1023, 0, 7);
currentButton = debounce(lastButton, BUTTON);
if (valt != val1) {
count++;
valt = val1;
}
//Constrain light value
if (lastButton == LOW && currentButton == HIGH && val1<3)
{
if (count > 0)
{
for (int i = 0; i < count; i += 2)
{
delay(300);
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
delay(300);
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
}
count = 0;
}
else
{
for (int i = 0; i <= 8; i++)
{
delay(20);
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
delay(20);
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
}
}
}
else if (val1 > 3)
{
val = 255;
}
else if (val1 < 3 && val <= 30)
val = 0;
else if (val1 < 3 && val >= 200)
val = 255;
else
val = constrain(val, 0, 255);
lastButton = currentButton;
analogWrite(RLED, val);
analogWrite(GLED, val);
analogWrite(BLED, val);
}
The PIR and CdS sensors will control the LED behavior as follows:
When the PIR is triggered the LED is on.
When the CdS is uncovered and the PIR sensor is untriggered, the LED is off.
When the CdS cell is covered and the PIR sensor is off, the LED blinks.
To earn a 4 on this project your program can keep count of the number of time the PIR has been triggered and blink that number of times when it is in blink mode.
Code:
//Automatic Nightlight
const int RLED = 11; //Red LED on pin 11 (PWM)
const int GLED = 10;
const int BLED = 9;
const int LIGHT = 0; //Lght Sensor on analog pin 0
const int MOTION = 1;
const int BUTTON = 2;
const int MIN_LIGHT = 100; //Minimum expected light value
const int MAX_LIGHT = 500; //Maximum Expected Light value
int val = 0;
int val1 = 0;//variable to hold the analog reading
int valt = 0;
int count = 0;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean debounce(boolean last, int button)
{
boolean current = digitalRead(button); //Read the button state
if (last != current) //if it's different...
{
delay(10); //wait 5ms
current = digitalRead(button); //read it again
}
return current; //return the current value
}
void setup()
{
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(BLED, OUTPUT);//Set LED pin as output
pinMode(BUTTON, INPUT);
Serial.begin(115200);
}
void loop()
{
Serial.println(count);
val = analogRead(LIGHT); //Read the light sensor
val1 = analogRead(MOTION);
val = map(val, MIN_LIGHT, MAX_LIGHT, 255, 0);//Map the light reading
val1 = map(val1, 0, 1023, 0, 7);
currentButton = debounce(lastButton, BUTTON);
if (valt != val1) {
count++;
valt = val1;
}
//Constrain light value
if (lastButton == LOW && currentButton == HIGH && val1<3)
{
if (count > 0)
{
for (int i = 0; i < count; i += 2)
{
delay(300);
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
delay(300);
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
}
count = 0;
}
else
{
for (int i = 0; i <= 8; i++)
{
delay(20);
analogWrite(RLED, 255);
analogWrite(GLED, 255);
analogWrite(BLED, 255);
delay(20);
analogWrite(RLED, 0);
analogWrite(GLED, 0);
analogWrite(BLED, 0);
}
}
}
else if (val1 > 3)
{
val = 255;
}
else if (val1 < 3 && val <= 30)
val = 0;
else if (val1 < 3 && val >= 200)
val = 255;
else
val = constrain(val, 0, 255);
lastButton = currentButton;
analogWrite(RLED, val);
analogWrite(GLED, val);
analogWrite(BLED, val);
}
Comments
Post a Comment