Stepper motor(clockwise,counter-clockwise)

Hi, I’m trying to write program witch will change direction of stepper motor spin, depends on light intensity.
Unfortunately it doesn’t work when im using "if"conditional. Anyone can help?I include my code below:

#include <Stepper.h>

int sensorL = 0, sensorP = 0; 

const int stepsPerRvolution = 64;
Stepper myStepper(stepsPerRvolution,4,5,6,7);

void setup() {
 // put your setup code here, to run once:
 
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
digitalWrite(8,LOW);
digitalWrite(9,LOW);

myStepper.setSpeed(60);

Serial.begin(9600);

}

void loop() {
 // put your main code here, to run repeatedly:

sensorL = analogRead(A0);   
sensorP = analogRead(A1);    

Serial.println(sensorL);
Serial.println(sensorP);

 if(sensorL > sensorP)
 {
   digitalWrite(9,HIGH);
   digitalWrite(8,LOW);
   Serial.println("clockwise");
   myStepper.step(stepsPerRvolution);
   delay(1);
 }
 else if(sensorL < sensorP)
 {
   digitalWrite(8,HIGH);
   digitalWrite(9,LOW);
   Serial.println("counterclockise");
   myStepper.step(-stepsPerRvolution);
   delay(1);
 }
 else
 {
   digitalWrite(8,LOW);
   digitalWrite(9,LOW);
   delay(1);
 }
}

Have you tried printing the values that you are testing with the ifs ? Are they what you expect ?

Is the code entering the code blocks after the ifs ? Put some Serial.print()s in there to fund out ?

What do pins 8 and 9 do ?

Hi,
Welcome to the forum.

Have you got your code to drive the stepper back and forward without the sensor in the code?

Tom… :slight_smile: