我正在製作一個小型的arduino二進制計算器。爲什麼這兩個值在Arduino上不相等?
我有代碼運行一些小數學問題:✓
我將答案從十進制轉換爲二進制:✓
我通過for循環循環二進制答案,並打開面包板上的LED指示燈以顯示答案:✗
//First led in pin 2
void setup()
{
Serial.begin(9600);
}
//I have the code run some little math problem:Check
int a=2;
int b=5;
int answer=b-a;
int myNum = answer;
void loop(){
//I convert the answer from decimal to binary:Check
int zeros = 8 - String(myNum,BIN).length();
String myStr;
for (int i=0; i<zeros; i++) {
myStr = myStr + "0";
}
myStr = myStr + String(myNum,BIN);
Serial.println(myStr);
//I loop through the binary answer with a for loop
//and power on LEDs on a bread board to display the answer:Not check
for(int i=2;i<=9;i=i+1){
//This part doesn't work
if(int(myStr[i-2])==1){
digitalWrite(int(i), HIGH);
}else{Serial.println(myStr[i-2]);}
}
while(true){}
}
由於某種原因,它說INT(myStr的[I-2)是在您的幫助絕不等於1
感謝。