/*
iPhone Automatic Arduino Scanner. This program takes inputs from two LDRs.
The value of the LDRs determine if they were covered. If they are covered,
the Arduino sends a signal to the iPhone to take a picture.

by

Dr. Mohamed Qasem
*/

 

int photoResistorPin0 = 0;
int photoResistorPin1 = 1;
int takePicturePin = 2;
int redDiodePin = 13;
int yellowDiodePin = 12;
int lightThreshold = 70;
unsigned long waitBeforeScan = 3000;

int calibration0, calibration1;
boolean picWasTaken;
boolean shouldTakePic;
unsigned long startTime;

void setup() 
{
  Serial.begin(9600);
    // Calibrate the light level, and initialize everything
  calibration0 = analogRead(photoResistorPin0);
  calibration1 = analogRead(photoResistorPin1);
  picWasTaken = false;
  shouldTakePic = false;
  pinMode(takePicturePin, OUTPUT);
  pinMode(redDiodePin, OUTPUT);
  pinMode(yellowDiodePin, OUTPUT);
  startTime = 0;
  
}

void loop() 
{
    // Read the photo resistor values.
  int lightLevel0 = analogRead(photoResistorPin0);
  int lightLevel1 = analogRead(photoResistorPin1);
    // set them to the range between 0 and 100
  lightLevel0 = map(lightLevel0, 0, calibration0, 0, 100);
  lightLevel1 = map(lightLevel1, 0, calibration1, 0, 100);

    // If the both photo resistors have been covered, then we need to start timing before we take the picture.
    // The timer is there to get a change for the user to release the paper and adjust it if need be.
  if (lightLevel0 <= lightThreshold && lightLevel1 <= lightThreshold && shouldTakePic == false && picWasTaken == false)
  {
    startTime = millis();  // start the timer
    shouldTakePic = true;  // Yes we should take a picture
    analogWrite(yellowDiodePin, 255);  // turn on the yellow LED
    analogWrite(redDiodePin, 0);  // turn off the red LED
  }
    // if we should be taking the picture and the the proper amount of time has elpased
  if (shouldTakePic == true && millis() - startTime > waitBeforeScan)
  {
    analogWrite(takePicturePin, 255);  // Take the picture
    picWasTaken = true;  // We have taken the picture.
    delay(200); // wait a couple of seconds (I did this because the relay is slow. I gave it time to react).
    analogWrite(takePicturePin, 0);  // stop taking the picture
    shouldTakePic = false;  // We shouldn't be taking anymore pictures untill the paper is removed.
    analogWrite(yellowDiodePin, 0);  // Turn off the yellow LED.
  }

    // Check to see if both photo resistors have been uncovered.
  if (lightLevel0 > lightThreshold && lightLevel1 > lightThreshold)
  {
    shouldTakePic = false;  // don't take a picture
    picWasTaken = false;    // we haven't taken a picture
    analogWrite(yellowDiodePin, 0);  // turn off the yellow LED
    analogWrite(redDiodePin, 0);    // turn off the red LED
  }
    // If either of the LED is on and the other off, the trun on the red light and cancel taking picture.
  if ((lightLevel0 > lightThreshold && lightLevel1 <= lightThreshold) || (lightLevel0 <= lightThreshold && lightLevel1 > lightThreshold))
  {
    analogWrite(redDiodePin, 255);
    analogWrite(yellowDiodePin, 0);
    shouldTakePic = false;
  }
  
}