Hello Guys,
I'm working on a project if 3d printer base and I have the intention to use a optical sensor as a end position sensor. But the sensor acts as a flag differently depending on the steps of the stepper motor I use.
I'm just using the code below:
General information:
which I can modify the PPR value (how many steps of the motor)
mode = LOW make the motor turn its direction
Code:
for(int x = 0; x < PPR; x++) {
if (Pos==LOW)
{
mode = LOW;
}
digitalWrite(stepPin, HIGH); // Pulso nível baixo
delayMicroseconds (1000); // MeioPeriodo de X microsegundos
digitalWrite(stepPin, LOW); // Pulso nível alto
delayMicroseconds (1000); // MeioPeriodo de X microsegundos
}
I didn't test using interruptions yet.
any ideas on how make sensor acts the same way for all PPR I choose?
Regards,
Bruno
Not going to work right with your delays or the "for" shown in the snippet.
Paul
Idea, sure, use an interrupt from the sensor to set a Boolean to TRUE. Then in your "for" loops, test the Boolean for TRUE and if so, your sensor has sensed. Then turn the Boolen back to FALSE.
Paul
Hey paul,
your suggestion didn't workout, actually I didn't say before but i'm using the A4988 polulu driver and using it in the full step mode, maybe by changing the mode will it help?
Full code below:
an additional information is that I captured commands to change the direction (Pos variable), steps (PPR variable) and turnOnOff.
// defines pins numbers
const int stepPin = 9; // step pin
const int dirPin = 8; //direction pin
const int MS1Pin = 6;
const int MS2Pin = 5;
const int MS3Pin = 4;
volatile boolean mode = HIGH;
int period = 1000;
float PPR=400;
bool turnOnOff;
//optical sensor
const int sensor = 7;
bool Pos;
const byte interruptPin = 2;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,INPUT_PULLUP);
digitalWrite(MS1Pin, LOW);
digitalWrite(MS2Pin, LOW);
digitalWrite(MS3Pin, LOW);
pinMode(sensor,INPUT_PULLUP);
mode = LOW;
Pos = HIGH;
PPR = 400;
turnOnOff=LOW;
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), sensor, CHANGE);
}
void loop() {
if(Serial.available()){
input = Serial.readStringUntil('\n');
if(input.equals("Z")){
turnOnOff = HIGH;
}
if(input.equals("X")){
turnOnOff = LOW;
}
if(input.equals("C")){
Pos = HIGH;
}
if(input.equals("V")){
Pos = LOW;
}
if(input.equals("15")){
PPR = 16.6;
}
if(input.equals("30")){
PPR = 33.34;
}
if(input.equals("60")){
PPR = 66.67;
}
if(input.equals("120")){
PPR = 133.33;
}
if(input.equals("360")){
PPR = 400;
}
if(input.equals("1")){
PPR = 0.9;
}
}
if (turnOnOff == HIGH){
for(int x = 0; x < PPR; x++) {
digitalWrite(stepPin, HIGH); // Pulso nível baixo
delayMicroseconds (period); // MeioPeriodo de X microsegundos
digitalWrite(stepPin, LOW); // Pulso nível alto
delayMicroseconds (period); // MeioPeriodo de X microsegundos
digitalWrite(dirPin, Pos);
}
Serial.print(mode);
Serial.print(Pos);
}
delay(1000);
}
void sensor()
{
mode = !mode;
if (mode == LOW && Pos == HIGH) {
Pos = LOW;
}
}
regards
Sure not going to work during a delay.
Paul
I solved the problem with the code below:
// Define the Pins used
#define step_pin 9 // Pin 5 connected to Steps pin on EasyDriver
#define dir_pin 8 // Pin 6 connected to Direction pin
#define home_switch 2 // Pin 9 connected to Home Switch (MicroSwitch)
int direction; // Variable to set Rotation (CW-CCW) of the motor
int steps; // Used to set HOME position after Homing is completed
void setup() {
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(home_switch, INPUT_PULLUP);
delay(5); // Wait for EasyDriver wake up
// Start Homing procedure of Stepper Motor at startup
while (digitalRead(home_switch)) { // Do this until the switch is activated
digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
digitalWrite(step_pin, HIGH);
delay(5); // Delay to slow down speed of Stepper
digitalWrite(step_pin, LOW);
delay(5);
}
while (!digitalRead(home_switch)) { // Do this until the switch is not activated
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(10); // More delay to slow even more while moving away from switch
digitalWrite(step_pin, LOW);
delay(10);
}
steps=0; // Reset position variable to zero
Serial.begin(9600);
}
void loop() {
if (steps < 6000) { // Maximum steps the stepper can move away from the Home Position
digitalWrite(dir_pin, LOW);
digitalWrite(step_pin, HIGH);
delay(1);
digitalWrite(step_pin, LOW);
delay(1);
steps++; // Increase the number of steps taken
}
Serial.println(steps);
}