attempt two to post code. This time I "selected all" before copying for the forum. lets see how this is.
/*
i need to add debounce to read switch some time
There are 2 limit switches. When it hits the back one it reverses direction, if it it hits the front it shuts off
There is a one pole 3 position switch (on off on ) that controls the direction. If in cut direction the speed is cut, if return direction the speed is jog. If the switch is off the motor stops.
There are 2 pots to control speed one if cut speed the other is jog speed
There is a jog button that only works in the cut direction this increases the speed from cut speed to jog speed while pressed
I would like use position to set a 0 when starts and when rear limit is hit go back to 0
if front switch is hit it stops
putting the 3 way switch to center stops and resets variables like tripped that is set when rear limit is hit
Basic operation :
1) Move carrage to start position either feed and jog or return (start position)
2) Load rRock.
3) start cut by pressing switch to feed. If needed press jog to move quicker. (start position updated)
4) continue cutting until rear limit switch is hit then reverse direction to start position then stop
note to keep track that rear switch has been set update a called tripped because as you move off of it it will go to low.
pin assignment
Ao is jog pot
A1 is cut pot
A2 cut switch
A3 return switch
A4 jog switch
12 rear limit
11 Front limit
3 direction
2 pulse
4 cut led (green)
5 ret led (yellow)
6 off led (red)
added pause to help debug
added all of the print statements to help debug, before switched to ide ver 2 that has a debug mode
remove all of them when it is working
note on modes
O is off
C is cut
J is Jog
R is return from cut
S is return mode from switch setting
end of comment
setting up global variables is next they are global if declaired before setup
*/
int driverdir = 3; //direction pin
int driverpul = 2; // pulsepin
char mode = 'O'; // mode will be cut jog return or off
int cutswitch = A2; // global variable for cut switch
int cut = 1; //cut switch reading
int returnswitch = A3; // global variable for return switch
int ret = 1; // global for return
int jogswitch = A4; // global for jog switch
int jog = 1; //global for jog switch
int rearswitch = 12; // global for rear limit
int rear = 1; //global for rear
int frontswitch = 11; // global for front limit
int front = 1; //global for front
int cutpot = A0; // global for cut pot
int jogpot = A1; //global for jog pot
int cutspeed = 5; //global for cut speed set to five untill pots working
int jogspeed = 300; //global variable for jog speed change to non integer
int tripped = 1; //variable to store tripped rear limit switch default to on same as switches defaulted hign
int parkedbyreturn = 1; // a new variable to say parked by returning
int runled = 4; // led to show in run
int retled = 5; // led to show returning
int offled = 6; // led to show off
int temp = 0; // a global variable to calculate speed
#include <AccelStepper.h> // this is a library that helps with stepper motors
int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// initialize the stepper library driver mode pins 2 and 3
AccelStepper mystepper(1, driverpul, driverdir);
void setup() {
Serial.begin(115200); // open the serial port at 9600 bps
pinMode(jogpot, INPUT); //jog pot
pinMode(cutpot, INPUT); //cut pot
pinMode(cutswitch, INPUT_PULLUP); //CUT
pinMode(returnswitch, INPUT_PULLUP); //RETURN
pinMode(jogswitch, INPUT_PULLUP); //jog
pinMode(rearswitch, INPUT_PULLUP); //rear limit
pinMode(frontswitch, INPUT_PULLUP); //front limit
pinMode(driverpul, OUTPUT); //pulse
pinMode(driverdir, OUTPUT); //direction
pinMode(runled, OUTPUT); // run led
pinMode(retled, OUTPUT); // return led
pinMode(offled, OUTPUT); // off led
pinMode(10, INPUT_PULLUP); // SWITCH TO ALLOW PASE TO HELP DEBUT
mystepper.setMaxSpeed(4000);
mystepper.setSpeed(40); // a default low speed
}
void loop() {
while (digitalRead(10) == HIGH) { // set here to debug comment out when debuged
} // end of while for pause
readpots(); //reads pots
readswitches(); //reads switches
setmode(); //determines the mode
Serial.print("mode is ");
Serial.println(mode);
Serial.print("tripped is ");
Serial.println(tripped);
Serial.print("parked by return is ");
Serial.println(parkedbyreturn);
switch (mode) {
case 'O':
digitalWrite(offled, HIGH);
digitalWrite(runled, LOW);
digitalWrite(retled, LOW);
Serial.print(" CASE off mode");
mystepper.disableOutputs();
tripped = 1; // motor is off set rtipped high
mystepper.setCurrentPosition(0); // set position to 0
break;
case 'C':
digitalWrite(offled, LOW);
digitalWrite(runled, HIGH);
digitalWrite(retled, LOW);
Serial.print("CASE Cut Mode");
mystepper.enableOutputs();
mystepper.setSpeed(cutspeed);
for (int i = 0; i <= 200; i++) { //take half a turn
mystepper.runSpeed();
Serial.print("in cut for loop");
Serial.println("my position is");
Serial.println(mystepper.currentPosition());
}
break;
case 'J':
digitalWrite(offled, LOW);
digitalWrite(runled, HIGH);
digitalWrite(retled, LOW);
Serial.print("CASE jog mode"); // in jog mode
mystepper.setSpeed(jogspeed);
for (int i = 0; i <= 200; i++) { //take half a turns
Serial.print("in jog for counter is ");
Serial.println(i);
mystepper.runSpeed();
} //take a step do it 200 times
mystepper.setCurrentPosition(0); // set cuttent position to 0
break;
case 'R': //this is return during a cut
digitalWrite(offled, LOW);
digitalWrite(runled, LOW);
digitalWrite(retled, HIGH);
Serial.print("CASE return mode");
mystepper.setSpeed(jogspeed * -1);
for (int i = 0; i <= 200; i++) { //take half a turn
mystepper.runSpeed();
if (mystepper.currentPosition() <= 0) {
i = 200;
mode = 'O';
}
Serial.print("in return for loop ");
Serial.print("position is ");
Serial.println(mystepper.currentPosition());
};
mystepper.setCurrentPosition(0); // set cuttent position to 0
break;
case 'S': // in this case we ahave return switch set so back up
digitalWrite(offled, LOW);
digitalWrite(runled, LOW);
digitalWrite(retled, HIGH);
Serial.print(" in return case S return mode");
mystepper.setSpeed(jogspeed * -1);
for (int i = 0; i <= 200; i++) { //take half a turn
mystepper.runSpeed();
}
break;
default: // default we are in unknown state shut things down
mystepper.disableOutputs();
tripped = 1; // motor is off set rtipped high
digitalWrite(offled, HIGH);
digitalWrite(runled, LOW);
digitalWrite(retled, LOW);
break;
} //end of switch mode
} // end of loop()
void readpots() {
Serial.println(analogRead(A1));
cutspeed = map((analogRead(A1)), 0, 1025, 0, 20); // set cut speed
Serial.print("cut speed is steps per sec ");
Serial.println(cutspeed);
Serial.print("or inches per min ");
temp = cutspeed * 60 / 400;
Serial.println(temp);
jogspeed = map((analogRead(A0)), 0, 1025, 0, 2000); //set jog speed
Serial.print("Jog speed is steps per sec ");
Serial.println(jogspeed);
Serial.print("or this many inches per min");
temp = jogspeed * 60 / 400;
Serial.println(temp);
Serial.print("current position ");
Serial.println(mystepper.currentPosition());
}
void readswitches() {
cut = digitalRead(cutswitch);
Serial.print("cut switch ");
Serial.println(cut);
ret = digitalRead(returnswitch);
Serial.print("return switch ");
Serial.println(ret);
jog = digitalRead(jogswitch);
Serial.print("jog switch ");
Serial.println(jog);
rear = digitalRead(rearswitch);
Serial.print("rear switch ");
Serial.println(rear);
front = digitalRead(frontswitch);
Serial.print("front switch ");
Serial.println(front);
}
void setmode() {
// note pins are high by default same for tripped and parked
if (cut == 1 && ret == 1) { //cut and return switches are off defaulted high clear settings
mode = 'O'; // set mode to off
parkedbyreturn = 1; // reset parked by return
tripped = 1; //remember default to high do this even if tripped or parked
Serial.println(" if #2 cut and return are off");
}
if (cut == 0 && tripped == 0 && front == 0) { // was in return mode but hit front switch
mode = 'O';
parkedbyreturn = 0;
}
if (cut == 0 && tripped == 0 && mystepper.currentPosition() <= 0) { // in return mode from tripped but position is home if 3
mode = 'O';
tripped = 1; // reset tripped
parkedbyreturn = 0; // set parked
Serial.println("if #3 cut and return tripped with position home");
}
if (cut == 0 && parkedbyreturn == 0) { //cut switch is on but have been parked so stop if 4
mode = 'O';
tripped = 1; // reset tripped sonce now home
Serial.println("if #4 cut is on but parkedbyreturn");
}
if (ret = 0 && front == 0) { //in return mode but hit front limit
mode = 'O';
Serial.println("was returning but hit front switch");
}
if (cut == 0 && tripped == 1 && parkedbyreturn == 1) { // in cut mode but not tripped yet and parked has been reset if 5
mode = 'C';
Serial.println("if #5 cut on but not tripped or parked");
}
if (cut == 0 && jog == 0 && tripped == 1 && parkedbyreturn == 1) { // in cut mode, and jogg pressed but not tripped, not parkedif 6
mode = 'J';
Serial.println("if #6 cut and jog npot tripped not parked");
}
if (cut == 0 && rear == 0) { // in cut mode but hit rear stop staqrt return mode, set tripped if 7
mode = 'R';
tripped = 0;
Serial.println("if #7 cut and just hit rear limit");
}
if (cut == 0 && tripped == 0 && mystepper.currentPosition() > 0) { // you were cutting but now returning but not at 0 so step again if 8
mode = 'R';
Serial.println("if #8 cut and tripped but position is not 0");
}
if (cut == 0 && tripped == 0 && mystepper.currentPosition() <= 0) {
Serial.println("hit 0 pn return");
mode = 'O';
parkedbyreturn = 0;
tripped = 1;
}
if (ret == 0 && front == 1) mode = 'S'; // ret switch set and front not hit
} // end of setmode
it looks better,