後我有藍牙模塊和振動電機安裝Arduino的UNO。我可以打開振動器,但似乎無法關閉它。下面的代碼Arduino的不關閉振動輸入0
#include<SoftwareSerial.h>//import the serial library
int vib=8;
SoftwareSerial Genotronex(10,11);//RX,TX
int BluetoothData;//the data given
void setup() {
Genotronex.begin(9600);
Genotronex.println("Bluetooth on please press 1 to vibrate");
pinMode(vib,OUTPUT);
// put your setup code here, to run once:
}
void loop() {
if (Genotronex.available()){
BluetoothData=Genotronex.read();{
if(BluetoothData='1'){
digitalWrite(vib,'1');
Genotronex.println("Vibrator on");
delay(500);
}
else if (BluetoothData='0'){
digitalWrite(vib,'0');
Genotronex.println("Vibrator off");
delay(500);
}
}
}
delay(100);
// put your main code here, to run repeatedly:
}
在藍牙終端,它說 < 1>振動器上 當我輸入「1」 但也表示在 < 0)振動器,當我輸入「0」時,它應我一直在振動器關閉。
感謝所有幫助
' '0''和'' 1''是不一樣的'0'和'1'。第一組是一個字符串(或字符),第二組是一個整數。您的'BluetoothData'變量是一個整數,因此您需要將其與整數值進行比較。這同樣適用於你的'digitalWrite'功能,你要改變那些整數或使用'HIGH'和'LOW' – TyloBedo
@TyloBedo:''1''不是字符串。它是一個字符('char'類型)。因爲這是'C++',所以這個字符是數字數據類型。所以將char和int進行比較並不是問題(除非沒有數據可讀)。然而,關於'digitalWrite(vib,'0')'的好處。這將是另一個問題。不只是使用賦值運算符,而不是比較一個 – KIIV
解決了!非常感謝你:) – AFME