Displacement variable not updating during test in Arduino project

I’m working on a project using Arduino to control a test machine for compression/tension. The displacement is calculated based on pulses from a sensor connected to a digital input. The logic for calculating the displacement works perfectly when the test is not active, but as soon as the test starts, the displacement value freezes at 0 and does not update.

Here is the simplified loop code:

cpp
Copy code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>void loop() {
// Verifica comandos recebidos via Serial
if (Serial.available() > 0) {
String command = Serial.readStringUntil('n');
command.trim();
if (command == "C") {
calibrateScale();
} else if (command == "N") {
resetCalibration();
} else if (command == "IC") {
contagemRegressiva();
x = 0; // Reseta o contador
displacement = 0; // Reseta o deslocamento
ensaioAtivo = true; // Inicia o ensaio
TEnsaio = "COMPRESSAO...";
} else if (command == "IT") {
contagemRegressiva();
x = 0; // Reseta o contador
displacement = 0; // Reseta o deslocamento
ensaioAtivo = true; // Inicia o ensaio
TEnsaio = "TRACAO...";
} else if (command == "P") {
pararMotor();
ensaioAtivo = false;
lcd.setCursor(0, 0);
lcd.print("STOP S.O.S");
}
}
// Atualiza deslocamento independentemente do ensaio
if (digitalRead(input) == HIGH && state == 0) {
state = 1; // Define o estado para 1
x = x + 1; // Incrementa o contador de pulsos
displacement = x * 2; // Calcula o deslocamento
}
if (digitalRead(input) == LOW) {
state = 0; // Redefine o estado
}
// Lógica do ensaio ativo
if (ensaioAtivo) {
float cargaAtual = abs(scale.get_units());
if (cargaAtual >= MAX_LOAD) {
ensaioAtivo = false;
pararMotor();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CARGA MAX");
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(cargaAtual, 2);
lcd.print("N");
} else if (ultimaCarga > 2 && (ultimaCarga - cargaAtual) / ultimaCarga >= LOAD_DROP_THRESHOLD) {
ensaioAtivo = false;
pararMotor();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("RUPTURA OK!");
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print(ultimaCarga, 2);
lcd.print("N");
} else {
// Exibe informações do ensaio durante a execução
lcd.setCursor(0, 0);
lcd.print(TEnsaio);
lcd.setCursor(0, 1);
lcd.print("d:");
lcd.print(displacement, 0); // Exibe deslocamento
lcd.print("mm ");
lcd.print("P:");
lcd.print(cargaAtual, 2);
lcd.print("N");
}
ultimaCarga = cargaAtual;
delay(200);
}
// Exibe deslocamento no monitor Serial
Serial.print("Deslocamento: ");
Serial.print(displacement);
Serial.println(" mm");
}
</code>
<code>void loop() { // Verifica comandos recebidos via Serial if (Serial.available() > 0) { String command = Serial.readStringUntil('n'); command.trim(); if (command == "C") { calibrateScale(); } else if (command == "N") { resetCalibration(); } else if (command == "IC") { contagemRegressiva(); x = 0; // Reseta o contador displacement = 0; // Reseta o deslocamento ensaioAtivo = true; // Inicia o ensaio TEnsaio = "COMPRESSAO..."; } else if (command == "IT") { contagemRegressiva(); x = 0; // Reseta o contador displacement = 0; // Reseta o deslocamento ensaioAtivo = true; // Inicia o ensaio TEnsaio = "TRACAO..."; } else if (command == "P") { pararMotor(); ensaioAtivo = false; lcd.setCursor(0, 0); lcd.print("STOP S.O.S"); } } // Atualiza deslocamento independentemente do ensaio if (digitalRead(input) == HIGH && state == 0) { state = 1; // Define o estado para 1 x = x + 1; // Incrementa o contador de pulsos displacement = x * 2; // Calcula o deslocamento } if (digitalRead(input) == LOW) { state = 0; // Redefine o estado } // Lógica do ensaio ativo if (ensaioAtivo) { float cargaAtual = abs(scale.get_units()); if (cargaAtual >= MAX_LOAD) { ensaioAtivo = false; pararMotor(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("CARGA MAX"); lcd.setCursor(0, 1); lcd.print("P:"); lcd.print(cargaAtual, 2); lcd.print("N"); } else if (ultimaCarga > 2 && (ultimaCarga - cargaAtual) / ultimaCarga >= LOAD_DROP_THRESHOLD) { ensaioAtivo = false; pararMotor(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("RUPTURA OK!"); lcd.setCursor(0, 1); lcd.print("P:"); lcd.print(ultimaCarga, 2); lcd.print("N"); } else { // Exibe informações do ensaio durante a execução lcd.setCursor(0, 0); lcd.print(TEnsaio); lcd.setCursor(0, 1); lcd.print("d:"); lcd.print(displacement, 0); // Exibe deslocamento lcd.print("mm "); lcd.print("P:"); lcd.print(cargaAtual, 2); lcd.print("N"); } ultimaCarga = cargaAtual; delay(200); } // Exibe deslocamento no monitor Serial Serial.print("Deslocamento: "); Serial.print(displacement); Serial.println(" mm"); } </code>
void loop() {
  // Verifica comandos recebidos via Serial
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('n');
    command.trim();

    if (command == "C") {
      calibrateScale();
    } else if (command == "N") {
      resetCalibration();
    } else if (command == "IC") {
      contagemRegressiva();
      x = 0;               // Reseta o contador
      displacement = 0;    // Reseta o deslocamento
      ensaioAtivo = true;  // Inicia o ensaio
      TEnsaio = "COMPRESSAO...";
    } else if (command == "IT") {
      contagemRegressiva();
      x = 0;               // Reseta o contador
      displacement = 0;    // Reseta o deslocamento
      ensaioAtivo = true;  // Inicia o ensaio
      TEnsaio = "TRACAO...";
    } else if (command == "P") {
      pararMotor();
      ensaioAtivo = false;
      lcd.setCursor(0, 0);
      lcd.print("STOP S.O.S");
    }
  }

  // Atualiza deslocamento independentemente do ensaio
  if (digitalRead(input) == HIGH && state == 0) {
    state = 1;              // Define o estado para 1
    x = x + 1;              // Incrementa o contador de pulsos
    displacement = x * 2;   // Calcula o deslocamento
  }

  if (digitalRead(input) == LOW) {
    state = 0; // Redefine o estado
  }

  // Lógica do ensaio ativo
  if (ensaioAtivo) {
    float cargaAtual = abs(scale.get_units());

    if (cargaAtual >= MAX_LOAD) {
      ensaioAtivo = false;
      pararMotor();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("CARGA MAX");
      lcd.setCursor(0, 1);
      lcd.print("P:");
      lcd.print(cargaAtual, 2);
      lcd.print("N");
    } else if (ultimaCarga > 2 && (ultimaCarga - cargaAtual) / ultimaCarga >= LOAD_DROP_THRESHOLD) {
      ensaioAtivo = false;
      pararMotor();
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("RUPTURA OK!");
      lcd.setCursor(0, 1);
      lcd.print("P:");
      lcd.print(ultimaCarga, 2);
      lcd.print("N");
    } else {
      // Exibe informações do ensaio durante a execução
      lcd.setCursor(0, 0);
      lcd.print(TEnsaio);
      lcd.setCursor(0, 1);
      lcd.print("d:");
      lcd.print(displacement, 0);  // Exibe deslocamento
      lcd.print("mm  ");
      lcd.print("P:");
      lcd.print(cargaAtual, 2);
      lcd.print("N");
    }

    ultimaCarga = cargaAtual;
    delay(200);
  }

  // Exibe deslocamento no monitor Serial
  Serial.print("Deslocamento: ");
  Serial.print(displacement);
  Serial.println(" mm");
}

New contributor

Lewys Len is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật