0

New to the site and Obj C. Attempting to get a pitch value from Device Motion (working), put into an array with the most recent 60 values (not working) and select the maximum value within the array. With each new pitch value from the device, new pitch value is added to the array and the 61st value is dropped. When I hook up my phone and run, I get the log values for the pitch and maxPitch; however, I am not getting the array of 60 values so I don't believe it is working properly. Any help is greatly appreciated.

I believe the problem may be in the line : if (pitchArray.count <= 60) {

[pitchArray addObject:[NSString stringWithFormat:@"%.2gº", motion.attitude.pitch * kRadToDeg]];

Here is the full code:

#import "ViewController.h"
#import <CoreMotion/CoreMotion.h>

#define kRadToDeg   57.2957795

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;
@property (nonatomic, strong) CMMotionManager *motionManager;

@end

@implementation ViewController

- (CMMotionManager *)motionManager
{
    if (!_motionManager) {
        _motionManager = [CMMotionManager new];
        [_motionManager setDeviceMotionUpdateInterval:1/60];
    }
    return _motionManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {

        self.pitchLabel.text = [NSString stringWithFormat:@"%.2gº", motion.attitude.pitch * kRadToDeg];

     NSMutableArray *pitchArray = [NSMutableArray array];
     pitchArray = [[NSMutableArray  alloc] initWithCapacity:60];

        if (pitchArray.count <= 60) {

            [pitchArray addObject:[NSString stringWithFormat:@"%.2gº", motion.attitude.pitch * kRadToDeg]];
        }
        else {

        [pitchArray removeObjectAtIndex:0];

        }

        NSNumber *maxPitch = [pitchArray valueForKeyPath:@"@max.intValue"];

        NSLog(@"%@",pitchArray);

        NSLog(@"Max Pitch Value = %d",[maxPitch intValue]);


      }];
}

 @end
dkwill
  • 11
  • 2

2 Answers2

0

You keep allocating a new array every time you get a new pitch value. So you shall define the pitch array as a property and allocate it before your motion update handler. Your code would be:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *pitchLabel;
@property (nonatomic, strong) CMMotionManager *motionManager;
@property (nonatomic, strong) NSMutableArray *pitchArray;

@end

@implementation ViewController

- (CMMotionManager *)motionManager
{
    if (!_motionManager) {
        _motionManager = [CMMotionManager new];
        [_motionManager setDeviceMotionUpdateInterval:1/60];
    }
    return _motionManager;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.pitchArray = [[NSMutableArray  alloc] initWithCapacity:60];

    [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {

        self.pitchLabel.text = [NSString stringWithFormat:@"%.2gº", motion.attitude.pitch * kRadToDeg];

        if (self.pitchArray.count <= 60) {

            [self.pitchArray addObject:[NSString stringWithFormat:@"%.2gº", motion.attitude.pitch * kRadToDeg]];
        }
        else {

        [self.pitchArray removeObjectAtIndex:0];

        }

        NSNumber *maxPitch = [self.pitchArray valueForKeyPath:@"@max.intValue"];

        NSLog(@"%@",self.pitchArray);

        NSLog(@"Max Pitch Value = %d",[maxPitch intValue]);


      }];
}
spassas
  • 4,778
  • 2
  • 31
  • 39
0

Ah, simple error. It wasn't looping so I changed the if/else statement to while. The code works now and outputs the 60 item array and max value.

dkwill
  • 11
  • 2