Post

Replies

Boosts

Views

Activity

Reply to How di
Thank you SO MUCH for your help! I made the modifications below. I am still getting an error message. I also want the while loop to repeat infinitely every 1 second. Error message: Value of optional type 'String?' must be unwrapped to a value of type 'String' Coalesce using '??' to provide a default when the optional value contains 'nil' Force-unwrap using '!' to abort execution if the optional value contains 'nil' This is in regards to the line: var xoutput = Double(self.xaxis.text) ?? 0.0 // // ViewController.swift // Accelerometer // // Created by Brian Cleaver on 8/22/25. // import UIKit import CoreMotion let motion = CMMotionManager() class ViewController: UIViewController { @IBOutlet var xaxis: UILabel! @IBOutlet var yaxis: UILabel! @IBOutlet var zaxis: UILabel! let movementManager = CMMotionManager() override func viewDidLoad() { super.viewDidLoad() movementManager.startAccelerometerUpdates() movementManager.accelerometerUpdateInterval = 0.1 if let data = self.movementManager.accelerometerData { self.xaxis.text = String(data.acceleration.x) //OLD - self.yaxis.text = String(data.acceleration.y) //OLD - self.zaxis.text = String(data.acceleration.z) var xoutput = Double(self.xaxis.text) ?? 0.0 var distance: Double = 0.0 var velocityi: Double = 0.0 var velocityf: Double = 0.0 var x: Int = 1 while x == 1 { distance = velocityi * 0.1 + (1/2) * xoutput * pow(0.1, 2) velocityf = velocityi + xoutput * 0.1 velocityi = velocityf } } } }
Sep ’25
Reply to Accelerometer data not printing correctly.
Hi everyone. I spent all day an all night messing with the settings, and I finally figured it out. For anyone getting the same problem, I did the following: Click the label. Go to the right triangle that says "Show the size inspector." Under layout, click "auto resizing mask." Then, select horizontal arrow and vertical arrow. Thanks everyone who took the time to read this!!!
Aug ’25
Reply to Xcode won't execute code?
I rewrote the code without errors but the screen is just blank and white. struct ContentView: View { var body: some View { } } import CoreMotion class MyViewController: UIViewController { let motionManager = CMMotionManager() func startAccelerometer() { if motionManager.isAccelerometerAvailable { motionManager.accelerometerUpdateInterval = 0.1 // 10 updates per second motionManager.startAccelerometerUpdates(to: .main) { (data, error) in guard let accelerometerData = data else { return } let x = accelerometerData.acceleration.x let y = accelerometerData.acceleration.y let z = accelerometerData.acceleration.z // Process the x, y, and z acceleration values here print("X: \(x), Y: \(y), Z: \(z)") } startAccelerometer() } } } #Preview { ContentView() }
Aug ’25
Reply to Xcode won't execute code?
I wrote the following code above the code in my first post.. struct ContentView: View { var body: some View { Button("Tap Me") { // Action to perform when the button is tapped print("Button was tapped!") startAccelerometer() } } } The error message says "Cannot find 'startAccelerometer' in scope" Thanks again for the help!
Aug ’25
Reply to Xcode won't execute code?
Thank you for your help! I put this code above the code already mentioned. inline-code struct ContentView: View { var body: some View { Button("Tap Me") { // Action to perform when the button is tapped print("Button was tapped!") startAccelerometer() } } func myFunction() { // Your Swift code to be executed print("myFunction was called!") } } `inline-code` I get an error message saying: " Cannot find 'startAccelerometer' in scope"
Aug ’25