const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 100;
const int eastLDR = A0;
const int westLDR = A1;
void setup() {
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
int east = analogRead(eastLDR); //+ calibration;
int west = analogRead(westLDR);
int error = east - west;
if (abs(error) <= 35 ) { // Stop if LDR readings are equal
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, LOW);
}
if (abs(error) > 36 && east > west) { // Move right if east LDR is larger
digitalWrite(dirPin, HIGH);
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
Serial.println(x);
if ( abs(error) <= 35 ){
break;
}
}
}
else if (abs(error) > 36 && east < west) { // Move right if east LDR is larger
digitalWrite(dirPin, LOW);
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
Serial.println(x);
if ( abs(error) <= 35 ){
break;
}
}
}
}