In: Computer Science
Download the Xcode starter project. The starter project includes the view controller and base Core Bluetooth implementation to save you time. You’ll add more code to interface with the BLE Shield later on in the tutorial.
Next, download and install the Arduino IDE from the Arduino Download page; it’s available for Windows, Mac OS X and Linux platforms. You’ll use the Arduino IDE to write and compile the code for the Arduino. Ensure you grab the latest release version, not the beta or nightly builds.
The Basic Design of your App
Your finished project will consist of an iOS app that will send messages via Bluetooth to the BLE Shield module. The module will then send the message to the Arduino board to tell the servo which position it should rotate to.
Here’s an example use-case of your project:
To begin, you’ll work with the Arduino IDE and program the board logic.
Programming the Arduino
Start the Arduino IDE; you’ll see the editor appear with a blank document, or “sketch”, as shown below:
Before doing anything else, click File\Save in the top
menu and save the current file to a convenient location as
Arduino_Servo_Controller. This creates a folder in your save
location that contains a file with a .ino
file
extension.
Before you start writing code, you’ll need to set up the IDE to communicate with the Arduino Uno board.
Select Tools\Board\Arduino Uno to let the IDE know what kind of board you’ll be dealing with. Next, connect the Uno to your computer with the USB cable as shown below:
Once that’s done, select Tools\Serial Port\… and select the USB port the Arduino is connected to. Generally it’s similar to /dev/tty.usbserial… or /dev/tty.usbmodem….
At this point the Arduino IDE is ready for programming.
Add the code below to your project using the Arduino IDE:
// Arduino Bluetooth LE Servo Controlled by iOS void setup() // Called only once per startup { } void loop() // Continuous loop { }
Illuminating an LED
The main objective of your Arduino program in this project is to receive the one-byte messages coming from the BLE Shield and use the contents of the message to set the servo’s position. But before you get too involved into the servo code, it would be nice to test your current connections.
Replace the code in your project with the following:
// Arduino Bluetooth LE Servo Controlled by iOS int LED = 13; // Most Arduino boards have an onboard LED on pin 13 void setup() // Called only once per startup { pinMode(LED, OUTPUT); // Set pin as an output digitalWrite(LED, HIGH); // Turn on LED (ie set to HIGH voltage) } void loop() // Continuous loop { delay(500); digitalWrite(LED, LOW); delay(500); digitalWrite(LED, HIGH); }
Since most Arduino boards have an onboard LED on pin 13, you can
use the illumination of the LED as confirmation that your Arduino
program is executing properly. You set the LED
variable to 13 to refer to that pin.
In setup()
, you set the initial state of the board.
pinMode(LED, OUTPUT)
sets pin 13 as a digital output,
which means that the voltage on this pin can be set to HIGH (+5v)
or LOW (0v). digitalWrite()
sets the pin’s voltage; in
the code above you’ve set it to HIGH to illuminate the LED.
After setup()
has finished, loop()
will run continuously. delay()
will wait a certain
number of milliseconds which you’ll use to get the LED to blink.
After a 500ms delay, you turn the LED off by writing the value LOW;
after another delay, you turn the LED back on.
Click the Verify button again to compile the program and check for any syntax errors. If your program compiles without issue, you’ll see Done Compiling in the status bar of the Arduino IDE. If not, read the status bar message for an indication of where you’ve messed up, fix the error and click the Verify button to make sure you’ve squashed the bug for good.
Now it’s time to program the code to the Arduino board and test the LED.
Make sure your Arduino is connected to your computer with the USB cable then click the Upload button (the one with the right arrow icon) to program the Arduino board. Once the upload process has finished, the Done uploading message should appear in the status bar .
Check your Arduino board, and you should see the LED start to blink.
Hope this helps you. Please give an upvote. Thank you.