const int lightAnalog = A0;
const int stepPin = 4;
const int dirPin = 2;
int globalSteps = 0;
int iteration = 0, lastTimeCondition = -1;
void turnOn(){
digitalWrite(dirPin, HIGH);
for(int steps = 0; steps < 1200; steps++){
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
globalSteps++;
delayMicroseconds(500);
}
}
void turnOff(){
digitalWrite(dirPin, LOW);
for(int steps = 0; steps < 1200; steps++){
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
globalSteps--;
delayMicroseconds(500);
}
}
/*
Program a void function called “turnon()”. The
purpose of this function is to turn on the gate
valve by having the stepper motor rotate clockwise
for 6 rotations. Then, program a loop of pulses
such that 6 rotations are made. (Note: There must
be a delay between setting the STEP pin to go HIGH
and LOW. The duration of this delay also controls
the speed of the rotation. Any speed setting is
acceptable for this project.) (4 pts)
*/
void sensorOperation(float resistance, float *lux, int *timeCondition){
/*
Let the threshold for full daylight be around 10000 lux. If the value of
lux is above or equal to 10000, then that means that the sensor is exposed
to full daylight. In this case, print “Full Daylight” on the serial monitor.
If the value of lux is below 800, then the sensor is not exposed to full
daylight. In this case, print “No Full Daylight” in the serial monitor. (3 pts)
*/
if(resistance >= 1250000.0){
*lux = 0.1;
}
else if(resistance >= 250000.0 && resistance < 1250000.0){
*lux = 1.0;
}else if(resistance >= 50000.0 && resistance <250000.0){
*lux = 10.0;
}else if(resistance >= 9980.0 && resistance < 50000.0){
*lux = 100.0;
}else if(resistance >= 1990.0 && resistance < 9980.0){
*lux = 1000.0;
}else if(resistance >= 397.0 && resistance < 1990.0){
*lux = 10000.0;
}else if(resistance >= 79.0 && resistance < 397.0){
*lux = 100000.0;
}
if(resistance <= 397.0){
*timeCondition = 1;
}else if(resistance > 3700.0){
*timeCondition = 0;
}else{
*timeCondition = -1;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
}
void loop() {
float rawvoltage, voltage, lux;
int timeCondition, toggleCondition;
//
//digitalWrite(dirPin, HIGH);
delay(1000);
rawvoltage = analogRead(lightAnalog);
voltage = rawvoltage * (5.0 / 1023.0);
//Program the Arduino to determine the analog voltage from the LDR sensor (2 pts).
float resistance = 2000 * voltage / (1 - voltage / 5);
sensorOperation(resistance, &lux, &timeCondition);
//Then, convert the analog voltage to its resistance by following the equation below (2 pts):
Serial.print("Resistance: (");
Serial.print(resistance);
Serial.print(") Voltage: (");
Serial.print(voltage);
Serial.print(") Lux: ");
Serial.print(lux);
if(timeCondition != lastTimeCondition){
iteration = 0;
lastTimeCondition = timeCondition;
}
if(timeCondition == 1){
Serial.print(" Full Daylight! ");
Serial.print(iteration);
Serial.print(" Steps ");
Serial.print(globalSteps);
if(iteration == 0 && globalSteps != 1200){
turnOn();
}
iteration++;
}else if(timeCondition == 0){
Serial.print(" No Full Daylight! ");
Serial.print(iteration);
Serial.print(" Steps ");
Serial.print(globalSteps);
if(iteration == 0 && globalSteps == 1200){
turnOff();
}
iteration++;
}else if(timeCondition == -1){
Serial.print("");
iteration++;
}
Serial.println();
//Continuously sample the LDR sensor module by getting the resistance value every 5 seconds.
//Print the resistance value of the LDR sensor module in the serial monitor every time it is sampled. (2 pts)
// put your main code here, to run repeatedly:
}