Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------ librarys
- #include <LiquidCrystal_I2C.h>
- #include <Wire.h>
- #include <Servo.h>
- //------ lcd
- LiquidCrystal_I2C lcd(0x27,16,2);
- //------ stepper
- #define stepPin 7
- #define dirPin 8
- //------ servo
- Servo snippers;
- #define servo 10
- #define openAngle 180
- #define closedAngle 0
- //------ input
- #define dellButton 5
- #define leftButton 6
- #define rightButton 7
- #define upButton 8
- #define downButton 9
- //------ user settings
- unsigned int wireLength = 0;
- unsigned int wireQuantity = 0;
- //------ system settings
- int state = 0;
- int incrementSpeed = 1;
- int previousWireLength = 25;
- int previousWireQuantity = 3;
- float mmPerStep = 0.18096;
- void setup() {
- Serial.begin(9600);
- lcd.init();
- lcd.backlight();
- pinMode(upButton, INPUT_PULLUP);
- pinMode(downButton, INPUT_PULLUP);
- pinMode(leftButton, INPUT_PULLUP);
- pinMode(rightButton, INPUT_PULLUP);
- pinMode(stepPin, OUTPUT);
- pinMode(dirPin, OUTPUT);
- snippers.attach(servo);
- snippers.write(openAngle);
- delay(1000);
- }
- void loop() {
- //Pokud je stisknute tlacitko „right“
- if(!digitalRead(rightButton)) {
- //a hodnota proměnné state = 5
- if(state == 5) {
- //uměníme hodnotu tate na 0 -> home screen
- state = 0;
- }
- else {
- //poku není zvýšíme hodnotu proměnné state o 1
- state += 1;
- }
- delay(200);
- lcd.clear();
- }
- //pokud je stisknuté tlačítko „left“ a hodnota proměnné state je mezi 0 a 4
- if(!digitalRead(leftButton) && state > 0 && state < 4) {
- //snizime hodnotu state o 1
- state -= 1;
- delay(200);
- lcd.clear();
- }
- //------ Menu
- switch(state) {
- case 0:
- homeScreen();
- break;
- case 1:
- chooseWireLength();
- break;
- case 2:
- chooseWireQuantity();
- break;
- case 3:
- confirm();
- break;
- case 4:
- currentlyCutting();
- break;
- case 5:
- finishedCutting();
- break;
- }
- }
- void homeScreen() {
- lcd.setCursor(0, 0);
- lcd.print("WIRE CUTTER");
- lcd.setCursor(0, 1);
- lcd.print("v1.1.0");
- lcd.setCursor(11, 1);
- lcd.print("NEXT>");
- delay(100);
- }
- void chooseWireLength() {
- wireLength = changeValue(wireLength);
- lcd.setCursor(0, 0);
- lcd.print("Length:" + (String)previousWireLength + "mm");
- //nejprve zobrazíme poslední nastavení
- if(previousWireLength >Ř 0) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("LENGTH:" + (String)previousWireLength + "mm");
- if(!digitalRead(rightButton)) {
- wireLength = previousWireLength;
- }
- }
- if(!digitalRead(upButton)) {
- if(previousWireLength != wireLength) {
- lcd.clear();
- previousWireLength = wireLength;
- lcd.setCursor(0, 0);
- lcd.print("LENGTH:" + (String)wireLength + "mm");
- }
- }
- displayNavigation();
- }
- void chooseWireQuantity () {
- wireQuantity = changeValue(wireQuantity);
- lcd.setCursor(0, 0);
- lcd.print("Quantity:" + (String)previousWireQuantity + "pcs");
- //nejprve poslední nastavení
- if(previousWireQuantity > 0) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Quantity:" + (String)previousWireQuantity + "pcs");
- if(!digitalRead(rightButton)) {
- wireQuantity = previousWireQuantity;
- }
- }
- if(!digitalRead(upButton)) {
- if(previousWireQuantity != wireQuantity) {
- lcd.clear();
- previousWireQuantity = wireQuantity;
- lcd.setCursor(0, 0);
- lcd.print("Quantity:" + (String)wireQuantity + "pcs");
- }
- }
- displayNavigation();
- }
- void confirm() {
- lcd.setCursor(0, 0);
- lcd.print((String)wireLength + "mm x " + (String)wireQuantity + "pcs");
- lcd.setCursor(0, 1);
- lcd.print("<BACK");
- lcd.setCursor(10, 1);
- lcd.print("START>");
- delay(100);
- }
- void currentlyCutting() {
- lcd.setCursor(0, 0);
- lcd.print((String)0 + "/" + (String)wireQuantity);
- lcd.setCursor(0, 1);
- lcd.print("???s");
- int stepsToTake = (int)wireLength/mmPerStep;
- for(int i = 0; i < wireQuantity; i++) {
- unsigned long timeForOneCycle = millis();
- digitalWrite(dirPin, HIGH);
- for(int x = 0; x < stepsToTake; x++) {
- digitalWrite(stepPin, HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin, LOW);
- delayMicroseconds(500);
- }
- lcd.setCursor(0, 0);
- lcd.print((String)(i+1) + "/" + (String)wireQuantity);
- snippers.write(closedAngle);
- delay(600);
- snippers.write(openAngle);
- delay(600);
- lcd.setCursor(0, 1);
- unsigned long timeRemaining = ((millis() - timeForOneCycle) * (wireQuantity - (i+1)))/1000;
- lcd.print((String) timeRemaining + "s ");
- }
- previousWireLength = wireLength;
- previousWireQuantity = wireQuantity;
- state = 5;
- }
- void finishedCutting() {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("CUTTING COMPLETE");
- lcd.setCursor(11, 1);
- lcd.print("NEXT>");
- delay(500);
- lcd.clear();
- }
- int changeValue(int currentValue) {
- if(!digitalRead(upButton)) {
- delay(100);
- currentValue += incrementSpeed;
- }
- if(!digitalRead(downButton)) {
- if(currentValue - incrementSpeed >= 0) {
- delay(100);
- currentValue -= incrementSpeed;
- }
- else {
- currentValue = 0;
- }
- }
- if(!digitalRead(downButton) && !digitalRead(upButton)) {
- incrementSpeed = 1;
- }
- return currentValue;
- }
- void displayNavigation() {
- lcd.setCursor(0, 1);
- lcd.print("<BACK");
- if(state ==1 || state ==2) {
- lcd.setCursor(7, 1);
- lcd.print("X");
- }
- lcd.setCursor(11, 1);
- lcd.print("NEXT>");
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement