Bluetoothモジュールと振動モーターが付いている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」と同じではありません。最初のセットは文字列(またはchar)で、2番目のセットは整数です。あなたの 'BluetoothData'変数は整数なので、整数値と比較する必要があります。あなたの 'digitalWrite'関数も同様です。これらを整数に変更するか、' HIGH'と 'LOW'を使いたいでしょう – TyloBedo
@TyloBedo:' '1''は文字列ではありません。これは文字( 'char'型)です。これは 'C++ 'なので、文字は数値データ型です。したがって、charとintを比較するのは問題ありません(読み込むデータがない限り)。しかし、 'digitalWrite(vib、 '0')'についての良い点です。これは別の問題です。比較演算子の代わりに代入演算子を使用するだけではありません – KIIV
解決済み!ありがとうございます:) – AFME