Close

New Source-Code

A project log for Cable-Cutting-Machine

No more boring and endless cable-cutting-sessions.

kirschner-christophKirschner Christoph 03/14/2017 at 20:570 Comments

Down there you can see the new code i wrote.


/*
    Author: Christoph Kirschner
    Created: 14.03.2016
    Description: Automatischer Kabelschneider
*/

#include <LiquidCrystal_I2C.h>
#include <Stepper.h>
#include <Wire.h>

// Anzahl der Schritte, die für eine Umdrehung benötigt werden
#define STEPS 400

int SPc = 18;

// Spannungsgrenzwerte der ersten 5 Knöpfen
int voltages[5][2] = {{1005, 1016}, {510, 520}, {90, 110}, {170, 185}, {930, 932}};

// Sorgt dafür, dass die Knöpfe nur jede Millisekunde ausgelesen werden
unsigned long lastButtonReadTime = 0;

// Zählt wie viele Millisekunden eine Spannungänderung anlag
unsigned int changedVoltageTime = 0;

// Letzte entprellte Eingangsspannung
int lastButtonVoltage = 0;

//letzter entprellter Eingangswert
int lastButtonState1 = 0;

//letzter entprellter Eingangswert
int lastButtonState2 = 0;

//letzter entprellter Eingangswert
int lastButtonState3 = 0;

//Zeit seit der letzten Entprellung
int lastDebounceTime1 = 0;

//Zeit seit der letzten Entprellung
int lastDebounceTime2 = 0;

//Zeit seit der letzten Entprellung
int lastDebounceTime3 = 0;

//Entprellzeit
int debounceDelay = 50;

//Positionierung beim Scrollen durch Menü
int menuPosition = 1;
int settingsPosition = 0;

//Eingabemodus
bool settings = false;

//Werte der zu schneidenden Kabel
int cableLength1 = 0;
int cableLength2 = 0;
int cableNumber1 = 0;
int cableNumber2 = 0;
int cableLength3 = 0;
int cableLength4 = 0;
int cableNumber3 = 0;
int cableNumber4 = 0;

// Zur Wahl von Kabellänge oder Stückzahl
bool changeLength = true;

// Kabellänge der zu schneidenden Kabel
unsigned int cableLength = 0;

// Stückzahl der zu schneidenden Kabel
unsigned int cableNumber = 0;

//Geschnittenen Kabel
unsigned int alreadyCutted = 0;

//Menü-Taster
const int button1 = 2; // button1 - 3: Menüwahl-Taster; button1: Scrollen; button2: Wahl; button3: Zurück
const int button2 = 3;
const int button3 = 4;

// Bibliotheken initializieren
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
Stepper stepper(STEPS, 6, 7, 8, 9);

// Prädeklarationen
void buttonReading();
void menuTasterAction();
void checkButton(int voltage);
void pressButton(int buttonIndex);
void startCutting();
bool isPliersCompressed();
bool isPliersOpen();
void cutt();
void lcdDisplay(unsigned int alreadyCutted);
void lcdDisplaySettings();


/**********************************************
  Initializiere Bibliotheken und zeige Boot-Nachricht
 **********************************************/
void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2); // Initialisiere LCD
  lcd.backlight();

  //Input-Pins mit internem Pullup für Menü-Taster
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);

  // rpm des Steppermotors
  stepper.setSpeed(30);

  lcd.setCursor(0, 0);
  lcd.print("Welcome!");
  delay(1000);

  // System is starting...
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("System is");
  lcd.setCursor(0, 1);
  lcd.print("starting...");

  cutt();

  // System is ready now!
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("System is");
  lcd.setCursor(0, 1);
  lcd.print("ready now!");
  delay(2000);

  lcdDisplay(alreadyCutted);
}

/**********************************************
  Hauptloop mit Knopfentprellung
 **********************************************/
void loop() {
  int i;
  i = menuPosition;
  buttonReading();
  if (menuPosition == 5) {
    lcdDisplaySettings();
  }
  if (i != menuPosition) {
    lcdDisplay(alreadyCutted);
  }
}

