[英]Control Stepper Motor with A4988 Driver motor Using LDR Sensor
Hello i am now trying to control stepper motor using LDR sensor.您好,我现在正在尝试使用 LDR 传感器控制步进电机。 im using NEMA 17 Stepper motor with A4988 motor driver.
我使用带有 A4988 电机驱动器的 NEMA 17 步进电机。 when i put code for LDR module, in serial monitor just show 1 data and not looping.
当我为 LDR 模块输入代码时,在串行监视器中只显示 1 个数据而不是循环。 when i using L298N, is not problem.
当我使用 L298N 时,没问题。 i try to change from serial.print and delay but still not working.
我尝试从 serial.print 更改并延迟但仍然无法正常工作。 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);
}
for (int x = A2; x = 400; x++)
is an infinite loop. for (int x = A2; x = 400; x++)
是一个无限循环。
x = 400 evaluates to 400, which is a true value so the loop runs forever. x = 400 计算结果为 400,这是一个真值,因此循环永远运行。 I think you should revisit the basics of C++ control structures.
我认为您应该重新审视 C++ 控制结构的基础知识。
Also why do you start at A2
?另外你为什么从
A2
开始? That's a pin number.那是一个密码。 Doesn't make sense to me.
对我来说没有意义。 You probably wanted do use
AnalogValue
您可能想要使用
AnalogValue
for (int x = A2; x > 400; x++)
is another non-sense loop. for (int x = A2; x > 400; x++)
是另一个无意义的循环。 it will only run if A2
is > 400 and then it will run forever.它只会在
A2
> 400 时运行,然后它将永远运行。 But it is unlikely A2 will ever be > 400. So you could as well just remove that loop.但 A2 不太可能 > 400。所以你也可以删除那个循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.