 EngineVariable engine_tcr   = {"TCR", 0.0, 0.0, 0.0, 50.0, 1};
 EngineVariable engine_lct   = {"LCT", 0.0, 0.0, 0.0, 100.0, 1};
 
-long startTime;
-long endTime;
-
-double afr;
-double tgt;
-
 unsigned long interval = 100;
 unsigned long lastMillis = 0;
 unsigned long currentMillis = 0;
-
-int messageReceived;
 
 unsigned long goodDataCount = 0;
 unsigned long badDataCount = 0;

    if(partialBars > 0) {
      lcd.write(byte(partialBars - 1));    
    }  
  }

  
}


          Serial.println(" Init CAN BUS Shield again");
          delay(100);
      }
  Serial.println("COMM OK");
}

void loop() {
  load_from_can();
  
  currentMillis = millis();
  if(currentMillis - lastMillis >= interval) {
    lastMillis = currentMillis;
    if(engine_rpm.currentValue != engine_rpm.previousValue) {
      lcd.setCursor(4, 0);
      if(is_current_value_shorter(engine_rpm)) {
        lcd.print("     ");
        lcd.setCursor(4, 0);
      }
      lcd.print(engine_rpm.currentValue, engine_rpm.decimalPlaces);
      draw_bar(engine_rpm, 1);
    }
    if(engine_afr.currentValue != engine_afr.previousValue) {
      lcd.setCursor(4, 2);
      lcd.print(engine_afr.currentValue);
      draw_bar(engine_afr, 3);      
    }
  }
}

void load_from_can() {
  unsigned char len = 0;
  unsigned char buf[8];
  if(CAN_MSGAVAIL == CAN.checkReceive())            // check if data coming
  {
    CAN.readMsgBuf(&len, buf);    // read data,  len: data length, buf: data buf

    unsigned int canId = CAN.getCanId();
    
    switch(canId) {
      case 1512:
        engine_map.previousValue = engine_map.currentValue;
        engine_map.currentValue = ((buf[0] * 256) + buf[1]) / 10.0;

        engine_rpm.previousValue = engine_rpm.currentValue;
        engine_rpm.currentValue = buf[2] * 256 + buf[3];

        engine_clt.previousValue = engine_clt.currentValue;
        engine_clt.currentValue = (buf[4] * 256 + buf[5]) / 10.0;

        engine_tps.previousValue = engine_tps.currentValue;
        engine_tps.currentValue = (buf[6] * 256 + buf[7]) / 10.0;
        
        break;
      case 1513:
        engine_pw1.previousValue = engine_pw1.currentValue;
        engine_pw1.currentValue = (buf[0] * 256 + buf[1]) / 1000.0;

        engine_pw2.previousValue = engine_pw2.currentValue;
        engine_pw2.currentValue = (buf[2] * 256 + buf[3]) / 1000.0;

        engine_iat.previousValue = engine_iat.currentValue;
        engine_iat.currentValue = (buf[4] * 256 + buf[5]) / 10.0;

        engine_adv.previousValue = engine_adv.currentValue;
        engine_adv.currentValue = (buf[6] * 256 + buf[7]) / 10.0;
        
        break;
      case 1514:
        engine_tgt.previousValue = engine_tgt.currentValue;
        engine_tgt.currentValue = (double)buf[0] / 10.0;

        engine_afr.previousValue = engine_afr.currentValue;
        engine_afr.currentValue = (double)buf[1] / 10.0;

        engine_ego.previousValue = engine_ego.currentValue;
        engine_ego.currentValue = (buf[2] * 256 + buf[3]) / 10.0;

        engine_egt.previousValue = engine_egt.currentValue;
        engine_egt.currentValue = (buf[4] * 256 + buf[5]) / 10.0;

        engine_pws.previousValue = engine_pws.currentValue;
        engine_pws.currentValue = (buf[6] * 256 + buf[7]) / 1000.0;
        
        break;
      case 1515:
        engine_bat.previousValue = engine_bat.currentValue;
        engine_bat.currentValue = (buf[0] * 256 + buf[1]) / 10.0;

        //not tested
        engine_sr1.previousValue = engine_sr1.currentValue;
        engine_sr1.currentValue = (buf[2] * 256 + buf[3]) / 10.0;

        //not tested
        engine_sr2.previousValue = engine_sr2.currentValue;
        engine_sr2.currentValue = (buf[4] * 256 + buf[5]) / 10.0;

        //not tested
        engine_knk.previousValue = engine_knk.currentValue;
        engine_knk.currentValue = (buf[6] * 256) / 10.0;

        break;
      case 1516:
        //not tested
        engine_vss.previousValue = engine_vss.currentValue;
        engine_vss.currentValue = (buf[0] * 256 + buf[1]) / 10.0;

        //not tested
        engine_tcr.previousValue = engine_tcr.currentValue;
        engine_tcr.currentValue = (buf[2] * 256 + buf[3]) / 10.0;

        engine_lct.previousValue = engine_lct.currentValue;
        engine_lct.previousValue = (buf[4] * 256 + buf[5]) / 10.0;
        
        break;
      default:
        //do nothing
        break;
    }
  }
}

bool is_current_value_shorter(EngineVariable engine) {
  int currentLength;
  if(engine.currentValue >= 10000) {
    currentLength = 5;
  }
  else if(engine.currentValue >= 1000) {
    currentLength = 4;
  }
  else if(engine.currentValue >= 100) {
    currentLength = 3;
  }
  else if(engine.currentValue >= 10) {
    currentLength = 2;
  }
  else {
    currentLength = 1;
  }
  if(engine.decimalPlaces > 0) {
    currentLength++;
    currentLength+= engine.decimalPlaces;
  }

  int previousLength;
  if(engine.previousValue >= 10000) {
    previousLength = 5;
  }
  else if(engine.previousValue >= 1000) {
    previousLength = 4;
  }
  else if(engine.previousValue >= 100) {
    previousLength = 3;
  }
  else if(engine.previousValue >= 10) {
    previousLength = 2;
  }
  else {
    previousLength = 1;
  }
  if(engine.decimalPlaces > 0) {
    previousLength++;
    previousLength += engine.decimalPlaces;
  }

  return currentLength < previousLength;
}

void draw_bar(EngineVariable engineVar, int row) {
  if(engineVar.currentValue == engineVar.previousValue) {
    //relax homeboy
  }