void buttonReading () {
  lastButtonReadTime = millis();
  if (lastButtonReadTime != millis()) {
    int voltage = analogRead(A0);
    int reading1 = digitalRead(button1);
    int reading2 = digitalRead(button2);
    int reading3 = digitalRead(button3);


    //Entprellen von Menü-Taster 1
    if (reading1 != lastButtonState1) {
      lastDebounceTime1 = millis();
    }
    if ((millis() - lastDebounceTime1) > debounceDelay) {
      if (reading1 == 0 && lastButtonState1 == !reading1 ) {
        Serial.println ("Scrollen+");
        menuTasterAction (1);
      }
    }

    //Entprellen von Menü-Taster 2
    if (reading2 != lastButtonState2) {
      lastDebounceTime2 = millis();
    }
    if ((millis() - lastDebounceTime2) > debounceDelay) {
      if (reading2 == 0 && reading2 != lastButtonState2) {
        Serial.println ("Scrollen-");
        menuTasterAction (2);
      }
    }

    //Entprellen von Menü-taster 3
    if (reading3 == 0 && reading3 != lastButtonState3) {
      lastDebounceTime3 = millis();
    }
    if ((millis() - lastDebounceTime3) > debounceDelay) {
      if (reading3 == 0 && reading3 != lastButtonState3 ) {
        Serial.println ("Auswählen/Menü");
        menuTasterAction (3);
      }
    }

    //Abspeichern der Tasterzustände
    lastButtonState1 = reading1;
    lastButtonState2 = reading2;
    lastButtonState3 = reading3;

    // Zählt die ms, bei denen eine andere als die letzte Spannung analog
    if (voltage != lastButtonVoltage) {
      changedVoltageTime++;
    } else if (changedVoltageTime > 0) {
      changedVoltageTime--;
    }

    // Wenn die letzten 50ms eine andere Spannung als die letzte anlag
    if (changedVoltageTime >= 50) {
      changedVoltageTime = 0;
      lastButtonVoltage = voltage;

      if (voltage > 0) {
        checkButton(voltage);
      }
    }
  }
}
//Aktion bei Druck der Menü-Taster 1-3
void menuTasterAction (int action) {
  //Nach unten Scrollen
  if (action == 1) {
    if (menuPosition < 5) {
      menuPosition++;
      Serial.println(menuPosition);
    }
  }
  //Nach oben Scrollen
  if (action == 2) {
    if (menuPosition > 1) {
      menuPosition--;
      Serial.println(menuPosition);
    }
  }
  //Wahl des Eingabemodus
  if (action == 3) {
    settings = !settings;
    Serial.println (settings);
  }
}

/**********************************************
  Führt die entsprechende Aktion je nach Spannung aus
 **********************************************/
void checkButton(int voltage) {
  for (int i = 0; i < 5; i++) {
    // Voltage wird gegenüber min und max Grenzwert verglichen.
    if (voltage >= voltages[i][0] && voltage <= voltages[i][1]) {
      pressButton(i);
      lcdDisplay(alreadyCutted);
      return;
    }
  }
}

/**********************************************
  Aktion des entsprechenden Knopfes wird ausgeführt
 **********************************************/
void pressButton(int buttonIndex) {
  switch (buttonIndex) {
    case 0:
      if (settings = true ) {
        Serial.println("Kabellänge/Stückzahl");
        changeLength = !changeLength;
      }
      break;
    case 1:
      if (menuPosition == 5 && settingsPosition == 3) {
        SPc++;
        Serial.println (SPc);
        break;
      }
      if (changeLength == true) {

        if (cableLength1 < 100 && menuPosition == 1) {
          cableLength1++;
          Serial.println("Length + 1");
          break;
        }

        if (cableLength2 < 100 && menuPosition == 2) {
          cableLength2++;

          Serial.println("Length + 1");
          break;
        }

        if (cableLength3 < 100 && menuPosition == 3) {
          cableLength3++;

          Serial.println("Length + 1");
          break;
        }
        if (cableLength4 < 100 && menuPosition == 4) {
          cableLength4++;

          Serial.println("Length + 1");
          break;
        }
      }
      if (changeLength == false) {
        if (cableNumber1 < 100 && menuPosition == 1) {
          if (menuPosition == 1) {
            cableNumber1++;
            Serial.println("Number + 1");
            break;
          }
        }
        if (cableNumber2 < 100 && menuPosition == 2) {
          cableNumber2++;
          Serial.println("Number + 1");
          break;
        }

        if (cableNumber3 < 100 && menuPosition == 3) {
          cableNumber3++;
          Serial.println("Number + 1");
          break;
        }

        if (cableNumber4 < 100 && menuPosition == 4) {
          cableNumber4++;
          Serial.println("Number + 1");
          break;
        }
      }
      break;
    case 2:
      if (menuPosition == 5 && settingsPosition == 3) {
        SPc--;
        Serial.println (SPc);
        break;
      }
      if (changeLength == true) {
        if (cableLength1 > 0 && menuPosition == 1) {
          cableLength1--;
          Serial.println("Length - 1");
          break;
        }

        if (cableLength2 > 0 && menuPosition == 2) {
          cableLength2--;
          Serial.println("Length - 1");
          break;
        }

        if (cableLength3 > 0 && menuPosition == 3) {
          cableLength3--;
          Serial.println("Length - 1");
          break;
        }

        if (cableLength4 > 0 && menuPosition == 4) {
          cableLength4--;
          Serial.println("Length - 1");
          break;
        }
      }
      if (changeLength == false) {
        if (cableNumber1 > 0 && menuPosition == 1) {
          if (menuPosition == 1) {
            cableNumber1--;
            Serial.println("Number - 1");
            break;
          }
        }
        if (cableNumber2 > 0 && menuPosition == 2) {
          cableNumber2--;
          Serial.println("Number - 1");
          break;
        }

        if (cableNumber3 > 0 && menuPosition == 3) {
          cableNumber3--;
          Serial.println("Number - 1");
          break;
        }

        if (cableNumber4 > 0 && menuPosition == 4) {
          cableNumber4--;
          Serial.println("Number - 1");
          break;
        }
      }
      break;
    case 3:

      Serial.println("Start");
      startCutting();

      break;
    case 4:
      Serial.println("Einzug");
      stepper.step(50);
      cutt();
      break;
  }
}


/**********************************************
  Schneide entsprechende Anzahl der Kabel mit entsprechender Länge ab
 **********************************************/
void startCutting() {
  lcd.noCursor();
  menuPosition = 1;
  lcdDisplay(alreadyCutted);

  //Schneidevorgang für Einstellbereich 1
  if (menuPosition == 1 && cableNumber1 != 0) {
    alreadyCutted = 0;
    lcdDisplay(alreadyCutted);
    while (alreadyCutted < cableNumber1 ) {
      stepper.step(cableLength1 * SPc);
      cutt();
      alreadyCutted++;
      lcdDisplay(alreadyCutted);
    }
    if (cableNumber1 == alreadyCutted) {
      menuPosition++;
      alreadyCutted = 0;

    }
  }

  //Schneidevorgang für Einstellbereich 2
  if (menuPosition == 2 && cableNumber2 != 0) {
    alreadyCutted = 0;
    lcdDisplay(alreadyCutted);
    while ( alreadyCutted < cableNumber2 ) {
      stepper.step(cableLength2 * SPc);
      cutt();
      alreadyCutted++;
      lcdDisplay(alreadyCutted);
    }
    if (cableNumber2 == alreadyCutted) {
      menuPosition++;
      alreadyCutted = 0;

    }
  }

  //Schneidevorgang für Einstellbereich 3
  if (menuPosition == 3 && cableNumber1 != 0) {
    alreadyCutted = 0;
    lcdDisplay(alreadyCutted);
    while (alreadyCutted < cableNumber3 ) {
      stepper.step(cableLength3 * SPc);
      cutt();
      alreadyCutted++;
      lcdDisplay(alreadyCutted);
    }
    if (cableNumber3 == alreadyCutted) {
      menuPosition++;
      alreadyCutted = 0;

    }
  }

  //Schneidevorgang für Einstellbereich 4
  if (menuPosition == 4 && cableNumber4 != 0) {
    alreadyCutted = 0;
    lcdDisplay(alreadyCutted);
    while ( alreadyCutted < cableNumber4 ) {
      stepper.step(cableLength4 * SPc);
      cutt();
      alreadyCutted++;
      lcdDisplay(alreadyCutted);
    }
    if (cableNumber4 == alreadyCutted) {
      menuPosition = 1;
      alreadyCutted = 0;

    }
  }
}
/**********************************************
  Gibt an, ob die Zange zusammengezogen ist
 **********************************************/
