Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //include required libraries
- #include <SoftwareSerial.h>
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- //===CONSTANTS and variables (buttons and pins)
- const int buttonPin = 9; // start button
- int startbuttonstate = 0;
- int startup = 0; // has the start button been pressed 1=yes
- int indexed = 0; // is table indexed
- int indexsteps = 0;
- int indexstep;
- const int estopbutton = 8; //estop button
- int estopstate = 0; // are we in a stop state
- const int indexswitch = 7; //table index switch
- const int indexmotor = 6; //table stepper motor
- const int indexdir = 10; //table stepper direction pin
- int indexresult; //indexresult vars: --0=unindexed --1=indexed --2=first stage done --3=second stage done --4=finishing up
- int stationresult = 1; //stationresult vars : --0=not ready --1=get time --2=running
- int station1status; // 0 = ready 1 = good but not done 2 = bad but not done 3 = done
- int station2status; // 0 = ready 1 = good but not done 2 = bad but not done 3 = done
- int stationdone; // 0 = ready 1 = good 2 = failure
- int ser; // serial communication variable
- //===== LED PINS
- const int stn1good = 50; //50
- const int stn1bad = 52; //52
- const int stn2good = 46; //46
- const int stn2bad = 48; //48
- //===== DEBUG BUTTON STUFF
- int tbuttcounter = 0;
- int lastbuttonstate = 0;
- int testcounter = 0;
- int TESTBUTTON = 22;
- int TESTBUTTONSTATE = 0;
- //==== LCD STATUS VARIABLES
- int status1 = 1; // LCD STATUS DISPLAYS 1 = BOOTING 2 = READY 3 = INDEXING 4 = FAILURE 5 = PASS
- int status2 = 1;
- int status3 = 1;
- int status4 = 1;
- int status1old;
- int status2old;
- int status3old;
- int status4old;
- int hstatus = 0; // header
- int mstatus = 0; // Middle status 1= Startup 2=Ready 3=Waiting for STN 4 index stg 1
- int bstatus = 0; // Bottom status
- int hstatusold;
- int mstatusold;
- int bstatusold;
- int feedback; // lcd feedback print
- int feedbackold;
- //===== REFERENCED SETTINGS
- int stepspeed = 100; // Stepper motor step sign in MS
- void setup() {
- //PINOUT BUTTONS/LEDs
- pinMode(indexmotor, OUTPUT);
- pinMode(indexdir, OUTPUT);
- pinMode(estopbutton, INPUT);
- pinMode(indexswitch, INPUT);
- pinMode(buttonPin, INPUT);
- pinMode(TESTBUTTON, INPUT);
- //PINOUT LEDS
- pinMode(stn1good, OUTPUT);
- pinMode(stn1bad, OUTPUT);
- pinMode(stn2good, OUTPUT);
- pinMode(stn2bad, OUTPUT);
- //setup serial communication
- Serial.begin(9600);
- //LCD STARTUP
- lcd.begin(20, 4);
- lcd.setCursor(0, 0);
- lcd.print(" Booting! ");
- lcd.setCursor(0, 1);
- lcd.print("1:B 2:B 3:B 4:B");
- }
- //=====BEGIN FUNCTIONS
- //============================================LCD UPDATE FUNCTION============================
- int LCDUPDATE() {
- if (hstatus != hstatusold) {
- if (hstatus == 1) {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print(" Booting!2 ");
- hstatus = hstatusold;
- }
- if (hstatus == 2) {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print(" Ready For Start ");
- hstatus = hstatusold;
- }
- if (hstatus == 3) {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print(" Indexing Table ");
- hstatus = hstatusold;
- }
- if (hstatus == 4) {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print(" Process Stopped ");
- hstatus = hstatusold;
- }
- if (hstatus == 5) {
- lcd.setCursor(0, 0);
- lcd.print(" ");
- lcd.setCursor(0, 0);
- lcd.print(" Table Indexed ");
- hstatus = hstatusold;
- }
- }//END hstatus UPDATE
- if (mstatus != mstatusold) {
- if (mstatus = 1) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- mstatus = mstatusold;
- }
- if (mstatus == 2) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print("Ready for start");
- mstatus = mstatusold;
- }
- if (mstatus == 3) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print("Waiting for Stations");
- mstatus = mstatusold;
- }
- if (mstatus == 4) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print("Index Stage 1");
- mstatus = mstatusold;
- }
- if (mstatus == 5) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print("Index Stage 2");
- mstatus = mstatusold;
- }
- if (mstatus == 6) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print("Index Stage 3");
- mstatus = mstatusold;
- }
- if (mstatus == 7) {
- lcd.setCursor(0, 2);
- lcd.print(" ");
- lcd.setCursor(0, 2);
- lcd.print("Index Completed");
- mstatus = mstatusold;
- }
- }//END mstatus UPDATE
- if (status1 != status1old) {
- if (status1 == 1) {
- lcd.setCursor(2, 1);
- lcd.print("R");
- status1 = status1old;
- }
- if (status1 == 2) {
- lcd.setCursor(2, 1);
- lcd.print("P");
- status1 = status1old;
- }
- if (status1 == 3) {
- lcd.setCursor(2, 1);
- lcd.print("F");
- status1 = status1old;
- }
- if (status1 == 4) {
- lcd.setCursor(2, 1);
- lcd.print("T");
- status1 = status1old;
- }
- }//END STATUS1 UPDATE
- if (status2 != status2old) {
- if (status2 == 1) {
- lcd.setCursor(6, 1);
- lcd.print("R");
- status2 = status2old;
- }
- if (status2 == 2) {
- lcd.setCursor(6, 1);
- lcd.print("P");
- status2 = status2old;
- }
- if (status2 == 3) {
- lcd.setCursor(6, 1);
- lcd.print("F");
- status2 = status2old;
- }
- if (status2 == 4) {
- lcd.setCursor(6, 1);
- lcd.print("T");
- status2 = status2old;
- }
- }//END status2 UPDATE
- if (status3 != status3old) {
- if (status3 = 1) {
- lcd.setCursor(10, 1);
- lcd.print("R");
- status3 = status3old;
- }
- if (status3 == 2) {
- lcd.setCursor(10, 1);
- lcd.print("P");
- status3 = status3old;
- }
- if (status3 == 3) {
- lcd.setCursor(10, 1);
- lcd.print("F");
- status3 = status3old;
- }
- if (status3 == 4) {
- lcd.setCursor(10, 1);
- lcd.print("T");
- status3 = status3old;
- }
- }//END status3 UPDATE
- if (status4 != status4old) {
- if (status4 = 1) {
- lcd.setCursor(14, 1);
- lcd.print("R");
- status4 = status4old;
- }
- if (status4 == 2) {
- lcd.setCursor(14, 1);
- lcd.print("P");
- status4 = status4old;
- }
- if (status4 == 3) {
- lcd.setCursor(14, 1);
- lcd.print("F");
- status4 = status4old;
- }
- if (status4 == 4) {
- lcd.setCursor(14, 1);
- lcd.print("T");
- status4 = status4old;
- }
- }//END status4 UPDATE
- if (bstatus != bstatusold) {
- if (bstatus == 1) {
- lcd.setCursor(0, 3);
- lcd.print(" ");
- bstatus = bstatusold;
- }
- if (bstatus == 2) {
- lcd.setCursor(0, 3);
- lcd.print(" ");
- lcd.setCursor(0, 3);
- lcd.print("Moving Table CW");
- bstatus = bstatusold;
- }
- if (bstatus == 3) {
- lcd.setCursor(0, 3);
- lcd.print(" ");
- lcd.setCursor(0, 3);
- lcd.print("Moving Table CCW");
- bstatus = bstatusold;
- }
- lcd.setCursor(18, 1);
- lcd.print(testcounter);
- }//END bstatus UPDATE
- if (feedback != feedbackold) {
- lcd.setCursor(17, 1);
- lcd.print(" ");
- lcd.setCursor(17, 1);
- lcd.print(feedback);
- feedbackold = feedback;
- }
- }//================================END LCDUPDATE
- // ================================ TABLE MOVE FUNCTIONS =========================
- int motorcw() {
- bstatus = 2;
- LCDUPDATE();
- digitalWrite(indexdir, LOW);
- digitalWrite(indexmotor, HIGH);
- delay(stepspeed);
- digitalWrite(indexmotor, LOW);
- bstatus = 1;
- LCDUPDATE();
- }
- int motorccw() {
- bstatus = 3;
- LCDUPDATE();
- digitalWrite(indexdir, HIGH);
- digitalWrite(indexmotor, HIGH);
- delay(stepspeed);
- digitalWrite(indexmotor, LOW);
- digitalWrite(indexdir, LOW);
- bstatus = 1;
- LCDUPDATE();
- }
- // ==================================== Button Check Loop ============================
- int buttoncheck() {
- startbuttonstate;
- int butToggle;
- startbuttonstate = digitalRead(buttonPin);
- estopstate = digitalRead(estopbutton);
- if (startbuttonstate == LOW && butToggle == 0 && startup == 0) {
- }
- if (startbuttonstate == HIGH && butToggle == 0 && startup == 0) { // start button
- indexresult = 0;
- indexstep = 0;
- butToggle = 1;
- startup = 1;
- }
- if (startbuttonstate == LOW && estopstate == LOW) { //clear when start is unpressed
- butToggle = 0;;
- }
- estopstate = digitalRead(estopbutton);
- if (estopstate == HIGH && butToggle == 0 ) { //if estop is pressed
- startup = 0; //unset variables
- indexresult = 0; //unset indexed
- int Step = motorccw(); //back off one step to unload any stresses on table
- hstatus = 4;
- }
- // DEBUG BUTTON
- }// END BUTTON CHECK LOOP
- // ================================= INDEX FUNCTION 2.0 ==============================
- int indexfunction() {
- int indexstate;
- LCDUPDATE();
- if (indexresult != 1 && startup == 1) { // start of index statement
- // indexresult 1 = indexed
- indexstate = digitalRead(indexswitch);
- estopstate = digitalRead(estopbutton);
- hstatus = 3;
- status1 = 4;
- //Step 1 Get off the switch if it is currently pressed
- if (indexstep == 0) { //breakout to step 1 or step 2 depending on if switch is pressed
- if (indexstate == HIGH && estopstate != HIGH) {
- indexstep = 1;
- feedback = 10; /// DEBUG
- }
- if (indexstate == LOW && estopstate != HIGH) { //enable step 2
- indexstep = 2;
- feedback = 11; /// DEBUG
- }
- } //END BREAKOUT
- if (indexstep == 1) { //step 1, rotate until you get off the switch
- if (indexstate == HIGH && estopstate != HIGH) {
- motorcw();
- mstatus = 4;
- feedback = 10; /// DEBUG
- return;
- }
- if (indexstate == LOW && estopstate != HIGH) { //enable step 2
- indexstep = 2;
- feedback = 11; /// DEBUG
- }
- } // END STEP 1
- if (indexstep == 2) { //step 2, keep rotating until switch is presed
- if (indexstate == LOW && estopstate != HIGH) {
- motorcw();
- mstatus = 4;
- feedback = 12; /// DEBUG
- return;
- }
- if (indexstate == HIGH && estopstate != HIGH) { //enable step 2
- indexstep = 3;
- feedback = 13; /// DEBUG
- }
- }
- if (indexstep == 3) { //step 3, back off switch for good zero
- if (indexstate == HIGH && estopstate != HIGH) {
- motorccw();
- mstatus = 4;
- feedback = 14; /// DEBUG
- return;
- }
- if (indexstate == LOW && estopstate != HIGH) { //enable step 2
- indexstep = 4;
- feedback = 15; /// DEBUG
- }
- }
- } //END INDEXING IFTHAN
- if (estopstate != HIGH && indexstep == 4 ) { //Step 4, Housecleaning
- hstatus = 5;
- mstatus = 6;
- feedback = 16;
- indexstep = 0;
- indexresult = 1; //Set index variable to indexed
- }
- estopstate = digitalRead(estopbutton);
- if (estopstate == HIGH) { //if estop is pressed
- startup = 0; //unset variables
- indexresult = 0; //unset indexed
- estopstate = 1;
- int motorccw(); //back off one step to unload any stresses on table
- hstatus = 1;
- status1 = 3;
- LCDUPDATE();
- }
- }// END INDEX FUNCTION
- int serialcheck() {
- ser = Serial.read();
- if (ser == 'H' && station1status == 0) {
- station1status = 1;
- Serial.print('H');
- }
- if (ser == 'L' && station1status == 0) {
- station1status = 2;
- Serial.print('L');
- }
- if (ser == 'J' && station2status == 0) {
- station2status = 1;
- Serial.print('J');
- }
- if (ser == 'K' && station2status == 0) {
- station2status = 2;
- Serial.print('K');
- }
- }
- void loop() {
- testcounter = testcounter; // CHANGE FIRST TO VARIABLE TO DEBUG
- LCDUPDATE();
- buttoncheck();
- if (startup == 1 && indexresult != 1) {
- indexfunction();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement