   }
 }
 
-void hardwareDispense(){
-  int bills[4];
-  int total = 0;
-  for (int i = 0; i<=4; i++){
-    while(Serial.available() <= 0){ //wait until Matlab sends voltage on serial buffer
-    }
-    serialData = Serial.read(); //get the desired count from serial
-    bills[i] = char2Int(serialData); //converting serial string into int
-    total = total + bills[i];
-  }
-  //define IR sensor pin as interrupt pin to add one automatically to bills count
-  attachInterrupt(digitalPinToInterrupt(infrared), counter, RISING);
-
-  //TODO: Dispense here
-  
-  //check number of dispensed bills
-  if(billCount == total)
-    Serial.print("t");
-  else if(billCount != total)
-    Serial.print("f");
-  billCount = 0; //resets billCount
-}
-
 void openDoor(){
   digitalWrite(drawer, HIGH);
   delay(doorDelay);

  Serial.begin(9600);
}

//Check serial buffer for any strings from Matlab and call Corresponding Function
void loop() {
  if(Serial.available() > 0) { //Check serial buffer if any data available
    serialData = Serial.read(); //read string from serial buffer

  return temp; //return converted variable
}

//adds one to billCount: used as ISR 'called when IR sensor detects new bill'
void counter() {
  billCount = billCount + 1;
}

//Calculate the current pump using
float getCurrent() {
  tempFloat = analogRead(current); //read from motor current sensor pin
  tempFloat = (tempFloat * 5) / 1024; //converting binary number to voltage (0-1024)->(0-5)
  tempFloat = tempFloat - currentOffset; //subtracting sensor offset
  tempFloat = tempFloat / 0.185; //mapping sensor output 'Voltage' to 'Ampere', 185mV/A
  return tempFloat;
}

//dispenses given bill array
//array is given via serial port from RPI
void hardwareDispense(){
  int bills[4];
  int total = 0;
  for (int i = 0; i<=4; i++){
    while(Serial.available() <= 0){ //wait until RPI sends voltage on serial buffer
    }
    serialData = Serial.read(); //get the desired count from serial
    bills[i] = char2Int(serialData); //converting serial string into int
    total = total + bills[i];
  }
  //define IR sensor pin as interrupt pin to add one automatically to bills count
  attachInterrupt(digitalPinToInterrupt(infrared), counter, RISING);

  //dispense bill array
  for (int i = 0 ; i < 4; i++){
    for (int j = bills[i]; j > 0; j--){
      dispenseOneBill(i+1); //dispense one bill of warehouse number i+1
      //as warehouse 0 is the empty one and 1 to 4 is the drugs' warehouses
    }
  }
  stopMotors(); //stop all motors "set all pins to low"
  //check number of dispensed bills
  if(billCount == total)
    Serial.print("t");
  else if(billCount != total)
    Serial.print("f");
  billCount = 0; //resets billCount
}

//dispense one bill from given warehouse number
void dispenseOneBill(int warehouseNumber){
  moveToWarehouse(warehouseNumber); //move to drug warehouse
  getBill(); //get one bill using pump
  moveToWarehouse(0); //return to empty warehouse
  releaseBill(); //release bill to the door
}

//move to given warehouse number
void moveToWarehouse(int warehouseNumber){
  int steps = 0;
  if(warehouseNumber == 0){
    steps = 5 - selectedWarehouse;
  }else{ //empty warehouse selected
    steps = warehouseNumber;
  }
  for (int i = 0; i < steps; i++){ //move the required steps
    moveOneStep(); //moves 72 degree to select next warehouse
  }
}

//get one bill using pump
void getBill(){
  int steps = 0; //for storing steps to return to initial position again
  digitalWrite(pump, HIGH); //activates the pump
  while(getCurrent() < currentThreeshold && steps < maxUpSteps){ //go down until gets one bill
    stepDown(); //move one step down
  }
  delay(getBillDelay); //to wait until bill catched for sure
  for(int i = 0; i<steps; i++){ //retun to initial position
    stepUp(); //move one step up
  }
}

//release bill to the door
void releaseBill(){
  digitalWrite(pump, LOW); //disable the pump
  delay(releaseBillDelay); //to wait untill bill released
}

//moves 72 degree to select next warehouse
void moveOneStep(){
  for(int i = 0; i < step72; i++){
    for(int j = 0; j <= 7; j++){
      digitalWrite(upPin1, seq[i][0]);
      digitalWrite(upPin2, seq[i][1]);
      digitalWrite(upPin3, seq[i][2]);
      digitalWrite(upPin4, seq[i][3]);
      delay(roStepperDelay);
    }
  }
}

//move one step up
void stepUp(){
  for(int i = 0; i <= 7; i++){
    digitalWrite(upPin1, seq[i][0]);
    digitalWrite(upPin2, seq[i][1]);
    digitalWrite(upPin3, seq[i][2]);
    digitalWrite(upPin4, seq[i][3]);
    delay(upStepperDelay);
  }
}

//move one step down
void stepDown(){
  for(int i = 7; i >= 0; i--){
    digitalWrite(upPin1, seq[i][0]);
    digitalWrite(upPin2, seq[i][1]);
    digitalWrite(upPin3, seq[i][2]);
    digitalWrite(upPin4, seq[i][3]);
    delay(upStepperDelay);
  }
}

//stop all motors "set all pins to low"
void stopMotors(){
  //up/down stepper
  digitalWrite(upPin1, LOW);
  digitalWrite(upPin2, LOW);
  digitalWrite(upPin3, LOW);
  digitalWrite(upPin4, LOW);
  //rotation stepper
  digitalWrite(roPin1, LOW);
  digitalWrite(roPin2, LOW);
  digitalWrite(roPin3, LOW);
  digitalWrite(roPin4, LOW);
  //pump
  digitalWrite(pump, LOW);
}
