Implementing Sequence

Hello,


I am working on a personal project and have gotten the following (pertinent) variables from my Apple watch:


totalAcceleration

Xrotation


What I want to implement is the following:


If totalAcceleration > acceleration_THRESHOLD:

Wait 3 seconds


total_Xrotation = 0


For the next 5 seconds:

total_Xrotation += Xrotation


If total_Xrotation > rotation_THRESHOLD:

Activate watch vibration



I am mainly unsure how to implement the timing rules.


I come from a mainly non OOP background (Python mainly), so any assistance is much appreciated!

Replies

Regarding timing, in objective C:

theManager = [[CMMotionManager alloc] init];
[theManager startAccelerometerUpdates];

theManager.deviceMotionUpdateInterval=0.1;
[theManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical toQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion,NSError*error){
            
//   code here that records motions 10 tiems per second and does something after recording 30 or 50 values 
                
            }];

Take care not to block the main thread for 3 + 5 seconds.

Dispatch to new threads for collecting data after a delay

Such as:


            DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: .now() + 3.0, execute: {
                // Compute new value for rotation
            })

And of course do all UI in main thread.