hi all
im using a nema 17 motor and im facing a problem that i dont understand what is happening, im trying to achive de motor to have two buttons switch 1 to move counter clockwise other clockwise and then to convert that into degrees 0 to 360 degree output serial monitor
this the code im using
#define MotorDir 6
#define MotorStep 7
#define ButaoA A1
#define ButaoB A0
int msDelay = 5
int ButaoEstadoA = 0;
int ButaoEstadoB = 0;
int count = 0;
int n;
void setup() {
Serial.begin(9600);
pinMode(MotorDir, OUTPUT);
pinMode(MotorStep, OUTPUT);
pinMode(ButaoA, INPUT);
pinMode(ButaoB, INPUT);
}
void loop() {
int degree = (count * 1.8) ;
ButaoEstadoA = digitalRead(ButaoA);
ButaoEstadoB = digitalRead(ButaoB);
Serial.print(" count=");
Serial.print(count);
Serial.print(" graus=");
Serial.println(degree);
if (ButaoEstadoA == HIGH)
{
digitalWrite(MotorDir, HIGH);
for ( n = 0; n <= 1; n++)
{
digitalWrite(MotorStep, HIGH);
delay(msDelay);
digitalWrite(MotorStep, LOW);
delay(msDelay);// faz um delay
if (count >= 200) {
count = 0;
degree = 0;
}
count = count + n;
}
}
else if (ButaoEstadoA == LOW) {
{
digitalWrite(MotorStep, LOW);
}
if (ButaoEstadoB == HIGH)
{
digitalWrite(MotorDir, LOW);
for (n = 0; n <= 1; n++)
{
digitalWrite(MotorStep, HIGH);
delay(msDelay);// faz um delay
digitalWrite(MotorStep, LOW);
delay(msDelay);
if (count >= -1) {
// how to do it?
}
count = count - n;
}
}
else if (ButaoEstadoB == LOW)
{
digitalWrite(MotorStep, LOW);
}
}
}
at this moment is counting down when goes below 0 degree counter clockwise i just cant figuer out how to convert that into degrees 360.359,358 and so on...
another problem im facing is the revolutions count it seems that the motor is moving 100 steps count to make a 360 degree, when it should be 200 steps, i have tryed a simpler code without buttons etc.. and makes 200 step to do the 360 degree..