int stepCounter;
int steps = 10000;
int delayit = 200; 
int stepload = 3800;
int pinEnable = 8;
int pinStep = 2;
int pinDir =  5;
int state = 0;    // ventil=0 laden == 1;
int CoolEn = 3;
int spindir = 13;
int strechts = 10;
int command = 0;


int pinStepY = 3;    // SpritzenMotor
int pinDirY = 6;
int delayit2 =200;
int volume = 0;

int pinStepZ = 4;    // Pumpenmotor
int pinDirZ = 7;
int delayitZ =2000;
 
void setup()
{
  pinMode(A3,OUTPUT);     // LED On =  inject
  pinMode(spindir,INPUT); // Schalter Ventil

  Serial.begin(115200);
  pinMode(strechts,INPUT); // Home = inject
 
  pinMode(pinEnable, OUTPUT); // Enable
  pinMode(pinStep, OUTPUT); // Step
  pinMode(pinDir, OUTPUT); // Richtung
  digitalWrite(pinEnable,LOW);
  ventil();

  // Spritzen Motor
  pinMode(pinStepY, OUTPUT); // Step
  pinMode(pinDirY, OUTPUT); // Richtung
  pinMode(9,INPUT);
  spmotor_home();

   pinMode(pinStepZ, OUTPUT); // Step
  pinMode(pinDirZ, OUTPUT); // Richtung
}


void loop()
{
  while (Serial.available()>0){
    Serial.println("1)laden 2)ventil 3)sp_home 4)sp_leer 5)vol? 6)doit");
     command = Serial.parseInt();
     if (Serial.read() == '\r') {
        Serial.println(command);
        switch (command) {
          case 1 : laden();
          break;
          case 2 : ventil();
          break;
          case 3 : spmotor_home();
          break;
          case 4 : spmotor_leer();
          break;
          case 5 : Serial.println(volume);
          break;
          case 6 : do_it();
          break;
          case 7 : pumpe();
          break;
          default:   
            if (command < 0){
              command =command*-1;
              spmotor_zurueck(command);
              //Serial.println(command);
            } else {
              spmotor_vor(command);
              //Serial.println(command);
            }
        } 
       
     }
  }
  if (digitalRead(spindir) == LOW) {
    Serial.println("Low");
    delay(100);
    if (state == 1){
      ventil();
    } else {
      laden();
    }    
  }
  
}
int pumpe() {
digitalWrite(pinDirZ,HIGH); // im Uhrzeigersinn 
    for(int d=0;d<50000;d++)
    {
      digitalWrite(pinStepZ,HIGH);
      delayMicroseconds(delayitZ);
      digitalWrite(pinStepZ,LOW);
      delayMicroseconds(delayitZ);   
    }  
  
}
//////////// Programmierte Injektion
int do_it(){
  int anz = 10;
  unsigned long det1 = 40;//40;
  unsigned long det2 = 30;//30;
  int q;
  unsigned long det;
  laden();
  delay(500);
  spmotor_vor(60);
  delay(1000);
  for (q=1;q<=anz;q++){ 
    ventil();
    infoline(q,anz);
    det = det1*1000;
    delay(det);
    laden();    
    delay(500);
    spmotor_vor(30);
    det = det2*1000;
    delay(det);
    //Serial.println(q);
  }
  Serial.println("Finished !");
}
int infoline(int i,int q){
  Serial.print("Volume ");
  Serial.print(volume);
  Serial.print(" Inject ");
  Serial.print(i);
  Serial.print("/");
  Serial.println(q);
  
}
//////////// Spritzenmotorint
int spmotor_home(){
   volume = 360;
   Serial.println(volume);
    digitalWrite(pinDirY,HIGH); // im Uhrzeigersinn 
    while( digitalRead(9)== LOW)
    {
      digitalWrite(pinStepY,HIGH);
      delayMicroseconds(delayit2);
      digitalWrite(pinStepY,LOW);
      delayMicroseconds(delayit2);   
    }  
}
int spmotor_leer(){
   spmotor_vor(volume);
}

int spmotor_zurueck(int x){
  int q =0;
  int p = 0;
  for (q=0;q<=x;q++){
    if (digitalRead(9) == LOW){    
      digitalWrite(pinDirY,HIGH); // im Uhrzeigersinn   
      for(p = 0; p < 500; p++)
      {
        digitalWrite(pinStepY,HIGH);
        delayMicroseconds(delayit2);
        digitalWrite(pinStepY,LOW);
        delayMicroseconds(delayit2);   
      }
      volume++;
      Serial.println(volume);
    }
  }
}

int spmotor_vor(int x){
  int q;
  int p;
  digitalWrite(pinDirY,LOW); // im Uhrzeigersinn 
  //if (volume > 0){
   for (q=0;q<=x;q++){
    for(p = 0; p < 500; p++)
      {
      digitalWrite(pinStepY,HIGH);
      delayMicroseconds(delayit2);
      digitalWrite(pinStepY,LOW);
      delayMicroseconds(delayit2);   
      }
      volume--;
      //Serial.println(volume);
      if (volume <= 0){
        break;
      }
    }
  //} 
}
//////////// Ventil
int laden(){
  if (state == 0 ) {
    state = 1;
    digitalWrite(A3,LOW);
    digitalWrite(pinDir,HIGH); // im Uhrzeigersinn 
    for(stepCounter = 0; stepCounter < stepload; stepCounter++)
    {
      digitalWrite(pinStep,HIGH);
      delayMicroseconds(delayit);
      digitalWrite(pinStep,LOW);
      delayMicroseconds(delayit);   
    }
  }
}

int ventil(){
  state = 0;
  digitalWrite(A3,HIGH);
  digitalWrite(pinDir,LOW); // im Uhrzeigersinn 
  for(stepCounter = 0; stepCounter < steps; stepCounter++)
  {
    if (!checkErechts()){
      digitalWrite(pinStep,HIGH);
      delayMicroseconds(delayit);
      digitalWrite(pinStep,LOW);
      delayMicroseconds(delayit);
    } 
  }
}

int checkErechts(){
  return digitalRead(strechts);
}
