   return temp; //return converted variable
 }
 
-//Calculate the current motor using
+//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 - 2; //subtracting sensor offset
+  tempFloat = tempFloat - currentOffset; //subtracting sensor offset
   tempFloat = tempFloat / 0.185; //mapping sensor output 'Voltage' to 'Ampere', 185mV/A
   return tempFloat;
 }
 
-//adds one to billCount: used as ISR 'called when IR sensor detects new bill'
-void counter() {
-  billCount = billCount + 1;
-}
+//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);
+}

////////////////////////////////////////////////////////////

//Arduino Pins definition
//stepper-1
#define    stepper11       2
#define    stepper12       3
#define    stepper13       4
#define    stepper14       5
//stepper-2
#define    stepper21       6
#define    stepper22       7
#define    stepper23       8
#define    stepper24       9
//other outputs
#define    drawer         10
#define    warehouse      11
#define    relay          12
//sensors
#define    infrared        13
#define    current         A0
#define    temperature     A1
#define    lightLevel      A2

#define    doorDelay        1000
#define    warehouseDelay   1000

//variables definition
char  serialData; //for storing serial string
float tempFloat = 0; //for saving sensor data temporary
int billCount = 0; //for storing bill count added by ISR

void setup() {
  //Setting input/output Pins
  //outputs
  pinMode(  stepper11,  OUTPUT);
  pinMode(  stepper12,  OUTPUT);
  pinMode(  stepper13,  OUTPUT);
  pinMode(  stepper14,  OUTPUT);
  pinMode(  stepper21,  OUTPUT);
  pinMode(  stepper22,  OUTPUT);
  pinMode(  stepper23,  OUTPUT);
  pinMode(  stepper24,  OUTPUT);
  pinMode(  drawer,     OUTPUT);
  pinMode(  warehouse,  OUTPUT);
  pinMode(  relay,      OUTPUT);
  //inputs
  pinMode(  infrared,    INPUT);
  pinMode(  current,     INPUT);

  }
}

void openDoor(){
  digitalWrite(drawer, HIGH);
  delay(doorDelay);
