#include <Stepper.h>

const float GAMMA = 0.7;
const float RL10 = 50;

const int stepsPerRevolution = 200;
const int dirPin = 2;
const int stepPin = 4;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

const int switchPin = 8;
int switchState = 0;
int count=0;



void setup() {
  Serial.begin(115200);

 // set the speed at 60 rpm:
  myStepper.setSpeed(60);

  pinMode(stepPin, OUTPUT);
	pinMode(dirPin, OUTPUT);

  pinMode(switchPin, INPUT);
  
}

void rolldown(){
 // Set motor direction clockwise
	digitalWrite(dirPin, HIGH);

	for(int x = 0; x < stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(1000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(1000);
	}
	delay(1000); 

}

void rollup(){
  // Set motor direction counterclockwise
	digitalWrite(dirPin, LOW);

	for(int x = 0; x < stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(1000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(1000);
	}
	delay(1000);
}


void loop() {

  switchState = digitalRead(switchPin);

  if (switchState == HIGH ){ // library is open, switch is on the right
// Convert the analog value into lux value:
 int analogValue = analogRead(A0);
  float voltage = analogValue / 1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));

Serial.println(resistance);
delay (5000);

if (lux >= 800  && count<4) {
    Serial.println("Direct Sunlight");
    rolldown();
    count++;
    rolldown();
    count++;
    rolldown();
    count++;
    rolldown();
    count++;
  } else if (lux < 800 && count < 4){
    Serial.println("No Direct Sunlight");
    rollup();
    count++;
    rollup();
    count++;
    rollup();
    count++;
    rollup();
    count++;
  }
  } else if (switchState == LOW && count < 4 ){// library is closed, switch is on the left
   rolldown();
   count++;
   rolldown();
   count++;
   rolldown();
   count++;
   rolldown();
   count++;
    delay(5000);
  }
  
}
A4988