/*
ZoeTrope Simulation Example
Mech1010 - Arduino Outputs Lab
Written by Pete Culmer 2023
*/
/*
--------------------------------------------
-------- Begin Initialise Variables --------
--------------------------------------------
*/
//STAGE 1
//*** DEFINE PINS HERE
const int LDR_pin = A0;
const int LED_pin = 2;
const int transistor_pin = 11;
//Add more...
//*** DEFINE VARIABLES HERE
int LDR_current = 0;
int LDR_previous = 0;
int motorcommand = 0;
unsigned long starttime = 0;
unsigned long currenttime = 0;
int slotcounter = 0;
float RPM = 0;
// etc etc
//*** DEFINE EXP PARAMETERS HERE
int slots_number = 11;
int LDR_background = 1023;
int LDR_threshold = 1223;
int LDR_step = 200;
//number of slots in zoetrope
//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 ****
//Configure the Arduino Pins
pinMode(LDR_pin, OUTPUT);
pinMode(LED_pin, OUTPUT);
pinMode(transistor_pin, OUTPUT);
//**** STAGE 2b ****
//Turn things on....
digitalWrite(LED_pin, HIGH);
analogWrite(transistor_pin,127.5);
//**** 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 ****
//initialise your counter
slotcounter = 0;
//find the start time
starttime=micros();
currenttime=micros();
// **** STAGE 3b **** MEASURE THE RPM OF THE ZOETROPE
// ** Start DO WHILE loop here
do{
//read photo resistor
int slotcounter_value=analogRead(A0);
//if light threshold is crossed, increment cross count
if(LDR_current>lowest_value && LDR_previous<lowest_value)
{
slotcounter_value=slotcounter_value+1;
}
//update previous LDRval
}while(slotcounter<11);
// ** End DO WHILE loop here
// Loop while your counter is less than the number of slots on the wheel
// **** STAGE 4 ****
//Find the end time of the rotation
//Find the elapsed time for 1 rotation
//
// **** STAGE 5 **** Automate the control?!
// delay before starting again...
delay(1000);
Serial.print('\n');
}
/*
----------------------------------
--------- End Main Loop ----------
----------------------------------
*/
/*
-----------------------------------------------------------------------------
--------- Declare User Defined Functions to be called by Main Loop ----------
-----------------------------------------------------------------------------
*/
void printOutputs()
{
// DEFINE YOUR FUNCTION HERE TO PRINT OUTPUTS - OPTIONAL!
}
int find_LDR_background()
{
int value;
int lowest_value = 10000000;
for (int i=0; i<500; i++){
value = analogRead(LDR_pin);
if (value < lowest_value) {
lowest_value = value;
}
}???
return lowest_value;
}
/*
---------------------------------------------------------------------------------
--------- End Declare User Defined Functions to be called by Main Loop ----------
---------------------------------------------------------------------------------
*/