I have following code, which move stepper motor to 4 positions, how to modify the code for stepper motor control , make it simpler. i.e. only let code run based on the motor data table:
(use Array??)
Steps Dir Delay(After that position)
Position1 125 HIGH 1000
Position2 225 LOW 1800
Position3 325 HIGH 1200
Position4 208 HIGH 900
Thank!
Current Code:
int pushbuttonpin =7; //Pin for momentary push button switch or sensor
int val = 0; //Integer for reading push button switch status
int dirpin = 3; //pin for DIR
int steppin = 2; //pin for STEP
void setup()
{
Serial.begin(9600);
pinMode(pushbuttonpin, INPUT); //Declare push button switch pin as input
pinMode(dirpin, OUTPUT); //Declare dirpin as output
pinMode(steppin, OUTPUT); //Declare steppin as output
}
void loop()
{
int i;
val = digitalRead(pushbuttonpin); //Read push button switch pin status
if (val == HIGH)
{
digitalWrite(dirpin, HIGH); // Set the direction pin to move forward
delay(500); //Give it some time
for (i = 0; i<125; i++) // Step Forward 125 steps
{
digitalWrite(steppin, LOW); // Start out with step pin low
delayMicroseconds(3000); // Delay controls speed and Torque.
digitalWrite(steppin, HIGH); // Now switch it high
delayMicroseconds(3000); // Delay controls speed and Torque you need at least a 680 delay
}
//----------------------------------------------------------------------------------------------------------
delay(1000); //stop at 1st. position for a while
//----------------------------------------------------------------------------------------------------------
digitalWrite(dirpin, LOW);
delay(500);
for (i = 0; i<225; i++) // Step Backward to position #2
{
digitalWrite(steppin, LOW);
delayMicroseconds(3000);
digitalWrite(steppin, HIGH);
delayMicroseconds(3000);
}
//----------------------------------------------------------------------------------------------------------
delay(1800); //stop at 2nd. position for a while
//----------------------------------------------------------------------------------------------------------
digitalWrite(dirpin, HIGH);
delay(500);
for (i = 0; i<325; i++) // Step Forward to position #3
{
digitalWrite(steppin, LOW);
delayMicroseconds(3000);
digitalWrite(steppin, HIGH);
delayMicroseconds(3000);
}
//----------------------------------------------------------------------------------------------------------
delay(1200); //stop at 3rd. position for a while
//----------------------------------------------------------------------------------------------------------
digitalWrite(dirpin, HIGH);
delay(500);
for (i = 0; i<208; i++) // Step Forward to position #4
{
digitalWrite(steppin, LOW);
delayMicroseconds(3000);
digitalWrite(steppin, HIGH);
delayMicroseconds(3000);
}
//----------------------------------------------------------------------------------------------------------
delay(900); //stop at 4th. position for a while
//----------------------------------------------------------------------------------------------------------
}
}