Hello i am now trying to control stepper motor using LDR sensor. im using NEMA 17 Stepper motor with A4988 motor driver. when i put code for LDR module, in serial monitor just show 1 data and not looping. when i using L298N, is not problem. i try to change from serial.print and delay but still not working. does anyone know the problem is it? this is the code :
const int stepPin = 4;
const int dirPin = 5;
int trig_pin = 2;
int echo_pin = 3;
long echotime;
float distance;
void setup() {
Serial.begin(9600);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
digitalWrite(trig_pin, LOW);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
}
void loop() {
//For Ultrasonic Sensor
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
echotime = pulseIn(echo_pin, HIGH);
distance= 0.0001*((float)echotime*340.0)/2.0;
Serial.print(distance);
Serial.println(" cm");
delay(3600);
//for LDR
unsigned int AnalogValue;
AnalogValue = analogRead(A2);
delayMicroseconds(10);
Serial.println(AnalogValue);
//for Stepper
digitalWrite(dirPin, HIGH);
for (int x = A2; x < 400; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(dirPin, LOW);
for (int x = A2; x > 400; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(dirPin, HIGH);
for (int x = A2; x = 400; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
}