Математические расчеты в Arduino - PullRequest
0 голосов
/ 06 января 2019

Я написал код для базовых вычислений с Arduino Uno. но он возвращает мне неправильный ответ за заполненный процент. Это всегда -1. в чем проблема с этим?

//include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 13, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int lowLevelDistance =112;
int highLevelDistance = 47;

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

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  int currentLevel = 101;

  int filledLevel = (lowLevelDistance - currentLevel);
  int fullLevel = (lowLevelDistance - highLevelDistance);
  int filledPercentage = filledLevel / (fullLevel / 100);

  Serial.println(lowLevelDistance);
  Serial.println(highLevelDistance);
  Serial.println(currentLevel);
  Serial.println(filledLevel);
  Serial.println(fullLevel);
  Serial.println(filledPercentage);
  delay(1000);
}

1 Ответ

0 голосов
/ 06 января 2019

Вы не должны использовать целое число для деления, ваш полный уровень / 100 возвращает 0,65, но затем преобразуется в целое число (усекает его до 0).

При делении на 0 выдается ошибка (-1).

...