I have:
2 x stepper motors
2 x stepper motor drivers
1 x 36V PSU
1 x 24V PSU
1 x 5V PSU
2 x limit switches
1 x proximity sensor
1 x packaging machine
Wiring
36V PSU to 2 stepper motor drivers
5V PSU to Arduino UNO
How I want it to work:
Power on.
[setup]
Stepper motors run simultaneously in the same direction (RIGHT) until limit switch 1 is ON.
Stepper motors run the opposite direction (LEFT) until limit switch 1 is OFF.
Stepper motors continue in that direction (LEFT) until I have a 1cm distance from limit switch 1.
Save position (A)
...Digital Read proximity sensor...
If proximity sensor is ON, then...
Stepper motors run (LEFT) until limit switch 2 is ON.
Stepper motors run the opposite direction (RIGHT) until limit switch 2 is OFF.
Stepper motors continue in that direction (RIGHT) until I have a 1cm distance from limit switch 2.
Save position (B)
[loop]
...Digital Read proximity sensor and pin from packaging machine...
If proximity sensor in ON and packaging machine sends HIGH, then...
Stepper motors run to position A
...Digital Read proximity sensor and pin from packaging machine...
If proximity sensor in ON and packaging machine sends HIGH, then...
Stepper motors run to position B
etc etc etc
The code:
const int pul = 8;
const int dir = 9;
const int en = 10;
const int store = 2; // from packaging machine
const int reset = 3;
const int prox = 4; // proximity sensor Normally HIGH
const int lmt1 = 12; // limit switch 1 Normally HIGH
const int lmt2 = 13; // limit switch 2 Normally HIGH
int x;
int y;
int amax;
int bmax;
int mm;
int steps;
int at;
int h;
int pos;
void setup() {
pinMode (pul, OUTPUT);
pinMode (dir, OUTPUT);
pinMode (en, OUTPUT);
pinMode (store, INPUT);
pinMode (reset, OUTPUT);
pinMode (prox, INPUT_PULLUP);
pinMode (lmt1, INPUT_PULLUP);
pinMode (lmt2, INPUT_PULLUP);
digitalWrite(en, LOW);
x = 500;
y = 1;
digitalWrite(reset, LOW);
while(digitalRead(lmt1)==HIGH){
digitalWrite(dir, HIGH);
digitalWrite(pul, HIGH);
delayMicroseconds(x);
digitalWrite(pul, LOW);
delayMicroseconds(x);
if(x>200 && y==2) {x--; y--; y--;}
y++;
}
while(!digitalRead(lmt1)){
digitalWrite(dir, LOW);
digitalWrite(pul, HIGH);
delayMicroseconds(1000);
digitalWrite(pul, LOW);
delayMicroseconds(1000);
amax = 1;
}
mm = 0;
while(amax==1 && mm < 800){ //800 steps in around 1 cm
digitalWrite(dir, LOW);
digitalWrite(pul, HIGH);
delayMicroseconds(200);
digitalWrite(pul, LOW);
delayMicroseconds(200);
mm++;
}
steps = 0;
at = 0;
h = 0;
while(digitalRead(prox)==HIGH){
}
if(digitalRead(feed)==LOW && at==0 && digitalRead(store)==HIGH){
digitalWrite(reset, HIGH);
digitalWrite(reset, LOW);
x = 500;
y = 1;
while(digitalRead(lmt2)==HIGH){
digitalWrite(dir, LOW);
digitalWrite(pul, HIGH);
delayMicroseconds(x);
digitalWrite(pul, LOW);
steps++;
if(x>70 && y==2) {x--; y--; y--;}
y++;
}
h = 1;
}
while(!digitalRead(lmt2) && h==1){
digitalWrite(dir, HIGH);
digitalWrite(pul, HIGH);
delayMicroseconds(1000);
digitalWrite(pul, LOW);
delayMicroseconds(1000);
steps--;
bmax = 1;
mm = 0;
}
while(bmax==1 && mm < 800){ //800 steps in around 1 cm
digitalWrite(dir, HIGH);
digitalWrite(pul, HIGH);
delayMicroseconds(200);
digitalWrite(pul, LOW);
delayMicroseconds(200);
mm++;
steps--;
}
pos = steps;
at = 1;
}
void loop() {
if(digitalRead(prox)==LOW && at==1 && digitalRead(store)==HIGH ){
digitalWrite(reset, HIGH);
digitalWrite(reset, LOW);
x = 500;
y = 1;
while(steps > 0){
digitalWrite(dir, HIGH);
digitalWrite(pul, HIGH);
delayMicroseconds(x);
digitalWrite(pul, LOW);
steps--;
if(x>70 && y==2) {x--; y--; y--;}
y++;
}
at = 0;
}
if(digitalRead(prox)==LOW && at==0 && digitalRead(store)==HIGH){
digitalWrite(reset, HIGH);
digitalWrite(reset, LOW);
x = 500;
y = 1;
while(steps < pos){
digitalWrite(dir, LOW);
digitalWrite(pul, HIGH);
delayMicroseconds(x);
digitalWrite(pul, LOW);
steps++;
if(x>70 && y==2) {x--; y--; y--;}
y++;
}
at = 1;
}
}
If I just used
if(digitalRead(feed)==LOW){
digitalWrite(reset, HIGH);
digitalWrite(reset, LOW);
the program would work. But I dont have the second signal and the int condition at==0/1 (Im not sure if this int is important, I just put it there in case the previous statement hasnt been done).
I tried this code but, instead of a proximity sensor, I used a push button and I had a 12V supply instead on a 5V supply and it was working fine. I dont know if its because Im on a 5V supply that Im not able to have multiple conditions in a statement???
Maybe a wiring diagram would be helpful but I haven't made one yet. Will be posting it later.
Another question: The limit switches work when I have 1 wire to Arduino GND and the other to a 1K ohm resistor then to digital pin INPUT_PULLUP. When I have 1 wire to 5V and the other directly into digital pin INPUT, it doesn't work and the stepper motors vibrate. I dont understand what is going on.
I have no background in electronics or programming at all. So I might be completely blind of simple mistakes. Please help, Thank you in advance.
EDIT
For some reason, it is working even when I add additional conditions to the IF statement. I didnt change the code at all... (?????) Probably loose contacts? Even though it is working, how can I make the code better and fail proof? The limit switches bounce and I havent debounced them yet.
Concerning the 5V input from the limit switches, that is still a question. Is it correct already that Im pulling up?