//STAGE 1
//*** DEFINE PINS HERE
const int LDR_pin = A0;
const int Relay_pin = 11;
const int LED_pin = 2;
//*** DEFINE VARIABLES HERE
int LDR_current = 0;
int LDR_previous = 0;
int Motor_command = 0;
unsigned long Start_t = 0;
unsigned long Current_t = 0;
int Slot_counter = 0;
float RPM = 0;
int for_counter = 0;
//*** DEFINE EXP PARAMETERS HERE
int LDR_background = 1023;
int LDR_step = 200;
int LDR_threshold = LDR_background + LDR_step;
//number of slots in zoetrope
int no_slots = 12;
//define variable for motor demand (0-255), 8-bit PWM. Start on at 50% demand.
int motorDemand = 127;
/*
--------------------------------------------
-------- End Initialise Variables ----------
--------------------------------------------
*/
/*
----------------------------------
-------- Begin Setup Loop --------
----------------------------------
*/
void setup() {
// initialize serial communication at 115200 bits per second.
Serial.begin(9600);
//**** STAGE 2a ****
pinMode(LDR_pin, INPUT);
pinMode(Relay_pin, OUTPUT);
pinMode(LED_pin, OUTPUT);
//**** STAGE 2b ****
digitalWrite(LED_pin, HIGH);
Motor_command = 255 / 2;
analogWrite(Relay_pin, Motor_command);
//**** STAGE 2c ***** Find the background LDR value here
find_LDR_background(); //you must write this function - see bottom of code!
}
/*
-------------------------------
------- End Setup Loop --------
-------------------------------
*/
/*
----------------------------------
-------- Begin Main Loop ---------
----------------------------------
*/
void loop() {
// **** STAGE 3 ****
Slot_counter = 0;
Start_t = micros();
// **** STAGE 3b **** MEASURE THE RPM OF THE ZOETROPE
do {
LDR_previous = LDR_current;
LDR_current = analogRead(LDR_pin);
//if light threshold is crossed, increment cross count
if (LDR_current > LDR_threshold && LDR_previous < LDR_threshold) {
Slot_counter = Slot_counter++;
}
//update previous LDRval
LDR_previous = LDR_current;
}
while (Slot_counter < no_slots);
// **** STAGE 4 ****
//Find the end time of the rotation
Current_t = micros();
//Find the elapsed time for 1 rotation
RPM = (Current_t - Start_t) / (1000 * 60);
//
Serial.println(RPM);
// **** STAGE 5 **** Automate the control?!
// delay before starting again...
delay(1000);
}
/*
----------------------------------
--------- End Main Loop ----------
----------------------------------
*/
/*
-----------------------------------------------------------------------------
--------- Declare User Defined Functions to be called by Main Loop ----------
-----------------------------------------------------------------------------
*/
void printOutputs() {
// DEFINE YOUR FUNCTION HERE TO PRINT OUTPUTS - OPTIONAL!
}
void find_LDR_background() {
for (for_counter = 0; for_counter = 500; for_counter++) {
LDR_previous = LDR_current;
LDR_current = analogRead(LDR_pin);
if (LDR_background > LDR_current); {
LDR_background = LDR_current;
}
}
LDR_threshold = LDR_background + LDR_step;
}
/*
---------------------------------------------------------------------------------
--------- End Declare User Defined Functions to be called by Main Loop ----------
---------------------------------------------------------------------------------
*/