Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.76 KB | None | 0 0
  1. //include required libraries
  2. #include <SoftwareSerial.h>
  3. #include <LiquidCrystal.h>
  4. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  5.  
  6. //===CONSTANTS and variables (buttons and pins)
  7. const int buttonPin = 9; // start button
  8. int startbuttonstate = 0;
  9. int startup = 0; // has the start button been pressed 1=yes
  10.  
  11. int indexed = 0; // is table indexed
  12. int indexsteps = 0;
  13. int indexstep;
  14.  
  15. const int estopbutton = 8; //estop button
  16. int estopstate = 0; // are we in a stop state
  17.  
  18. const int indexswitch = 7; //table index switch
  19. const int indexmotor = 6; //table stepper motor
  20. const int indexdir = 10; //table stepper direction pin
  21. int indexresult; //indexresult vars: --0=unindexed --1=indexed --2=first stage done --3=second stage done --4=finishing up
  22.  
  23. int stationresult = 1; //stationresult vars : --0=not ready --1=get time --2=running
  24. int station1status; // 0 = ready 1 = good but not done 2 = bad but not done 3 = done
  25. int station2status; // 0 = ready 1 = good but not done 2 = bad but not done 3 = done
  26. int stationdone; // 0 = ready 1 = good 2 = failure
  27. int ser; // serial communication variable
  28.  
  29. //===== LED PINS
  30. const int stn1good = 50; //50
  31. const int stn1bad = 52; //52
  32. const int stn2good = 46; //46
  33. const int stn2bad = 48; //48
  34.  
  35. //===== DEBUG BUTTON STUFF
  36. int tbuttcounter = 0;
  37. int lastbuttonstate = 0;
  38. int testcounter = 0;
  39. int TESTBUTTON = 22;
  40. int TESTBUTTONSTATE = 0;
  41.  
  42. //==== LCD STATUS VARIABLES
  43. int status1 = 1; // LCD STATUS DISPLAYS 1 = BOOTING 2 = READY 3 = INDEXING 4 = FAILURE 5 = PASS
  44. int status2 = 1;
  45. int status3 = 1;
  46. int status4 = 1;
  47. int status1old;
  48. int status2old;
  49. int status3old;
  50. int status4old;
  51. int hstatus = 0; // header
  52. int mstatus = 0; // Middle status 1= Startup 2=Ready 3=Waiting for STN 4 index stg 1
  53. int bstatus = 0; // Bottom status
  54. int hstatusold;
  55. int mstatusold;
  56. int bstatusold;
  57. int feedback; // lcd feedback print
  58. int feedbackold;
  59.  
  60.  
  61. //===== REFERENCED SETTINGS
  62. int stepspeed = 100; // Stepper motor step sign in MS
  63.  
  64.  
  65. void setup() {
  66. //PINOUT BUTTONS/LEDs
  67.  
  68. pinMode(indexmotor, OUTPUT);
  69. pinMode(indexdir, OUTPUT);
  70. pinMode(estopbutton, INPUT);
  71. pinMode(indexswitch, INPUT);
  72. pinMode(buttonPin, INPUT);
  73. pinMode(TESTBUTTON, INPUT);
  74.  
  75. //PINOUT LEDS
  76. pinMode(stn1good, OUTPUT);
  77. pinMode(stn1bad, OUTPUT);
  78. pinMode(stn2good, OUTPUT);
  79. pinMode(stn2bad, OUTPUT);
  80.  
  81. //setup serial communication
  82. Serial.begin(9600);
  83. //LCD STARTUP
  84. lcd.begin(20, 4);
  85. lcd.setCursor(0, 0);
  86. lcd.print(" Booting! ");
  87. lcd.setCursor(0, 1);
  88. lcd.print("1:B 2:B 3:B 4:B");
  89. }
  90.  
  91. //=====BEGIN FUNCTIONS
  92. //============================================LCD UPDATE FUNCTION============================
  93. int LCDUPDATE() {
  94. if (hstatus != hstatusold) {
  95. if (hstatus == 1) {
  96. lcd.setCursor(0, 0);
  97. lcd.print(" ");
  98. lcd.setCursor(0, 0);
  99. lcd.print(" Booting!2 ");
  100. hstatus = hstatusold;
  101. }
  102. if (hstatus == 2) {
  103. lcd.setCursor(0, 0);
  104. lcd.print(" ");
  105. lcd.setCursor(0, 0);
  106. lcd.print(" Ready For Start ");
  107. hstatus = hstatusold;
  108. }
  109. if (hstatus == 3) {
  110. lcd.setCursor(0, 0);
  111. lcd.print(" ");
  112. lcd.setCursor(0, 0);
  113. lcd.print(" Indexing Table ");
  114. hstatus = hstatusold;
  115. }
  116. if (hstatus == 4) {
  117. lcd.setCursor(0, 0);
  118. lcd.print(" ");
  119. lcd.setCursor(0, 0);
  120. lcd.print(" Process Stopped ");
  121. hstatus = hstatusold;
  122. }
  123. if (hstatus == 5) {
  124. lcd.setCursor(0, 0);
  125. lcd.print(" ");
  126. lcd.setCursor(0, 0);
  127. lcd.print(" Table Indexed ");
  128. hstatus = hstatusold;
  129. }
  130.  
  131. }//END hstatus UPDATE
  132.  
  133. if (mstatus != mstatusold) {
  134. if (mstatus = 1) {
  135. lcd.setCursor(0, 2);
  136. lcd.print(" ");
  137. mstatus = mstatusold;
  138. }
  139. if (mstatus == 2) {
  140. lcd.setCursor(0, 2);
  141. lcd.print(" ");
  142. lcd.setCursor(0, 2);
  143. lcd.print("Ready for start");
  144. mstatus = mstatusold;
  145. }
  146. if (mstatus == 3) {
  147. lcd.setCursor(0, 2);
  148. lcd.print(" ");
  149. lcd.setCursor(0, 2);
  150. lcd.print("Waiting for Stations");
  151. mstatus = mstatusold;
  152. }
  153.  
  154. if (mstatus == 4) {
  155. lcd.setCursor(0, 2);
  156. lcd.print(" ");
  157. lcd.setCursor(0, 2);
  158. lcd.print("Index Stage 1");
  159. mstatus = mstatusold;
  160. }
  161.  
  162. if (mstatus == 5) {
  163. lcd.setCursor(0, 2);
  164. lcd.print(" ");
  165. lcd.setCursor(0, 2);
  166. lcd.print("Index Stage 2");
  167. mstatus = mstatusold;
  168. }
  169. if (mstatus == 6) {
  170. lcd.setCursor(0, 2);
  171. lcd.print(" ");
  172. lcd.setCursor(0, 2);
  173. lcd.print("Index Stage 3");
  174. mstatus = mstatusold;
  175. }
  176. if (mstatus == 7) {
  177. lcd.setCursor(0, 2);
  178. lcd.print(" ");
  179. lcd.setCursor(0, 2);
  180. lcd.print("Index Completed");
  181. mstatus = mstatusold;
  182. }
  183.  
  184.  
  185. }//END mstatus UPDATE
  186.  
  187. if (status1 != status1old) {
  188. if (status1 == 1) {
  189. lcd.setCursor(2, 1);
  190. lcd.print("R");
  191. status1 = status1old;
  192. }
  193. if (status1 == 2) {
  194. lcd.setCursor(2, 1);
  195. lcd.print("P");
  196. status1 = status1old;
  197. }
  198. if (status1 == 3) {
  199. lcd.setCursor(2, 1);
  200. lcd.print("F");
  201. status1 = status1old;
  202. }
  203. if (status1 == 4) {
  204. lcd.setCursor(2, 1);
  205. lcd.print("T");
  206. status1 = status1old;
  207. }
  208. }//END STATUS1 UPDATE
  209. if (status2 != status2old) {
  210. if (status2 == 1) {
  211. lcd.setCursor(6, 1);
  212. lcd.print("R");
  213. status2 = status2old;
  214. }
  215. if (status2 == 2) {
  216. lcd.setCursor(6, 1);
  217. lcd.print("P");
  218. status2 = status2old;
  219. }
  220. if (status2 == 3) {
  221. lcd.setCursor(6, 1);
  222. lcd.print("F");
  223. status2 = status2old;
  224. }
  225. if (status2 == 4) {
  226. lcd.setCursor(6, 1);
  227. lcd.print("T");
  228. status2 = status2old;
  229. }
  230. }//END status2 UPDATE
  231. if (status3 != status3old) {
  232. if (status3 = 1) {
  233. lcd.setCursor(10, 1);
  234. lcd.print("R");
  235. status3 = status3old;
  236. }
  237. if (status3 == 2) {
  238. lcd.setCursor(10, 1);
  239. lcd.print("P");
  240. status3 = status3old;
  241. }
  242. if (status3 == 3) {
  243. lcd.setCursor(10, 1);
  244. lcd.print("F");
  245. status3 = status3old;
  246. }
  247. if (status3 == 4) {
  248. lcd.setCursor(10, 1);
  249. lcd.print("T");
  250. status3 = status3old;
  251. }
  252. }//END status3 UPDATE
  253. if (status4 != status4old) {
  254. if (status4 = 1) {
  255. lcd.setCursor(14, 1);
  256. lcd.print("R");
  257. status4 = status4old;
  258. }
  259. if (status4 == 2) {
  260. lcd.setCursor(14, 1);
  261. lcd.print("P");
  262. status4 = status4old;
  263. }
  264. if (status4 == 3) {
  265. lcd.setCursor(14, 1);
  266. lcd.print("F");
  267. status4 = status4old;
  268. }
  269. if (status4 == 4) {
  270. lcd.setCursor(14, 1);
  271. lcd.print("T");
  272. status4 = status4old;
  273. }
  274. }//END status4 UPDATE
  275. if (bstatus != bstatusold) {
  276. if (bstatus == 1) {
  277. lcd.setCursor(0, 3);
  278. lcd.print(" ");
  279. bstatus = bstatusold;
  280. }
  281. if (bstatus == 2) {
  282. lcd.setCursor(0, 3);
  283. lcd.print(" ");
  284. lcd.setCursor(0, 3);
  285. lcd.print("Moving Table CW");
  286. bstatus = bstatusold;
  287. }
  288. if (bstatus == 3) {
  289. lcd.setCursor(0, 3);
  290. lcd.print(" ");
  291. lcd.setCursor(0, 3);
  292. lcd.print("Moving Table CCW");
  293. bstatus = bstatusold;
  294. }
  295. lcd.setCursor(18, 1);
  296. lcd.print(testcounter);
  297. }//END bstatus UPDATE
  298. if (feedback != feedbackold) {
  299. lcd.setCursor(17, 1);
  300. lcd.print(" ");
  301. lcd.setCursor(17, 1);
  302. lcd.print(feedback);
  303. feedbackold = feedback;
  304. }
  305.  
  306. }//================================END LCDUPDATE
  307.  
  308. // ================================ TABLE MOVE FUNCTIONS =========================
  309. int motorcw() {
  310. bstatus = 2;
  311. LCDUPDATE();
  312. digitalWrite(indexdir, LOW);
  313. digitalWrite(indexmotor, HIGH);
  314. delay(stepspeed);
  315. digitalWrite(indexmotor, LOW);
  316. bstatus = 1;
  317. LCDUPDATE();
  318. }
  319. int motorccw() {
  320. bstatus = 3;
  321. LCDUPDATE();
  322. digitalWrite(indexdir, HIGH);
  323. digitalWrite(indexmotor, HIGH);
  324. delay(stepspeed);
  325. digitalWrite(indexmotor, LOW);
  326. digitalWrite(indexdir, LOW);
  327. bstatus = 1;
  328. LCDUPDATE();
  329. }
  330.  
  331. // ==================================== Button Check Loop ============================
  332. int buttoncheck() {
  333. startbuttonstate;
  334. int butToggle;
  335. startbuttonstate = digitalRead(buttonPin);
  336. estopstate = digitalRead(estopbutton);
  337. if (startbuttonstate == LOW && butToggle == 0 && startup == 0) {
  338. }
  339. if (startbuttonstate == HIGH && butToggle == 0 && startup == 0) { // start button
  340. indexresult = 0;
  341. indexstep = 0;
  342. butToggle = 1;
  343. startup = 1;
  344. }
  345. if (startbuttonstate == LOW && estopstate == LOW) { //clear when start is unpressed
  346. butToggle = 0;;
  347. }
  348. estopstate = digitalRead(estopbutton);
  349. if (estopstate == HIGH && butToggle == 0 ) { //if estop is pressed
  350. startup = 0; //unset variables
  351. indexresult = 0; //unset indexed
  352. int Step = motorccw(); //back off one step to unload any stresses on table
  353. hstatus = 4;
  354. }
  355. // DEBUG BUTTON
  356.  
  357.  
  358. }// END BUTTON CHECK LOOP
  359.  
  360. // ================================= INDEX FUNCTION 2.0 ==============================
  361. int indexfunction() {
  362. int indexstate;
  363. LCDUPDATE();
  364. if (indexresult != 1 && startup == 1) { // start of index statement
  365. // indexresult 1 = indexed
  366. indexstate = digitalRead(indexswitch);
  367. estopstate = digitalRead(estopbutton);
  368. hstatus = 3;
  369. status1 = 4;
  370. //Step 1 Get off the switch if it is currently pressed
  371. if (indexstep == 0) { //breakout to step 1 or step 2 depending on if switch is pressed
  372. if (indexstate == HIGH && estopstate != HIGH) {
  373. indexstep = 1;
  374. feedback = 10; /// DEBUG
  375. }
  376. if (indexstate == LOW && estopstate != HIGH) { //enable step 2
  377. indexstep = 2;
  378. feedback = 11; /// DEBUG
  379. }
  380. } //END BREAKOUT
  381.  
  382. if (indexstep == 1) { //step 1, rotate until you get off the switch
  383. if (indexstate == HIGH && estopstate != HIGH) {
  384. motorcw();
  385. mstatus = 4;
  386. feedback = 10; /// DEBUG
  387. return;
  388. }
  389. if (indexstate == LOW && estopstate != HIGH) { //enable step 2
  390. indexstep = 2;
  391. feedback = 11; /// DEBUG
  392. }
  393.  
  394. } // END STEP 1
  395.  
  396. if (indexstep == 2) { //step 2, keep rotating until switch is presed
  397. if (indexstate == LOW && estopstate != HIGH) {
  398. motorcw();
  399. mstatus = 4;
  400. feedback = 12; /// DEBUG
  401. return;
  402. }
  403. if (indexstate == HIGH && estopstate != HIGH) { //enable step 2
  404. indexstep = 3;
  405. feedback = 13; /// DEBUG
  406. }
  407. }
  408.  
  409. if (indexstep == 3) { //step 3, back off switch for good zero
  410. if (indexstate == HIGH && estopstate != HIGH) {
  411. motorccw();
  412. mstatus = 4;
  413. feedback = 14; /// DEBUG
  414. return;
  415. }
  416. if (indexstate == LOW && estopstate != HIGH) { //enable step 2
  417. indexstep = 4;
  418. feedback = 15; /// DEBUG
  419. }
  420. }
  421. } //END INDEXING IFTHAN
  422.  
  423. if (estopstate != HIGH && indexstep == 4 ) { //Step 4, Housecleaning
  424. hstatus = 5;
  425. mstatus = 6;
  426. feedback = 16;
  427. indexstep = 0;
  428. indexresult = 1; //Set index variable to indexed
  429. }
  430. estopstate = digitalRead(estopbutton);
  431. if (estopstate == HIGH) { //if estop is pressed
  432. startup = 0; //unset variables
  433. indexresult = 0; //unset indexed
  434. estopstate = 1;
  435. int motorccw(); //back off one step to unload any stresses on table
  436. hstatus = 1;
  437. status1 = 3;
  438. LCDUPDATE();
  439. }
  440.  
  441. }// END INDEX FUNCTION
  442.  
  443. int serialcheck() {
  444. ser = Serial.read();
  445. if (ser == 'H' && station1status == 0) {
  446. station1status = 1;
  447. Serial.print('H');
  448. }
  449. if (ser == 'L' && station1status == 0) {
  450. station1status = 2;
  451. Serial.print('L');
  452. }
  453. if (ser == 'J' && station2status == 0) {
  454. station2status = 1;
  455. Serial.print('J');
  456. }
  457. if (ser == 'K' && station2status == 0) {
  458. station2status = 2;
  459. Serial.print('K');
  460. }
  461. }
  462.  
  463.  
  464.  
  465. void loop() {
  466. testcounter = testcounter; // CHANGE FIRST TO VARIABLE TO DEBUG
  467. LCDUPDATE();
  468. buttoncheck();
  469. if (startup == 1 && indexresult != 1) {
  470. indexfunction();
  471. }
  472.  
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement