Week 9: Analog and Digital sensors – Picture Perfect


I have always wondered why it is not possible to take the perfect picture of yourself. I realized that two of the most important controllable variables in photography are the lighting and the focus distance. Hence the project was designed to give an indication of the perfect lighting for the surroundings and the distance for the person taking the photo.


const int buttonPin = 7; 
int onOff = 0;
int sensorPin = A0;   
int ledPin = 13;  
int sensorValue = 0; 
const int blueLEDPin = 12;
const int yellowLEDPin = 8;
const int goLED = 10;
#define echoPin 2 
#define trigPin 3
long duration; 
int distance;
void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  digitalWrite(blueLEDPin, OUTPUT);
  digitalWrite(yellowLEDPin, OUTPUT);
  digitalWrite(goLED, OUTPUT);
  pinMode(buttonPin, INPUT);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT


void loop() {
  onOff = digitalRead(buttonPin);
  sensorValue = analogRead(A0);
  digitalWrite(ledPin, LOW);
  digitalWrite(blueLEDPin, LOW);
  digitalWrite(ledPin, HIGH);
  digitalWrite(blueLEDPin, HIGH);

  //Ultrasonic sensor
  digitalWrite(trigPin, LOW);
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2; 
  Serial.print("Distance: ");
  Serial.println(" cm");

  if ((distance>40&&distance<55)){
    digitalWrite(yellowLEDPin, HIGH);
  else {
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(yellowLEDPin, HIGH);
  if ((distance>40&&distance<55)&&(sensorValue>600 &&sensorValue<900)&&(onOff == LOW)){
    digitalWrite(goLED, HIGH);
    digitalWrite(yellowLEDPin, HIGH);
    digitalWrite(blueLEDPin, HIGH);
    digitalWrite(goLED, LOW);
    digitalWrite(yellowLEDPin, LOW);
    digitalWrite(blueLEDPin, LOW);


The program takes in the lighting of the surroundings using the LDR and then makes the blue LED turn on and off proportional to the intensity of the light. This shows if you need to increase the light in surroundings or if it getting closer to the perfect level. Likewise with the Ultrasonic sensor, it detects the distance from the object right in front and inputs distance data and the yellow led blinks proportionally to the distance from sensor. When button is pressed and conditions are met, the lights stay on for 10 seconds.


Initially, I wanted to use Tarek’s creative switch idea to make sure that the subject is flexing for the picture. However, I realized that it would require a considerable length of wire to obtain a reasonable distance to take a good picture. I also wanted to make the iPhone take the photo itself via voice commands (where a recoding of me sating “hi Siri, take a picture” would be output once the correct conditions are met). However, iOS security regulations and the lack of complexity in the speaker would not allow this function.

Leave a Reply