/* 
--------------------------------------------  
-------- Begin Initialise Variables --------
--------------------------------------------
*/

//initialise photoresistor input pin
const int photoResPin = A0;

const int laser_pin = 2;

//number of holes in zoetrope
const int numHole = 16;

//initialise variable to hold photoresistor reading
int LDRval = 0;
int LDRmax = 0;
int LDRmin = 1023;

//initialise variable to hold previous photoresistor reading
int LDRvalPrevious = 0;

//intialise Threshold
int lightThreshold = 300;

// set number of hall trips for RPM reading (higher improves accuracy)
int threshCross= 16;

// set threshold cross counter
int crossCount = 0;

// variables for timing
unsigned long startTime = 0;
unsigned long checkTime = 0;
float timePassed = 0.0;

//initialise variable for measured rpm
float rpmVal = 0;

//define pin for transistor connection to motor power
const int motor_pin = 11;

//define variable for motor demand (0-255), 8-bit PWM. Start on at 50% demand.
int motorDemand = 125;

/* 
--------------------------------------------  
-------- End Initialise Variables ----------
--------------------------------------------
*/


/* 
----------------------------------  
-------- Begin Setup Loop --------
----------------------------------
*/

void setup() 
{

  // initialize serial communication at 115200 bits per second.
  Serial.begin(9600);

  //set motor speed pin to output
  pinMode(TIP120pin, OUTPUT);

  pinMode(laser, OUTPUT);

  digitalWrite(laser, HIGH);
  
  analogWrite(TIP120pin, motorDemand);
  delay(1000);
  initialise_LDR();
  lightThreshold = LDRmin +200;
}

/* 
-------------------------------
------- End Setup Loop --------
-------------------------------
*/

/* 
----------------------------------  
-------- Begin Main Loop ---------
----------------------------------
*/ 

void loop() 
{
	
  
  //reinitialise crossing counter
  crossCount = 0;

  //populate variable with number of microseconds since the program began.
  startTime = micros();
    
  // counting number of times the photoresistor threshold is crossed
  do {

    //read photoresistor
    LDRval = analogRead(photoResPin);
    //Serial.println(LDRval);
    
    //if light threshold is crossed, increment cross count
    if((LDRval >= lightThreshold) && (LDRvalPrevious < lightThreshold))
    {
      //increment cross count
      crossCount = crossCount +1;
	  Serial.print(" "); Serial.print(crossCount);
    }

    //update previous LDRval
    LDRvalPrevious = LDRval;
	
   } while (crossCount < numHole);
  
  //populate variable with number of microseconds since the program began.
  checkTime = micros();

  //evaluate elapsed time in seconds
  timePassed = ((checkTime-startTime)/1000000.0);

  //Evaluate zoetrope RPM
  rpmVal = ((crossCount/numHole)/timePassed)*60.0;

  //Call user defined function to display values to user
  printValues();


   
  // delay in between reads for stability
  delay(1);    
}

/* 
----------------------------------  
--------- End Main Loop ----------
----------------------------------
*/ 



/* 
----------------------------------------------------------------------------- 
--------- Declare User Defined Functions to be called by Main Loop ----------
-----------------------------------------------------------------------------
*/ 

//User defined function to print values to serial
void printValues()
{
  Serial.print("Time Passed: ");
  Serial.print(timePassed);
  Serial.println("s");
  Serial.print(rpmVal);
  Serial.println(" RPM");
  Serial.println(motorDemand);
}


void initialise_LDR()
{

	for (int i=1; i < 1000; i++) {
		LDRval = analogRead(photoResPin);
		if (LDRval < LDRmin) {
		LDRmin = LDRval;
		}
	delay(1);
  }
  Serial.println("LDR Min");
  Serial.println(LDRmin);
}


/* 
--------------------------------------------------------------------------------- 
--------- End Declare User Defined Functions to be called by Main Loop ----------
---------------------------------------------------------------------------------
*/ 
NOCOMNCVCCGNDINLED1PWRRelay Module