Auto Tuning using ziegler nichols ??

I am trying to implement an autotuning using the ziegler nichols step response to an RC circuit . Using Proteus 8 to simulate the program "see attached image ". I used a battery to perform a step input . I also used the matlab to get the correct Kp,Ki,Kd and also the correct ydotmax and tmax . The main idea is to get the maximum change during the response (ydotmax) and the time at which that maximum change happened . But in arduino wrong results that is very far from the right ones . This is the code i am using :

-#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
const int controlout = 9 ;
float output ,slope , ydot ,ylast , ydotmax ,ymax,yo,to,kp,ki,kd,tmax;
unsigned long dt,tlast ,time;

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  lcd.clear();
}

void loop() {
  time = millis() ;
  output = analogRead(A0) ;
  output = output *(5.0/1024);
  
 
  dt = time - tlast;
  
  slope= ((output-ylast)*1000.0)/dt ;
  
  if(slope > ydotmax)
  {
    ydotmax = slope ; tmax = time/1000.0 ; ymax=output ;
  }
  
 yo= ydotmax*tmax - ymax ;
 to= yo/ydotmax ;
 kp=1.2/yo ;
 ki= 0.6/(yo*to);
 kd=(0.6*to)/yo ;
  
    
  lcd.setCursor(0,0);lcd.print(kp) ;  lcd.setCursor(8,0);lcd.print(ki) ;
  lcd.setCursor(0,1);lcd.print(kd) ; 


  ylast=output ;
  tlast=time ; 
  delay(10);
}

I don't know what is the problem , the code seems simple .