无法使用BLE Shield 2.1从我的自定义IOS应用发送数据到Arduino Uno

时间:2018-07-09 18:18:47

标签: ios arduino bluetooth-lowenergy arduino-uno

我目前正在设计类似蛇的机器人,该机器人将通过自定义Iphone iOS应用程序进行控制。 我正在使用以下技术: -Arduino的Uno -Adafruit马达护罩 -Nema 17双极步进电机 -RedBearLab的BLE Shield 2.1

我的iOS应用程序包含一个滑块,这样当滑块移动时,将控制步进电机的速度。我在xcode中使用Write函数将速度数据发送到arduino,但是当我尝试在Serial Monitor中打印读取结果时,数据仅为0。

func writeSpeed(_ speed: UInt8)
{
    // See if characteristic has been discovered before writing to it
    if let positionCharacteristic = self.positionCharacteristic
    { 
        let data = Data(bytes: [speed])
        self.peripheral?.writeValue(data, for: positionCharacteristic, type: CBCharacteristicWriteType.withResponse) //with originally
        print(data)
        print(speed)
    }
}

当我打印数据并加快速度时,结果如下...

1 bytes
3
1 bytes
4
1 bytes
3
1 bytes
2
end sliding

我的Arduino代码如下...

// Add in necessary libraries
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <boards.h>
#include <RBL_nRF8001.h>
#include <RBL_services.h>

SoftwareSerial BLE_Shield(9,8);  // Configure the Serial port to be on pins D8 and D9. This
                               // will match the jumpers on the BLE Shield (RX -> D8 & TX /> D9)

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// Define the stepper motor attached to the motor shield.
// i.e. 200 steps per revolution (1.8 degree) in motor port #2 (M3 and M4)
  const int stepsPerRevolution = 200;
  const int motorPort = 2;
  Adafruit_StepperMotor *myMotor = AFMS.getStepper(stepsPerRevolution, 
motorPort);

void setup() 
{ 
  // put your setup code here, to run once:
  BLE_Shield.begin(9600);   // Setup the serial port at 9600 bps. This is the BLE Shield default baud rate.
  ble_begin();

  // Initialize serial port at 9600 bps
  Serial.begin(9600);

  // Print a ready statement to serial monitor          
  Serial.println("Stepper Motor is Ready!");

  // Stepper motor
  AFMS.begin(1600);  // create with the default frequency 1.6KHz
}

void loop() 
{ 
  // put your main code here, to run repeatedly:

  ble_do_events();
  if(BLE_Shield.available()) 
  {
    Serial.println("Available");

    // Read the value of the slider and store it in the variable data
    int data = BLE_Shield.read();        //Read the incoming data & store into data
    Serial.println("The data from iphone is...");
    Serial.print(data);

    // Eventually sliderData will be a variable, but for now, a constant
    const int sliderData = 30; 

    // Specify the speed of my stepper motor
    myMotor->setSpeed(sliderData);  // 30 rpm
    Serial.println("Speed is 30 rpm");
  }

  else
  {
    Serial.println("Stopped working");
  }

  //ble_do_events();
  delay(500);
}

我真的不知道为什么这不起作用。

0 个答案:

没有答案