Question

In: Computer Science

hot use blutooth on your app stimulation to connect to a device. xcode swift. please inclide...

hot use blutooth on your app stimulation to connect to a device. xcode swift. please inclide a code if possible.

Solutions

Expert Solution

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:

  1. The user of the iOS app moves the slider to the middle position.
  2. The app sends the number 90, which represents 90 degrees in this case, to the BLE Shield module using the active Bluetooth connection.
  3. The BLE Shield transfers the number 90 to the Arduino board.
  4. The Arduino board rotates the servo to the 90 degree position.

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.


Related Solutions

"swift Create a SwiftUI project named DogBreeds based on the Contacts app. The app should be...
"swift Create a SwiftUI project named DogBreeds based on the Contacts app. The app should be named DogBreeds. The app will display 10 dog images and names in a lis"
I need swift programming code and instruction of app Named Shortest path. Please explain at the...
I need swift programming code and instruction of app Named Shortest path. Please explain at the end of the code how to run the app on Xcode. Thanks
Upload the 3D brain app onto your mobile device or visit the 3D brain app website...
Upload the 3D brain app onto your mobile device or visit the 3D brain app website below. Using your textbook and the app, please identify two structures in the brain. Name the structures, describe where they are located, and describe their functions. Based on case studies described in the app, discuss what is known about damage to these areas. In addition, discuss any new potential treatments for disorders associated with damage to these structures. You may also use outside links...
Intro to App Development with Swift by Apple education, Apple Education ITEC245, iOS App Development Description:...
Intro to App Development with Swift by Apple education, Apple Education ITEC245, iOS App Development Description: Write a report with your self reflection and learning outcome on below topics: 1. Playground Basics with sample program , Naming and Identifiers with sample program , Strings with sample program , First App with sample program , Functions with sample program
Stimulation (Please read the simulation before answering the questions . Please do not scan your answer,...
Stimulation (Please read the simulation before answering the questions . Please do not scan your answer, type please) Hey Ben. You’re getting an admission in the next 15 minutes coming from the outpatient clinic. BEN: Okay, what can you tell me about this client? JAN: Well, Susan Choi is 33 years old. (PATIENT) She’s been followed for depression over the past six months and is being admitted for acute mania. She is on administrative leave from her employment as a...
name a device that has one hot encoded input
name a device that has one hot encoded input
Please describe your favorite food in terms of the sense receptors which are stimulated, stimulation of...
Please describe your favorite food in terms of the sense receptors which are stimulated, stimulation of nerve fibers, impulse transmission to the CNS, sensations (memories and experiences), and perception for the sense of smell, taste, sight, and hearing.
1. Types of groups and teams (Connect, Perform) Use your knowledge of groups and teams to...
1. Types of groups and teams (Connect, Perform) Use your knowledge of groups and teams to answer the following questions. Please select the answer that best completes the sentence. In business organizations, most employees work in --------------- . For each example presented in the following table, identify the concept being illustrated. Example Problem-solving Team Virtual Team Cross-functional Team Self-directed Team Once the housekeeping unit of a small hotel was organized into this type of team, the employees took responsibility for...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the...
use android studio or MIT inventor Create a Presidents Quiz App for your project with the following requirements: You must have images Buttons Label Conditional Statement Homepage with instructions Sound (music, buzzer, etc.) - Ensure you are using the correct layout: Gravity(Android Studio), Horizontal or Vertical Arrangement (MIT). - 40POINTS Points System (indicating how many answers they got wrong/correct). 20POINT 1-2 questions per President (45+ questions)
Connect each classification of cement to its description. Then connect each description to the correct use...
Connect each classification of cement to its description. Then connect each description to the correct use of cement. (15 Points) Classification Description Uses Type I High Early Strength Dams and mass concrete Type II High Sulfate Resistance If conditions require or requested Type III General Purpose Grouting Type IV Moderate Sulfate Resistance Special properties not required Type V Low Heat of Hydration Usually adequate for concrete exposed to ground
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT