내 친구와 나는 Arduino 보드에 디지털 온도계를 배선/코딩하고 있으며 코드를 작성하고 있습니다. 온도계가 훌륭하게 작동하고 기본 온도 데이터가 출력용으로 사용되는 4 자리 7 세그먼트 LED 스크린으로 이동합니다. 내가 부정 (이하 제로) 온도를 표시하는 코드를 작성하려고 노력하고, 적절한 출력을 얻는 데 문제가 있습니다. 대신 음의 부호를 출력하는, 그것은 제Arduino에 음수 표시
여기서 루프() 메소드의 출력 :
void loop(void) {
int temp = getTemp();
boolean neg = false;
if (temp < 0) {
// Since the temperature is negative, multiplying it by -2 and adding it
// to itself gives us the absolute value of the number
temp += (temp * (-2));
// We set the neg boolean to true, indicating that we're dealing with a negative number
neg = true;
}
displayNumber(temp, neg);
}
여기 (절단) DisplayNumber로서() 메소드의 :
void displayNumber(int toDisplay, boolean negative) {
int num = toDisplay;
// The digits are 1-4, left to right
for(int digit = 4; digit > 0 ; digit--) {
//Turn on a digit for a short amount of time
switch(digit) {
case 1:
// The leftmost digit only needs to be on for temps 100.0 or above,
// or to display the negative sign for temps -10.0 or below
if (num >= 1000 || (num >= 100 && negative == true)) {
digitalWrite(digit1, HIGH);
}
if (num >= 100 && negative == true) {
lightNumber(11);
}
break;
case 2:
// Only needs to be on for temps 10.0 degrees or above, or
// for single-digit subzero temps.
if (num >= 100 || negative == true) {
digitalWrite(digit2, HIGH);
}
if (num < 100 && negative == true) {
lightNumber(11);
}
break;
case 3:
digitalWrite(digit3, HIGH);
break;
case 4:
digitalWrite(digit4, HIGH);
break;
}
//Turn on the right segments for this digit
lightNumber(toDisplay % 10);
toDisplay /= 10;
//Turn off all segments
lightNumber(10);
//Turn off all digits
digitalWrite(digit1, LOW);
digitalWrite(digit2, LOW);
digitalWrite(digit3, LOW);
digitalWrite(digit4, LOW);
}
}
을 ... 그리고 lightNumber() 메서드의 코드는 숫자 0-9에 대해 세그먼트를 올바르게 설정하거나 해제합니다. 10은 모든 세그먼트가 꺼져 있고 11은 가운데 세그먼트 만 켜져 있고 음수 기호입니다. 정수 매개 변수가있는 switch 문을 스위치로 사용합니다. 문제는 displayNumber()에 음수 값을 보내면 숫자 앞에 음수 기호가 표시되지 않고 음수 기호가 있어야하는 위치에 8이 표시된다는 것입니다. 이유가 무엇입니까?
'lightNumber','digitalWrite'는 어디에 있나요? 나는 아마 더 많은 코드를 볼 필요가 있다고 생각한다. – Tim
이렇게하지 마십시오 :'temp + = (temp * (-2));'! 그것은 혼란스럽고 비효율적입니다 .2. 어때?'temp = temp * (-1);'? – JimmyB