Advertisement
martin_cz

automatic wire cutter

May 4th, 2018
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.56 KB | None | 0 0
  1. //------ librarys
  2.  
  3. #include <LiquidCrystal_I2C.h>
  4. #include <Wire.h>
  5. #include <Servo.h>
  6.  
  7. //------ lcd
  8.  
  9. LiquidCrystal_I2C lcd(0x27,16,2);  
  10.  
  11. //------ stepper
  12.  
  13. #define stepPin 7
  14. #define dirPin 8
  15.  
  16. //------ servo
  17.  
  18. Servo snippers;
  19. #define servo 10
  20. #define openAngle 180
  21. #define closedAngle 0
  22.  
  23. //------ input
  24.  
  25. #define dellButton 5
  26. #define leftButton 6
  27. #define rightButton 7
  28. #define upButton 8
  29. #define downButton 9
  30.  
  31. //------ user settings
  32.  
  33. unsigned int wireLength = 0;
  34. unsigned int wireQuantity = 0;
  35.  
  36. //------ system settings
  37.  
  38. int state = 0;
  39. int incrementSpeed = 1;
  40. int previousWireLength = 25;
  41. int previousWireQuantity = 3;
  42. float mmPerStep = 0.18096;
  43.  
  44. void setup() {
  45.   Serial.begin(9600);
  46.   lcd.init();
  47.   lcd.backlight();
  48.  
  49.   pinMode(upButton, INPUT_PULLUP);
  50.   pinMode(downButton, INPUT_PULLUP);
  51.   pinMode(leftButton, INPUT_PULLUP);
  52.   pinMode(rightButton, INPUT_PULLUP);
  53.  
  54.   pinMode(stepPin, OUTPUT);
  55.   pinMode(dirPin, OUTPUT);
  56.  
  57.   snippers.attach(servo);
  58.  
  59.   snippers.write(openAngle);
  60.  
  61.   delay(1000);
  62. }
  63.  
  64. void loop() {
  65.   //Pokud je stisknute tlacitko „right“
  66.   if(!digitalRead(rightButton)) {
  67.     //a hodnota proměnné state = 5
  68.     if(state == 5) {
  69.       //uměníme hodnotu tate na 0 -> home screen
  70.       state = 0;
  71.     }
  72.     else {
  73.       //poku není zvýšíme hodnotu proměnné state o 1
  74.       state += 1;
  75.     }
  76.     delay(200);
  77.     lcd.clear();
  78.   }
  79.   //pokud je stisknuté tlačítko „left“ a hodnota proměnné state je mezi 0 a 4
  80.   if(!digitalRead(leftButton) && state > 0 && state < 4) {
  81.     //snizime hodnotu state o 1
  82.     state -= 1;
  83.     delay(200);
  84.     lcd.clear();
  85.   }
  86.  
  87.   //------ Menu
  88.   switch(state) {
  89.     case 0:
  90.       homeScreen();
  91.       break;
  92.     case 1:
  93.       chooseWireLength();
  94.       break;
  95.     case 2:
  96.       chooseWireQuantity();
  97.       break;
  98.     case 3:
  99.       confirm();
  100.       break;
  101.     case 4:
  102.       currentlyCutting();
  103.       break;
  104.     case 5:
  105.       finishedCutting();
  106.       break;
  107.   }
  108. }
  109.  
  110. void homeScreen() {
  111.   lcd.setCursor(0, 0);
  112.   lcd.print("WIRE CUTTER");
  113.   lcd.setCursor(0, 1);
  114.   lcd.print("v1.1.0");
  115.   lcd.setCursor(11, 1);
  116.   lcd.print("NEXT>");
  117.   delay(100);
  118. }
  119.  
  120. void chooseWireLength() {
  121.   wireLength = changeValue(wireLength);
  122.  
  123.   lcd.setCursor(0, 0);
  124.   lcd.print("Length:" + (String)previousWireLength + "mm");
  125.  
  126.   //nejprve zobrazíme poslední nastavení
  127.   if(previousWireLength >Ř 0) {
  128.     lcd.clear();
  129.     lcd.setCursor(0, 0);
  130.     lcd.print("LENGTH:" + (String)previousWireLength + "mm");
  131.     if(!digitalRead(rightButton)) {
  132.       wireLength = previousWireLength;
  133.     }
  134.   }
  135.   if(!digitalRead(upButton)) {
  136.     if(previousWireLength != wireLength) {
  137.       lcd.clear();
  138.       previousWireLength = wireLength;
  139.  
  140.       lcd.setCursor(0, 0);
  141.       lcd.print("LENGTH:" + (String)wireLength + "mm");
  142.     }
  143.   }
  144.   displayNavigation();
  145. }
  146.  
  147. void chooseWireQuantity () {
  148.   wireQuantity = changeValue(wireQuantity);
  149.  
  150.   lcd.setCursor(0, 0);
  151.   lcd.print("Quantity:" + (String)previousWireQuantity + "pcs");
  152.  
  153.   //nejprve poslední nastavení
  154.   if(previousWireQuantity > 0) {
  155.     lcd.clear();
  156.     lcd.setCursor(0, 0);
  157.     lcd.print("Quantity:" + (String)previousWireQuantity + "pcs");
  158.     if(!digitalRead(rightButton)) {
  159.       wireQuantity = previousWireQuantity;
  160.     }
  161.   }
  162.   if(!digitalRead(upButton)) {
  163.     if(previousWireQuantity != wireQuantity) {
  164.       lcd.clear();
  165.       previousWireQuantity = wireQuantity;
  166.  
  167.       lcd.setCursor(0, 0);
  168.       lcd.print("Quantity:" + (String)wireQuantity + "pcs");
  169.     }
  170.   }
  171.   displayNavigation();
  172. }
  173.  
  174. void confirm() {
  175.   lcd.setCursor(0, 0);
  176.   lcd.print((String)wireLength + "mm x " + (String)wireQuantity + "pcs");
  177.   lcd.setCursor(0, 1);
  178.   lcd.print("<BACK");
  179.   lcd.setCursor(10, 1);
  180.   lcd.print("START>");
  181.   delay(100);
  182. }
  183.  
  184. void currentlyCutting() {
  185.   lcd.setCursor(0, 0);
  186.   lcd.print((String)0 + "/" + (String)wireQuantity);
  187.   lcd.setCursor(0, 1);
  188.   lcd.print("???s");
  189.   int stepsToTake = (int)wireLength/mmPerStep;
  190.   for(int i = 0; i < wireQuantity; i++) {
  191.     unsigned long timeForOneCycle = millis();
  192.     digitalWrite(dirPin, HIGH);
  193.     for(int x = 0; x < stepsToTake; x++) {
  194.       digitalWrite(stepPin, HIGH);
  195.       delayMicroseconds(500);
  196.       digitalWrite(stepPin, LOW);
  197.       delayMicroseconds(500);
  198.     }
  199.  
  200.     lcd.setCursor(0, 0);
  201.     lcd.print((String)(i+1) + "/" + (String)wireQuantity);
  202.  
  203.     snippers.write(closedAngle);
  204.     delay(600);
  205.     snippers.write(openAngle);
  206.     delay(600);
  207.  
  208.     lcd.setCursor(0, 1);
  209.  
  210.     unsigned long timeRemaining = ((millis() - timeForOneCycle) * (wireQuantity - (i+1)))/1000;
  211.     lcd.print((String) timeRemaining + "s    ");
  212.   }
  213.   previousWireLength = wireLength;
  214.   previousWireQuantity = wireQuantity;
  215.   state = 5;
  216. }
  217.  
  218. void finishedCutting() {
  219.   lcd.clear();
  220.   lcd.setCursor(0, 0);
  221.   lcd.print("CUTTING COMPLETE");
  222.   lcd.setCursor(11, 1);
  223.   lcd.print("NEXT>");
  224.   delay(500);
  225.   lcd.clear();
  226. }
  227.  
  228. int changeValue(int currentValue) {
  229.   if(!digitalRead(upButton)) {
  230.     delay(100);
  231.     currentValue += incrementSpeed;
  232.   }
  233.   if(!digitalRead(downButton)) {
  234.     if(currentValue - incrementSpeed >= 0) {
  235.       delay(100);
  236.       currentValue -= incrementSpeed;
  237.     }
  238.     else {
  239.       currentValue = 0;
  240.     }
  241.   }
  242.   if(!digitalRead(downButton) && !digitalRead(upButton)) {
  243.     incrementSpeed = 1;
  244.   }
  245.   return currentValue;
  246. }
  247.  
  248. void displayNavigation() {
  249.   lcd.setCursor(0, 1);
  250.   lcd.print("<BACK");
  251.   if(state ==1 || state ==2) {
  252.     lcd.setCursor(7, 1);
  253.     lcd.print("X");
  254.   }
  255.   lcd.setCursor(11, 1);
  256.   lcd.print("NEXT>");
  257.   delay(100);
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement