'._('einklappen').'
 

Steuerungssoftware

zur Anleitung "Stackingsteuerung"


// Befehle uber Serielle oder IR und Ausgabe Seriell oder Display
//by Stefan Schorn https://www.mineralienatlas.de
//20180203 V. 1.0.3

#include <AccelStepper.h> //Steppermotor
#include <IRremote.h> // IR Remote
#include <LiquidCrystal_I2C.h>

AccelStepper stepper(AccelStepper::DRIVER, 13, 12);

int spd = 512;    // The current speed in steps/second
int sign = 1;      // Either 1, 0 or -1
String inString = ""; //Serial read more letters
String ctlString = ""; //one letter String for commands
String leer = "                    ";  //for delete end of display line
String txtInfoOne = ""; // text in first line
int intString = 0;
int start = 0;
int ende = 0;
int doitonce = 0; //Trigger fuer Pause nur einmal Anzeige update
int setInfoTwo = 0; //Display zweite Zeile
int countpic = 0;
int actpic = 0;
int dostep = 0;
int stopstack = 0;
int stackpos = 0;
int dopause = 0; //Pause
int step = 12;
int CAMERA = 10;
int stopnow = 11;
int stopnowv = 8;
int runback = 0;
int runbackv = 0;
int RECV_PIN = 9;

IRrecv irrecv(RECV_PIN);  // Bibliothek den Eingangs Pin mitteilen
decode_results results; // Die Ergebnisse decodieren
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

void setup()
{
  Serial.begin(115200);
  stepper.setMaxSpeed(5000);
  stepper.setSpeed(0);
  pinMode(CAMERA, OUTPUT);   //Pin 10 als output initialisieren
  irrecv.enableIRIn();
  lcd.begin(20, 4);  // initialize the lcd for 29 chars 4 lines, turn on backlight
  // ------- Quick 3 blinks of backlight  -------------
  for (int i = 0; i < 3; i++) {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on

  //-------- Write characters on the display ------------------
  // NOTE: Cursor Position: (CHAR, LINE) start at 0
  lcd.setCursor(0, 0); //Start at character 0 on line 0
  lcd.print("Hello,");
  delay(500);
  lcd.setCursor(0, 1);
  lcd.print("I'm ready.");
  delay(500);
  lcd.setCursor(0, 2);
  lcd.print("Lets make nice");
  lcd.setCursor(0, 3);
  lcd.print("pictures.");
}

void loop() {
  char c;
  if (Serial.available()) {
    c = Serial.read();
    inString += c;
    ctlString = inString;
    /*
        while (Serial.available() > 0) {
          c = Serial.read();
          if (c == '!') {
            Serial.println(inString);
            ctlString = inString;
          } else {
            inString += c;
          }
        }
    */

    /* -----------------------------------------------------------------------------------
      Serial commands
      ------------------------------------------------------------------------------------*/

    intString = atoi(ctlString.c_str());
    Serial.println(intString);

    if (ctlString == "f") {  // up
      doIt("up");
    } else if (ctlString == "z") {  // zurueck
      doIt("down");
    } else if (ctlString == "p") {  // pause
      doIt("pause");
    } else if (ctlString == "v") {  // vorwaerts
      doIt("up");
    } else if (ctlString == "r") {  // reverse
      doIt("down");
    } else if (ctlString == "s") {  // stop
      doIt("stop");
    } else if (ctlString == "a") {  // super slow
      spd = 8;
      stepper.setSpeed(sign * spd);
      dostep = 0;
    } else if (ctlString == "b") {  // medium
      spd = 128;
      stepper.setSpeed(sign * spd);
      dostep = 0;
    } else if (ctlString == "e") {  // fast
      spd = 1024;
      stepper.setSpeed(sign * spd);
      dostep = 0;
    } else if (ctlString == "g") {  // fast
      spd = 2048;
      stepper.setSpeed(sign * spd);
      dostep = 0;
    } else if (intString) {  // 1 steps more
      dostep = 0;
      spd = 128;
      stepper.setSpeed(sign * spd);
      if (sign == -1) {
        stepper.runToNewPosition(stepper.currentPosition() - intString);
      }
      if (sign == 1 || sign == 0) {
        stepper.runToNewPosition(stepper.currentPosition() + intString);
      }
    } else if (ctlString == "t") {  // test
      doIt("test");
    } else if (ctlString == "c") {  // clear start
      dostep = 0;
      stepper.setCurrentPosition(0);
    } else if (ctlString == "m") {  // 5 steps Mitutoyo
      step = 5;
    } else if (ctlString == "n") {  // 3 steps Mitutoyo
      step = 3;
    } else if (ctlString == "l") {  // 20 steps Lumo
      step = 20;
    } else if (ctlString == "h") {  // 100 steps
      step = 99;
    } else if (ctlString == "x") {  // startposition festsetzen
      doIt("startpos");
    } else if (ctlString == "y") {  // endeposition festsetzen
      doIt("endpos");
    } else if (ctlString == "k") {  // start Stack 5 steps abstand
      doIt("stack");
    } else if (ctlString == "d") {  // debug
      doIt("debug");
    }
  }

  /* -----------------------------------------------------------------------------------
    IR commands
    ------------------------------------------------------------------------------------*/
  if (irrecv.decode(&results)) { // Wenn die IR Bibliothek ein decodiertes Signal liefert ...

    if (!(results.value == 4294967295)) { // .. ueberflussige Codes nach jedem Befehl warum? Daher weg damit

      Serial.println(results.value, DEC); // ... dann soll dieses auf der Seriellen Schnittstelle ausgegeben werden

      if (results.value == 49760 || results.value == 4252731927 || results.value == 16734885) { // vorwaerts >>
        doIt("up");
      } else if (results.value == 49856 || results.value == 3891845595 || results.value == 16726215) { //stop Stoptaste
        doIt("stop");
      } else if (results.value == 49888 || results.value == 1055068007 || results.value == 16716015) { //zurueck <<
        doIt("down");
      } else if (results.value == 49816 || results.value == 1273413121 || results.value == 16738455) { //startposition festsetzen Pr+
        doIt("startpos");
      } else if (results.value == 49688 || results.value == 561578551 || results.value == 16756815) { //endeposition festsetzen Pr-
        doIt("endpos");
      } else if (results.value == 49852 || results.value == 2549732789 || results.value == 16753245) {  // debug (i Taste)
        doIt("debug");
      } else if (results.value == 49724 || results.value == 1302679411 || results.value == 16750695) {  // start Stacks (ok Enter)
        doIt("stack");
      } else if (results.value == 49769 || results.value == 3950443920 || results.value == 16736925) {  // test Taste 39SEC
        doIt("test");
      } else if (results.value == 49840 || results.value == 812884989 || results.value == 16769565) {  // pause
        doIt("pause");
      } else if (results.value == 49304 || results.value == 3100718103 || results.value == 16720605) {  // linkes +
        doIt("stepsplus");
      } else if (results.value == 49176 || results.value == 698936137 || results.value == 16761405) {  // linkes -
        doIt("stepsminus");
      } else if (results.value == 49272 || results.value == 3416790753 || results.value == 16718055) {  // rechtes +
        doIt("speedplus");
      } else if (results.value == 49400 || results.value == 1138136617 || results.value == 16730805) {  // rechtes -
        doIt("speedminus");
      } else if (results.value == 49704 || results.value == 910109905 || results.value == 16769055) {  // + >>|
        doIt("plussteps");
      } else if (results.value == 49832 || results.value == 2940092199 || results.value == 16748655) {  // - |<<
        doIt("minussteps");
      }
    }
    irrecv.resume(); // neue Messung durchführen
  }

  /* -----------------------------------------------------------------------------------
    Stepping
    ------------------------------------------------------------------------------------*/
  if (dostep == 1 && !dopause) {
    doitonce = 0;
    if (stackpos < ende) {
      delay(2000);
      txtInfoOne = "no vibrancy pause...";
      doIt("setInfoOne");
      doIt("setInfoTwo");
      delay(4000);
      txtInfoOne = "photo now ...";
      actpic = abs(floor(((ende - stackpos) / step) + 0.5)); // recent picture
      doIt("setInfoOne");
      doIt("setInfoTwo");
      digitalWrite(CAMERA, HIGH); //Kamera auslösen
      delay(500);                //kurz warten
      digitalWrite(CAMERA, LOW);
      delay(500);
      txtInfoOne = "run to new pos ...";
      doIt("setInfoOne");
      doIt("setInfoTwo");
      delay(500);
      stepper.setSpeed(1000);
      stackpos = stackpos + step; // step
      stepper.runToNewPosition(stackpos);
      doIt("serialout");
    } else {
      lcd.setCursor(0, 0);
      txtInfoOne = "stack finished";
      doIt("setInfoOne");
      doIt("setInfoTwo");
      dostep = 0;
    }
  } else if (dostep == 1 && dopause == 1) {
    if (doitonce == 0) {
      txtInfoOne = "pause";
      doIt("setInfoOne");
      doIt("setInfoTwo");
      doitonce = 1;
    }
  }

  //Endanschlag
  int stopstate = digitalRead(stopnow);
  if (stopstate == 1) {
    txtInfoOne = "end stop reached";
    doIt("txtInfoOne");
    stepper.setSpeed(1000);
    dostep = 0;
    runback = 1;
  }

  if ((stopstate == 0) && (runback == 1)) {
    stepper.setSpeed(0);
    dostep = 0;
    stepper.setSpeed(0);
    runback = 0;
  }

  //Vorneanschlag
  int stopstatev = digitalRead(stopnowv);
  if (stopstatev == 1) {
    txtInfoOne = "end stop reached";
    doIt("txtInfoOne");
    stepper.setSpeed(-1000);
    dostep = 0;
    runbackv = 1;
  }

  if ((stopstatev == 0) && (runbackv == 1)) {
    stepper.setSpeed(0);
    dostep = 0;
    stepper.setSpeed(0);
    runbackv = 0;
  }

  /* -----------------------------------------------------------------------------------
    Something else ...
    ------------------------------------------------------------------------------------*/
  stepper.runSpeed();

  ctlString = "";
  inString = "";

  if (c == '!') {
    ctlString = "";
    inString = "";
  }
}

/* -----------------------------------------------------------------------------------
  Here we do the action for the commands
  ------------------------------------------------------------------------------------*/
void doIt(String what) {

  if (what == "up") { //vorwaerts v, f

    sign = 1;
    stepper.setSpeed(sign * spd);
    dostep = 0;
    txtInfoOne = "up";
    doIt("setInfoOne");
    doIt("setInfoTwo");

  } else if ( what == "stop") { //stop s

    sign = 0;
    stepper.setSpeed(sign * spd);
    dostep = 0;
    stackpos = 0;
    txtInfoOne = "stop";
    doIt("setInfoOne");
    stepper.runToNewPosition(stepper.currentPosition()); //stepper.isRunning() setzt sich nur mit am Ziel auf false; so false erzwingen
    doIt("setInfoTwo");

  } else if ( what == "down") { //zurueck << z

    sign = -1;
    stepper.setSpeed(sign * spd);
    dostep = 0;
    txtInfoOne = "down";
    doIt("setInfoOne");

  } else if ( what == "startpos") { //startposition festsetzen Pr+ x

    stepper.setCurrentPosition(0);
    start = 0;
    txtInfoOne = "start pos set";
    doIt("setInfoOne");
    doIt("setInfoTwo");
    dostep = 0;

  } else if ( what == "endpos") { //endeposition festsetzen Pr- y

    ende = stepper.currentPosition();
    txtInfoOne = "end pos set";
    doIt("setInfoOne");
    if (ende - stackpos == 0) {
      actpic = 0;
    } else {
      actpic = abs(ceil(((ende - stackpos) / step) + 0.5)); // recent picture
    }
    doIt("setInfoTwo");
    dostep = 0;


  } else if ( what == "debug") { // debug (i Taste) d

    doIt("setInfoTwo");
    doIt("serialout");

  } else if ( what == "stack") { //start Stacks (ok Enter) k

    if (ende - stackpos == 0) {
      actpic = 0;
      txtInfoOne = "nothing todo ..";
      doIt("setInfoOne");
      doIt("setInfoTwo");
      dostep = 0;
    } else {
      actpic = abs(ceil(((ende - stackpos) / step) + 0.5)); // recent picture
      txtInfoOne = "goto start pos...";
      doIt("setInfoOne");
      doIt("setInfoTwo");
      stepper.setSpeed(1000);
      stepper.setAcceleration(500); //Neukalkulation der Geschwindigkeit, machts schneller
      stepper.runToNewPosition(0);
      dostep = 1;
      Serial.println("----------------------");
      Serial.println("start stack");
    }

  } else if ( what == "test") { //test Taste 30SEC t

    txtInfoOne = "goto pos 500";
    doIt("setInfoOne");
    stepper.setSpeed(2000);
    //stepper.runSpeedToPosition();
    stepper.setAcceleration(500);
    stepper.runToNewPosition(500);
    //stepper.moveTo(500);
    //stepper.setSpeed(1000);
    dostep = 1;
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("test");

  } else if ( what == "pause") { //pause p

    if (dopause) {
      dopause = 0;
    } else {
      dopause = 1;
    }
    Serial.println("----------------------");
    Serial.println("pause");

  } else if ( what == "stepsplus") { //linkes +

    dostep = 0;
    spd = 1000;
    intString = 5;
    sign = 0;
    stepper.setSpeed(sign * spd);
    stepper.setAcceleration(500);
    stepper.runToNewPosition(stepper.currentPosition() + intString);
    if (ende - stackpos == 0) {
      actpic = 0;
    } else {
      actpic = abs(ceil(((ende - stackpos) / step) + 0.5)); // recent picture
    }
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("steps minus");

  } else if ( what == "stepsminus") { // linkes -

    dostep = 0;
    spd = 1000;
    intString = 5;
    sign = -1;
    stepper.setSpeed(sign * spd);
    stepper.setAcceleration(500);
    stepper.runToNewPosition(stepper.currentPosition() - intString);
    if (ende - stackpos == 0) {
      actpic = 0;
    } else {
      actpic = abs(ceil(((ende - stackpos) / step) + 0.5)); // recent picture
    }
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("steps minus");

  } else if ( what == "speedplus") { //rechtes +

    spd = spd * 2;
    if (spd > 6400) spd = 6400;
    if (stepper.isRunning()) {
      stepper.setSpeed(sign * spd);
    }
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("speed plus");

  } else if ( what == "speedminus") { // rechtes -
    spd = spd / 2;
    if (spd < 1) spd = 1;
    if (stepper.isRunning()) {
      stepper.setSpeed(sign * spd);
    }
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("speed minus");

  } else if ( what == "plussteps") { //+ >>|

    step = step + 3;
    if (step > 99) step = 99;
    setInfoTwo = 1;
    if (ende - stackpos == 0) {
      actpic = 0;
    } else {
      actpic = abs(ceil(((ende - stackpos) / step) + 0.5)); // recent picture
    }
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("plus 3 steps");

  } else if ( what == "minussteps") { // - |<<

    step = step - 3;
    if (step < 3) step = 3;
    setInfoTwo = 1;
    if (ende - stackpos == 0) {
      actpic = 0;
    } else {
      actpic = abs(ceil(((ende - stackpos) / step) + 0.5)); // recent picture
    }
    doIt("setInfoTwo");
    Serial.println("----------------------");
    Serial.println("minus 3 steps");

  } else if (what == "setInfoTwo") {
    /*second and more lines */

    String stringA = "space ";
    String stringB = "spd. ";
    String stringC = ", ";
    lcd.setCursor(0, 1);
    lcd.print(leer);
    lcd.setCursor(0, 1);
    lcd.print(stringA + step + stringC + stringB + spd);
    Serial.println("----------------------");
    Serial.println(stringA + step + stringC + stringB + spd);

    String stringD = "start  ";
    String stringE = "end ";
    lcd.setCursor(0, 2);
    lcd.print(leer);
    lcd.setCursor(0, 2);
    lcd.print(stringD + start + stringC + stringE + ende);
    Serial.println(stringD + start + stringC + stringE + ende);

    if (ende - start == 0) {
      countpic = 0;
    } else {
      countpic = abs(ceil(((ende - start) / step) + 0.5)); //max pitures
    }
    String stringF = "to do  ";
    String stringG = "of ";
    lcd.setCursor(0, 3);
    lcd.print(leer);
    lcd.setCursor(0, 3);
    lcd.print(stringF + actpic + stringC + stringG + countpic);
    Serial.println(stringF + actpic + stringC + stringG + countpic);

  } else if (what == "setInfoOne") {
    /* erste Zeile am Display */

    lcd.setCursor(0, 0);
    lcd.print(leer);
    lcd.setCursor(0, 0);
    lcd.print(txtInfoOne);
    Serial.println("");
    Serial.println(txtInfoOne);
    Serial.println("");

  } else if (what == "serialout") { //Serielle Ausgabe

    //Ausgabe der Stepperposition
    Serial.println("----------------------");
    String pos = "curr. pos. ";
    Serial.println(pos + stepper.currentPosition());
    String dir = "direction: ";
    Serial.println(dir + sign);
  }
}