// lines between /* and */ are one large comment block // anything after // is a comment, either commenting on what the line does or to comment out debug lines like // print (print but no new line abd println which adds cr // all comments are removed during compile so do not affect speed or memory so after running comment out the dep=but lines, but leave in in case needed // so several ways to comment things it can be added at the end of a line to help explain things // there is a difference between '' and "" '' is used for one charecter as in mode "" is used for a string as in the print statements // to compile and upload this you need an "arduino ide" you can down load it and install it. //I will include the IDE version is used on the memory stick with this program /* i need to add debounce to read switch some time if it is a problem There are 2 limit switches. When it hits the back one it reverses direction and goes to the 0 position , 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 only in the cut direction this increases the speed from cut speed to jog speed while pressed if you jog it resets the position to 0 when done jogging. if yo use hte return to back it up it also resets the position to 0. if you refese to far and hit front switch or the counter goes off and 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. all pins are assigned high, and the switches are connected to ground. so if the switch is off it 1 and on if 0. a litle backward but easiest to wire. There are two buss bars one positive 5 v one ground basic wiring, is switches have one side to ground the other to assigned pin. pots are wired so "left" side is ground and "right side is 5 v. the read pot routine does the reading and sets speed in pulses per second. as a ref the motor is 400 steps per rev so 3 pulser per sec is .45 rpm 1.5 rpm is 10 pulses per second. with a 20 thread per inch feed screw .05 feed per rev shoot for 9-13 inches per hour 9 inches per hour divide by .05 inch per rev gives 180 revs per hour 180 rev per hour divide by 60 is 3 rpm 3 rpm times 400 pulses per rev is 1200 pulses per minute 1200 pulses per minute divide by 60 sec per min is 20 pulses per second. pulse reate is in pulses per second as a ref highland park 20 inch saw with 1 hp motor is 9 - 13.5 inches per hour 9 inches per hour / .05 advance per feed is 180 turns per hour or 3 rpm at 400 pulses per rev is 12000 pulses per min or 20 oylses per second 13 is 260 turns per hour or 4.33 rpm is 1733 pulses per min or 29 pulses per second make cut go from 15 to 33 the controller is wired so dir + and pulse + are wired to 5 v dir - and pul - are wired to arduino pins try 5 times that for rapid the speed range can be adjusted in the read pot routine this is a c++ program so a lot of {} this is used to tell the program what is included in a section of code () is most ofter input to a statement often followed by a set of {} all statements end with a ; if you have to debug this later do the following: install arduino ide onto a compter open the ide and open the file with the program on tools button set the comms port usually 4 or 5 depending on how you machine works you can uplug the computer and look at the ports available under tools port then plug it back in the computer will be the new port that was not available in the program delete the // characters from the beginning of the lines that defines serial comms speed and all of the print statements connect a wire to pin 10 on the computer (a thin one i might leave one connected but it will be screwed into the 5 volt buss so just unscrew the end from the 5 volt buss ) connect the usb port, i will leave a cable in the box it is an old format sorry it is so short i will look for a longer one if the wire to pin 10 is in the air or connected to the 5 volt buss the program stops or learn debugger and set some stop points, i have not learned it yet i just got ide 2 and the debugger is new to it without uploading the program you go to tools and open the serial monitor one of the comment out sections is a pause waiting for a switch on outout digital read 10 is the command. 10 defaults to hi, so to run ground d10 WHICH IS THIRD PIN ON THE RIGHT touch the wire to the ground bar for a second, then take it off, enlarge the serial monitor screen to see what is going on when the program is fixed save it add the // comment lines back this wilol let it run faster then use the check buton to double any erors, fix them and resave the program then use the right arrow to load the new program into the computer 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 B is return from cut S is return mode from switch front limit not hit i do switch reading this way the switch is assigned a pin number on the computer then the state of that is stored in a variable to be used for comparison */ // setting up global variables is next they are global if declaired before setup in not global their scope is limited to the function they are created in // i tried to make all global so they could be changed as needed and work in aby area of the code // global variables int driverdir = 3; //direction pin int in integer int driverpul = 2; // pulse pin char mode = 'O'; // mode will be cut jog return or off this is one character field o is off int cutswitch = A2; // pin for cutswitch int cut = 1; // variable for the cutswitch state there may be a better way but this make the code easier to read (i think) int returnswitch = A3; // global variable for the pin for the return switch int ret = 1; // global used to store the state of the return switch int tripped = 1; //variable to say that the rear switch was tripped, need to use it untill back at 0 position int jogswitch = A4; // global for pin jog switch int jog = 1; //global for state of jog switch int rearswitch = 12; // global for rear limit int rear = 1; //global for state of rear switch int frontswitch = 11; // global for front limit int front = 1; //global for state of front int cutpot = A1; // global for cut pot int jogpot = A0; //global for jog pot pin 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 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 // global variables called but still need to do some house keeping for the accellstepper library which declares a bunch of new functions #include // 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); // this sets up the controller with direction and pulse pins. void setup() { //this is run once // Serial.begin(115200); // open the serial port at 9600 bps needed to print lines for debug. pinMode(jogpot, INPUT); // this sets the jog pot as input pinMode(cutpot, INPUT); //this sets the cut pot as input pinMode(cutswitch, INPUT_PULLUP); // this is the cut switch as input but pullup makes it high or 1 pinMode(returnswitch, INPUT_PULLUP); //RETURN switch pinMode(jogswitch, INPUT_PULLUP); //jog pinMode(rearswitch, INPUT_PULLUP); //rear limit pinMode(frontswitch, INPUT_PULLUP); //front limit pinMode(driverpul, OUTPUT); // this sets the drive pin as outpout pinMode(driverdir, OUTPUT); // sets direction pin as output on pinMode(runled, OUTPUT); // same for the run led run led pinMode(retled, OUTPUT); // return led pinMode(offled, OUTPUT); // off led pinMode(10, INPUT_PULLUP); // a pin assigned to help debug. it allows exution to pause so i can read the debug screen. mystepper.setMaxSpeed(4000); // sets max speed // mystepper.setSpeed(10); // a default low speed overwritten but needs to be declared and set } // end of setup void loop() { // this is the loop that runs over and over again // while (digitalRead(10) == HIGH) { }//here to debug comment out when debuged it does nothing but wait untill the pin goes high so grounding the pin pauses readpots(); //reads pots this is a function defined below readswitches(); //reads switches same 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) { // this executes code based on what is stored in mode global case 'O': //off digitalWrite(offled, HIGH); // set the off led (red) on turn others off digitalWrite(runled, LOW); digitalWrite(retled, LOW); // Serial.println(" CASE off mode"); // debug to tell me what mode i am in mystepper.disableOutputs(); // if off this disables output to the controller tripped = 1; // motor is off set rtipped high this is in case we were returning mystepper.setCurrentPosition(0); // set position to 0 break; case 'C': //cut digitalWrite(offled, LOW); digitalWrite(runled, HIGH); digitalWrite(retled, LOW); // Serial.print("CASE Cut Mode speed "); // Serial.println (cutspeed); mystepper.enableOutputs(); // enables outout in case it was set off by the off state mystepper.setSpeed(cutspeed); // sets the cut speed determined during read pots mystepper.runSpeed(); // this tells it to take a step at the set speed // Serial.print("in cut for loop "); // Serial.println("my position is "); // Serial.println(mystepper.currentPosition()); break; // the end of this case case 'J': // jog mode digitalWrite(offled, LOW); // turn off led off digitalWrite(runled, HIGH); // turn run led on digitalWrite(retled, LOW); // turn return led off // Serial.print("CASE jog mode speed "); // in jog mode // Serial.println(jogspeed); mystepper.setSpeed(jogspeed); mystepper.runSpeed(); // take a step mystepper.setCurrentPosition(0); // set cuttent position to 0 so we can return here after the cut break; // end of jog 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); mystepper.runSpeed(); if (mystepper.currentPosition() <= 0) { // this determines of we are already at 0 could be there during one of the 200 steps we are doing mode = 'O'; // set mode to off // Serial.print("in return for loop "); // Serial.print("position is "); // Serial.println(mystepper.currentPosition()); } // the end of the for mystepper.setCurrentPosition(0); // set cuttent position to 0 break; // end of return mode case 'S': // in this case we have return switch set so back up // this is differnt than return mode which is caused by the rear switch in cut but back limit hit digitalWrite(offled, LOW); digitalWrite(runled, LOW); digitalWrite(retled, HIGH); // Serial.print(" in return case S return mode"); mystepper.setSpeed(jogspeed * -1); // the *-1 makes speed negative or backward mystepper.runSpeed(); break; // end of S case default: // default we are in unknown state shut things down it is what happens if the mode is not one of the set cases it should never happen mystepper.disableOutputs(); // disable the controller tripped = 1; // motor is off set rtipped highc // reset tripped digitalWrite(offled, HIGH); digitalWrite(runled, LOW); digitalWrite(retled, LOW); break; // end of default mode } //end of switch mode } // end of loop() void readpots() { // this starts defining a functon to read the pots cutspeed = map((analogRead(cutpot)), 0,1025,33,15 ); // analoRead reads the pot and converts it to digital o to 1025 bits // Serial.print ("cut "); // Serial.println (cutspeed); jogspeed = map((analogRead(jogpot)), 0,1025,3500,700); //set jog speed same logic as cut b // Serial.print ("jog "); // Serial.println (jogspeed); } void readswitches() { // start of reading the switches cut = digitalRead(cutswitch); // assigns the state of the cut swith to the cut variable // Serial.print("cut switch "); // Serial.println(cut); ret = digitalRead(returnswitch); // same for return switch // Serial.print("return switch "); // Serial.println(ret); jog = digitalRead(jogswitch); // and jog switch // Serial.print("jog switch "); // Serial.println(jog); rear = digitalRead(rearswitch); // and rear switch // Serial.print("rear switch "); // Serial.println(rear); front = digitalRead(frontswitch); // and front switch // Serial.print("front switch "); // Serial.println(front); } // end of readswitches function void setmode() { // logic o set the mode // 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; // this is a special case that we went past 0 and hit front switch } // end if (cut == 0 && tripped == 0 && mystepper.currentPosition() <= 0) { // in return mode from tripped but position is at 0 or less 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 this happens untill we set cut switch to off position mode = 'O'; tripped = 1; // reset tripped sonce now home // Serial.println("if #4 cut is on but parkedbyreturn"); } if (ret == 0 && front == 0) { // the return switch is held but 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 so cut the thing 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 start return mode, rear will be 1 soon set tripped if 7 mode = 'R'; tripped = 0; // this says we hit the return swwitch so in return mode untill at home // 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 return more if 8 mode = 'R'; // Serial.println("if #8 cut and tripped but position is not 0"); } if (cut == 0 && tripped == 0 && mystepper.currentPosition() <= 0) { // was cutting hit return switch and have returned to a home // Serial.println(" home from return"); mode = 'O'; parkedbyreturn = 0; // we are home so set parked by home tripped = 1; // reset the tripped variable } if (ret == 0 && front == 1) { mode = 'S'; // ret switch set and front not hit //Serial.print("IN IF FOR RET = 0 but return is "); //Serial.println(ret); } } // end of setmode