Hi guys I'm a beginner at arduino and I'm trying to control a solar panel using a stepper motor a nema 23 to be more specific, using a microstep driver (DQ54MA), and I just can't get the software right, can you please tell me what's wrong with it, and don't judge it because this is my first project using an arduino.
Thanks alot,,
this is my code
const int LDR1 = A0;
const int LDR2 = A1;
const int PUL = 3;
const int DIR = 2;
const int tol =20;
const int EN= 4;
void setup() {
pinMode(PUL, OUTPUT);
pinMode(DIR, OUTPUT);
pinMode(EN,OUTPUT);
}
void loop() {
int x1 = analogRead(LDR1);
int x2 = analogRead(LDR2);
int xd = x1 - x2;
if (abs(x1 - x2) <= tol) {
TurnMotorOFF();
}
else {
if (x1 < x2) {
TurnMotorCW();
}
if (x2 < x1) {
TurnMotorCCW();
}
}
delay(1000);
}
void TurnMotorCW() {
digitalWrite(EN,LOW);
digitalWrite(DIR, LOW);
digitalWrite (PUL, HIGH);
delayMicroseconds(2000);
digitalWrite(PUL, LOW);
delayMicroseconds(2000);
}
void TurnMotorCCW() {
digitalWrite(EN,LOW);
digitalWrite(DIR, HIGH);
digitalWrite (PUL, HIGH);
delayMicroseconds(2000);
digitalWrite(PUL, LOW);
delayMicroseconds(2000);
}
void TurnMotorOFF() {
digitalWrite(EN,HIGH);
}