hi gurus, I'm trying to control the stepper motor(step angle setting 1.8 degree) to rotate back and forth 180 degree at 30rpm, I observed that the motor is not making the full 180 degree move (around 160 degree). Appreciate your suggestion.
#define Pulse 9
#define dir_pin 8
#define home_switch 3
const int STEPS_PER_REV = 200;
void setup() {
pinMode(Pulse,OUTPUT);
pinMode(dir_pin,OUTPUT);
pinMode(home_switch, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Started !");
}
void loop() {
if (Serial.available()) {
String data_from_display = "";
delay(30);
while(Serial.available()) {
data_from_display += char(Serial.read());
}
sendData(data_from_display);
}
}
void Start() {
Serial.println("START invert");
for (int x = 1; x<=30 ; x++)
{
// Set motor direction clockwise
digitalWrite(dir_pin,LOW);
// Spin motor one rotation slowly
for(int i = 0; i < (STEPS_PER_REV/2); i++) {
digitalWrite(Pulse,HIGH);
delayMicroseconds(5000);
digitalWrite(Pulse,LOW);
delayMicroseconds(5000);
}
// Set motor direction counterclockwise
digitalWrite(dir_pin,HIGH);
// Spin motor two rotations quickly
for(int i = 0; i < (STEPS_PER_REV/2); i++) {
digitalWrite(Pulse,HIGH);
delayMicroseconds(5000);
digitalWrite(Pulse,LOW);
delayMicroseconds(5000);
}
Serial.println(x);
if (Serial.available()) {
String data_from_display = "";
//delay(30);
while(Serial.available()) {
data_from_display += char(Serial.read());
}
if (data_from_display=="S")
return;
}
}
}
void Homing() {
while (digitalRead(home_switch))
{ // Do this until the switch is activated
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(Pulse,HIGH);
delayMicroseconds(5000);
digitalWrite(Pulse,LOW);
delayMicroseconds(5000);
}
while (!digitalRead(home_switch))
{ // Do this until the switch is not activated
for (int i =0; i<5; i++)
{
digitalWrite(dir_pin, LOW); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(Pulse,HIGH);
delayMicroseconds(5000);
digitalWrite(Pulse,LOW);
delayMicroseconds(5000);
}
}
}
void sendData(String data_from_display) {
if (data_from_display == "START") {
Start();
}
if (data_from_display == "HOMING") {
Homing();
}
}