bool isPliersCompressed() {
  delay(50);
  if (analogRead(A1) == 0 && analogRead(A2) != 0) {
    delay(30);

    return analogRead(A1) == 0 && analogRead(A2) != 0;
  } else {
    return false;
  }
}

/**********************************************
  Gibt an, ob die Zange ganz offen ist
 **********************************************/
bool isPliersOpen() {
  delay(50);
  if (analogRead(A2) == 0 && analogRead(A1) != 0) {
    delay(30);

    return analogRead(A2) == 0 && analogRead(A1) != 0;
  } else {
    return false;
  }
}



/**********************************************
  Mit der Zange einmal das Kabel abschneiden
 **********************************************/
void cutt() {
  // Zuziehen solange die Zange nicht ganz zusammengedrückt ist
  digitalWrite(13, HIGH);

  while (!isPliersCompressed()) {
    delay (20);
  }

  // Öffnen solange die Zange nicht ganz offen ist
  digitalWrite(13, LOW);
  delay(50);
  digitalWrite(10, HIGH);

  while (!isPliersOpen()) {
    delay(20);
  }

  if (isPliersOpen() == true) {
    digitalWrite(10, LOW);
  }
  delay(50);
}
void lcdDisplaySettings() {
  settingsPosition = 0;
  settings = false;
  buttonReading();
  for (menuPosition == 5; settings != true; ) {
    buttonReading();

    while ( settingsPosition == 0) {
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print ("Einstellungen");
      settingsPosition++;
    }
  }
  for (menuPosition == 5; settings == true; settingsPosition != 3) {
    buttonReading();

    while (settingsPosition  == 1 ) {
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print ("SPc:");
      lcd.setCursor(5, 0);
      lcd.print (SPc);
      settingsPosition++;
    }
  }
  for (menuPosition == 5; settings != true; settingsPosition != 1 ) {
    if (settingsPosition == 2) {
      settingsPosition++;
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print ("SPc:");
      lcd.setCursor(5, 0);
      lcd.print (SPc);
      lcd.cursor();
      lcd.setCursor(5, 0);
    }
    int SPcNew;
    SPcNew = SPc;
    buttonReading();
    if (SPc != SPcNew) {
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print ("SPc:");
      lcd.setCursor(5, 0);
      lcd.print (SPc);
      lcd.cursor();
      lcd.setCursor(5, 0);
    }


  }


  menuPosition = 1;
  settingsPosition = 0;
}

/**********************************************
  Gibt die Länge, den Ist- und den Sollwert der Kabel am LCD aus.
 **********************************************/
void lcdDisplay(unsigned int alreadyCutted) {
  lcd.clear();
  lcd.noCursor();

  // Erste Zeile
  lcd.setCursor(0, 0);
  lcd.print("Kabell.:");
  lcd.setCursor(9, 0);
  if (menuPosition == 1) {
    lcd.print(cableLength1);
  }
  if (menuPosition == 2) {
    lcd.print(cableLength2);
  }

  if (menuPosition == 3) {
    lcd.print(cableLength3);
  }

  if (menuPosition == 4) {
    lcd.print(cableLength4);
  }
  lcd.setCursor(12, 0);
  lcd.print("cm");



  // Zweite Zeile
  lcd.setCursor(3, 1);
  lcd.print(alreadyCutted);
  lcd.setCursor(6, 1);
  lcd.print("/");
  lcd.setCursor(9, 1);
  if (menuPosition == 1) {
    lcd.print(cableNumber1);
  }
  if (menuPosition == 2) {
    lcd.print(cableNumber2);
  }

  if (menuPosition == 3) {
    lcd.print(cableNumber3);
  }

  if (menuPosition == 4) {
    lcd.print(cableNumber4);
  }

  lcd.setCursor(13, 1);
  lcd.print(menuPosition);
  lcd.setCursor(14, 1);
  lcd.print("/4");


  // Setzte den blinkenden Cursor auf den ausgewählten Wert
  // wenn gerade kein Schneidevorgang läuft
  if (alreadyCutted == 0 && settings == true) {
    lcd.cursor();

    if (changeLength) {
      lcd.setCursor(9, 0);
    } else {
      lcd.setCursor(9, 1);
    }
  }
}

Discussions