Hi all.
I'm creating a station within an automation cell that will produce branded wooden coasters. Basically a robot arm will place a coaster on a carriage controlled by a typical stepper with a timing belt(position RDR), it will move to the branding position(position BRD), move further to be examined by a vision system(position INS), then return to the original position for the robot to pick it again.
Each position within the station has a photosensor. My code involves the usual declarations, setup with pin modes with a "homing to RDR," instantiation of functions for each of the moves including debounces, then a void loop() that calls the move functions with timed delays in between.
This code functions as it should in terms of movement, but I'd like to control the speed at which the carriage moves from position to position using a potentiometer. I've had success with a POT using a clockwise/counterclockwise pushbuttons project, but haven't had success here. Motor wants to just stay at one very slow speed.
New to C++ and I know I could go the whole Accelstepper or whatever, but the goal is to incorporate the other inputs and outputs that control when the carriage moves into the void loop() so I can keep the code simple.
Also, I haven't incorporated the LS's nor steps in any of this yet. Any advice on how to create "e-stops" would be helpful as well.
Actually, any advice would be awesome.
-Karl
/*
* stepper test
*/
// define physical pins
int driverPUL = 7; //PUL pin
int driverDIR = 6; //DIR pin
int LS_neg = 4; //back limit switches shutoff
int LS_pos = 2; //front limit switch
int RDR = 8; //position that robot can drop and pick part
int BRD = 12; //position for branding iron
int INS = 13; //position for vision system inspection
int spd = A0; //potentiometer to control speed of motor
// variables
int pd = 500; //pulse delay period
void setup(){
//setup whether pin is output or input
pinMode (driverPUL, OUTPUT); //setup kind of pin
pinMode (driverDIR, OUTPUT);
pinMode (LS_neg, INPUT);
pinMode (LS_pos, INPUT);
pinMode (RDR, INPUT);
pinMode (BRD, INPUT);
pinMode (INS, INPUT);
reset_pins(); // reset output pins to low states
//homing procedure of stepper at startup within setup
while (digitalRead(RDR)) { //do this until the switch is activated
digitalWrite(driverDIR, HIGH);
digitalWrite(driverPUL, HIGH);
delay(5); //slow approach to pick drop position
digitalWrite(driverPUL, LOW);
delay(5);
}
while (!digitalRead(RDR)) { //do this until the switch is NOT activated
digitalWrite(driverDIR, LOW);
digitalWrite(driverPUL, HIGH);
delay(10); //slow even more while moving away from switch
digitalWrite(driverPUL, LOW);
delay(10);
}
}
//define all functions
void reset_pins() { //writes function to set outputs to low
digitalWrite(driverPUL, LOW);
digitalWrite(driverDIR, LOW);
}
//function to move to BRD sensor
void go_to_BRD() {
while (digitalRead(BRD)) {
pd = map((analogRead(spd)), 0, 1023, 2000, 50);
digitalWrite(driverDIR, LOW);
digitalWrite(driverPUL, HIGH);
delay(pd);
digitalWrite(driverPUL, LOW);
delay(pd);
}
while (!digitalRead(BRD)) {
digitalWrite(driverDIR, HIGH);
digitalWrite(driverPUL, HIGH);
delay(10);
digitalWrite(driverPUL, LOW);
delay(10);
}
}
//function to move to INS sensor
void go_to_INS() {
while (digitalRead(INS)) {
pd = map((analogRead(spd)), 0, 1023, 2000, 50);
digitalWrite(driverDIR, LOW);
digitalWrite(driverPUL, HIGH);
delay(pd);
digitalWrite(driverPUL, LOW);
delay(pd);
}
while (!digitalRead(INS)) {
digitalWrite(driverDIR, HIGH);
digitalWrite(driverPUL, HIGH);
delay(10);
digitalWrite(driverPUL, LOW);
delay(10);
}
}
//function to move to RDR sensor
void go_to_RDR() {
while (digitalRead(RDR)) {
pd = map((analogRead(spd)), 0, 1023, 2000, 50);
digitalWrite(driverDIR, HIGH);
digitalWrite(driverPUL, HIGH);
delay(pd);
digitalWrite(driverPUL, LOW);
delay(pd);
}
while (!digitalRead(RDR)) {
digitalWrite(driverDIR, LOW);
digitalWrite(driverPUL, HIGH);
delay(10);
digitalWrite(driverPUL, LOW);
delay(10);
}
}
//action sequence
void loop() {
go_to_BRD();
delay(2000);
go_to_INS();
delay(2000);
go_to_RDR();
delay(2000);
}