Hello,
I am writing a program for my school project. I build a device with 2 steppermotors. steppermotor 1 turns an object. steppermotor 2 is driving a spindle.
I have the following code. The code is working now but we need to include a IR sensor on one side of the spindel. every time i push the button, the program must check if the spindel stays on the "zero" position before the program starts. if it is not on the zero position, the spindel needs to move until it reaches the zero position and after that it has to start the program.
#define DIR_PIN 2 //2 + poort op arduino
#define STEP_PIN 3 //3 = poort op arduino
#define DIR_PIN1 4
#define STEP_PIN1 5
const int BUTTON = 8;
int buttonState = 0;
void setup() {
Serial.begin(9600);
digitalWrite(BUTTON, HIGH);
pinMode(BUTTON, INPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN1, OUTPUT);
pinMode(STEP_PIN1, OUTPUT);
}
void loop(){
buttonState = digitalRead(BUTTON);
if(buttonState == HIGH)
{
//// //rotate a specific number of degrees
// rotateDeg(3, .1);
// delay(1000);
//
// rotateDeg(-3, .1); //reverse
// delay(1000);
//rotate a specific number of microsteps (8 microsteps per step)
//a 200 step stepper would take 1600 micro steps for one full revolution
rotate1(10000, .05); // 10000 aantal micro stappen 1/32 , 0.05 snelheid
delay(2000);
rotate2(18000, .2);
delay(2000);
rotate1(10000, .05);
delay(10000);
rotate2(-18000, .2);
}
else { }
}
void rotate2(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);
digitalWrite(DIR_PIN1,dir);
float usDelay = (1/speed) * 150; //* groter getal is langzamer
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN1, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN1, LOW);
delayMicroseconds(usDelay);
}
}
void rotate1(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);
digitalWrite(DIR_PIN,dir);
float usDelay = (1/speed) * 70;
for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(usDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}
thanks in advance