//ENME461 Lab 1, P4
// photoresistor. This is a sensor that changes resistance based on the intensity of light.
//The resistance of a PR DECREASES!!! as the intensity of light received by it increases.
// Create a circuit and an appropriate program that performs the following:
// • Turns an LED on when the photoresistor is covered (by hand or a piece of paper) and
// turns off the LED when the photoresistor is uncovered.
// • Turns on a separate LED when a potentiometer is turned past the halfway point and turns
// off the LED when the potentiometer is turned below the halfway point.
// • Hint: First read the digitally mapped voltage reading from the photoresistor in ambient
// lab light, similar to Project three. Then use this number to construct an if loop which
// turns an LED on if voltage falls significantly below this number.
int greenphoto = 2;
int redpot = 5;
int potentipin = A0;
int photopin = A3;
int PotiVal;
int PhotoVal;
float PRambient = 400;
float MaxPoti = 1023;
void setup() {
pinMode(greenphoto, OUTPUT);
pinMode(redpot, OUTPUT);
Serial.begin(9600); //want to have serial monitor open to see PR resistance in ambient lighting
}
void loop() {
PhotoVal = analogRead(photopin); //read photopin in ambient lighting, then put above
Serial.print("Photoresistor Value: "); //print recorded pin to serial screen
Serial.println(PhotoVal);
delay(500);
//photometer green pin
if(PhotoVal >= PRambient) {
digitalWrite(greenphoto, HIGH);
}
else {
digitalWrite(greenphoto, LOW);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PotiVal = analogRead(potentipin); //read potentiometer
Serial.print("Potentiometer Value: "); //print, to find max should be 1023
Serial.println(PotiVal);
delay(500);
//potentiometer Red pin
if( PotiVal >= (0.5 * MaxPoti)) {
digitalWrite(redpot, HIGH);
}
else {
digitalWrite(redpot, LOW);
}
}