Advertisement
Guest User

Arduino wire cutter code

a guest
Oct 26th, 2017
18,474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. //------------------------------- librarys ----------------------------------
  2. #include <Wire.h>
  3. #include <rgb_lcd.h>
  4. #include <Servo.h>
  5.  
  6. //------------------------------- lcd ----------------------------------
  7. rgb_lcd lcd;
  8.  
  9. //------------------------------- stepper ----------------------------------
  10. #define stepPin 7
  11. #define dirPin 8
  12.  
  13. //------------------------------- servo ----------------------------------
  14. Servo snippers;
  15. #define servo 10
  16. #define openAngle 90
  17. #define closedAngle 0
  18.  
  19. //------------------------------- input ----------------------------------
  20.  
  21. #define leftButton 3
  22. #define rightButton 2
  23. #define upButton 4
  24. #define downButton 5
  25.  
  26. //------------------------------- user settings ----------------------------------
  27. unsigned int wireLength = 0;
  28. unsigned int wireQuantity = 0;
  29.  
  30. //------------------------------- system settings ----------------------------------
  31. int state = 0;
  32. int incrementSpeed = 1;
  33. int previousWireLength = 0;
  34. int previousWireQuantity = 0;
  35. float mmPerStep = 0.18096;
  36.  
  37.  
  38. void setup() {
  39.   Serial.begin(9600);
  40.  
  41.  
  42.   lcd.begin(16, 2); //LCD columns and rows
  43.   lcd.setRGB(255,255,0); //backlight colour
  44.  
  45.   pinMode(upButton, INPUT_PULLUP);
  46.   pinMode(downButton, INPUT_PULLUP);
  47.   pinMode(leftButton, INPUT_PULLUP);
  48.   pinMode(rightButton, INPUT_PULLUP);
  49.  
  50.   pinMode(stepPin,OUTPUT);
  51.   pinMode(dirPin,OUTPUT);
  52.  
  53.   snippers.attach(servo);
  54.  
  55.   snippers.write(openAngle);
  56.    
  57.   delay(1000);
  58. }
  59.  
  60. void loop() {
  61.   if (!digitalRead(rightButton)){
  62.     if(state == 5){
  63.       state = 0;
  64.     }
  65.     else{
  66.       state += 1;
  67.     }
  68.     delay(200);
  69.     lcd.clear();
  70.   }
  71.   if (!digitalRead(leftButton) && state > 0 && state < 4){
  72.     state -=1;
  73.     delay(200);
  74.     lcd.clear();
  75.   }
  76.  
  77.  
  78.   switch (state){
  79.     case 0:
  80.       homeScreen();
  81.       break;
  82.     case 1:
  83.        chooseWireLength();
  84.        break;
  85.     case 2:
  86.       chooseWireQuantity();
  87.       break;
  88.     case 3:
  89.       confirm();
  90.       break;
  91.     case 4:
  92.       currentlyCutting();
  93.       break;
  94.     case 5:
  95.       finishedCutting();
  96.       break;
  97.   }
  98.  
  99. }
  100.  
  101.  
  102. void homeScreen(){
  103.   lcd.setCursor(0, 0);
  104.   lcd.print("WIRE CUTTER");
  105.   lcd.setCursor(11, 1);
  106.   lcd.print("NEXT>");
  107.   delay(100);
  108. }
  109.  
  110.  
  111. void chooseWireLength(){
  112.   wireLength = changeValue(wireLength);
  113.  
  114.   //clear LCD if required
  115.   if(previousWireLength != wireLength){
  116.     lcd.clear();
  117.     previousWireLength = wireLength;
  118.   }
  119.  
  120.   //Display information on LCD
  121.   lcd.setCursor(0, 0);
  122.   lcd.print("LENGTH:" + (String)wireLength + "mm");
  123.   displayNavigation();
  124. }
  125.  
  126. void chooseWireQuantity(){
  127.   wireQuantity = changeValue(wireQuantity);
  128.  
  129.   //clear LCD if required
  130.   if(previousWireQuantity != wireQuantity){
  131.     lcd.clear();
  132.     previousWireQuantity = wireQuantity;
  133.   }
  134.  
  135.   //Display information on LCD
  136.   lcd.setCursor(0, 0);
  137.   lcd.print("QUANTITY:" + (String)wireQuantity);
  138.   displayNavigation();
  139. }
  140.  
  141. void confirm(){
  142.   lcd.setCursor(0, 0);
  143.   lcd.print((String)wireLength + "mm x " + (String)wireQuantity + "pcs");
  144.   lcd.setCursor(0, 1);
  145.   lcd.print("<BACK");
  146.   lcd.setCursor(10, 1);
  147.   lcd.print("START>");
  148.   delay(100);
  149. }
  150.  
  151. void currentlyCutting(){
  152.   lcd.setCursor(0, 0);
  153.   lcd.print((String)0 + "/" + (String)wireQuantity);
  154.   lcd.setCursor(0, 1);
  155.   lcd.print("???s");
  156.   int stepsToTake = (int)wireLength/mmPerStep;
  157.   for(int i = 0; i < wireQuantity; i++){
  158.     unsigned long timeForOneCycle = millis();
  159.     digitalWrite(dirPin,HIGH);
  160.     for(int x = 0; x < stepsToTake; x++) {
  161.       digitalWrite(stepPin,HIGH);
  162.       delayMicroseconds(500);
  163.       digitalWrite(stepPin,LOW);
  164.       delayMicroseconds(500);
  165.     }
  166.    
  167.     lcd.setCursor(0, 0);
  168.     lcd.print((String)(i+1) + "/" + (String)wireQuantity);
  169.    
  170.     snippers.write(closedAngle);
  171.     delay(600);
  172.     snippers.write(openAngle);
  173.     delay(600);
  174.  
  175.    
  176.     lcd.setCursor(0, 1);
  177.  
  178.     unsigned long timeRemaining = ((millis() - timeForOneCycle)*(wireQuantity - (i+1)))/1000;
  179.     lcd.print((String)timeRemaining + "s    ");
  180.    
  181.   }
  182.   wireLength = 0;
  183.   wireQuantity = 0;
  184.   state = 5;
  185. }
  186.  
  187. void finishedCutting(){
  188.   lcd.clear();
  189.   lcd.setCursor(0, 0);
  190.   lcd.print("CUTTING COMPLETE");
  191.   lcd.setCursor(11, 1);
  192.   lcd.print("NEXT>");
  193.   delay(100);
  194. }
  195.  
  196.  
  197. int changeValue(int currentValue){
  198.   if (!digitalRead(upButton)) {
  199.     currentValue += incrementSpeed;
  200.     incrementSpeed ++;
  201.   }
  202.   if (!digitalRead(downButton)) {
  203.     if(currentValue - incrementSpeed >= 0){
  204.       currentValue -= incrementSpeed;
  205.       incrementSpeed ++;
  206.     }
  207.     else{
  208.       currentValue = 0;
  209.     }
  210.   }
  211.   if (digitalRead(downButton) && digitalRead(upButton)){
  212.     incrementSpeed = 1;
  213.   }
  214.   return currentValue;
  215. }
  216.  
  217. void displayNavigation(){
  218.   lcd.setCursor(0, 1);
  219.   lcd.print("<BACK");
  220.   lcd.setCursor(11, 1);
  221.   lcd.print("NEXT>");
  222.   delay(100);
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement