androidとgenuino101でBLE通信をしてサーボモータを回したいです。
理想の形が、初期位置0度ではなく、90度から動くようにしたいです。90度基準で0度と90度を回したいと考えています。BLE通信とandroid側のプログラムはたぶん大丈夫だと思います。なので、サーボモータの基準位置を90度にする方法が知りたいです。サーボモータはSG90を使っています。
#include <CurieBLE.h> #include <Servo.h> //サーボモーター用ライブラリを読み込み #define SW1 12 #define SW2 11 Servo myservo; //サーボ用のオブジェクトを作成 BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); void setup() { Serial.begin(9600); myservo.attach(9); //デジタル9番ピンをサーボの角度命令出力ピンとして設定 pinMode(SW1,INPUT); //D12をスイッチとして使用 digitalWrite(SW1,HIGH); //pull-up ON pinMode(SW2,INPUT); //D11をスイッチとして使用 digitalWrite(SW2,HIGH); //pull-up ON BLE.begin(); // begin initialization BLE.setLocalName("KEY"); // set advertised local name and service UUID: BLE.setAdvertisedService(ledService); ledService.addCharacteristic(switchCharacteristic);// add the characteristic to the service BLE.addService(ledService); // add service switchCharacteristic.setValue(0); // set the initial value for the characeristic: BLE.advertise(); // start advertising Serial.println("BLE LED Peripheral"); } void loop() { BLEDevice central = BLE.central(); // listen for BLE peripherals to connect: if (central) { // if a central is connected to peripheral: Serial.print("Connected to central: "); Serial.println(central.address());// print the central's MAC address: // while the central is still connected to peripheral: while (central.connected()) { if(digitalRead(SW1)==LOW){ myservo.write(0); //サーボを動かす(0度) } if(digitalRead(SW2)==LOW){ myservo.write(90); //サーボを動かす(90度) } // if the remote device wrote to the characteristic, // use the value to control the LED: if (switchCharacteristic.written()) { if (switchCharacteristic.value()==1) { // any value other than 0 myservo.write(0); //サーボを動かす(0度) } else if(switchCharacteristic.value()==0) { // a 0 value myservo.write(90); //サーボを動かす(90度) } } } // when the central disconnects, print it out: Serial.print(F("Disconnected from central: ")); Serial.println(central.address()); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。