Resolved Changing UIAccelerometer's updateInterval via a UISlider.

Discussion in 'iOS Programming' started by troop231, Jul 24, 2012.

  1. troop231, Jul 24, 2012
    Last edited: Jul 25, 2012

    troop231 macrumors 603

    Joined:
    Jan 20, 2010
    #1
    Hello, I'm trying to change my UIAcceleromter's updateInterval via a UISlider that I have in my .xib. My NSLogs shows me the values perfectly, but the acceleromter's interval does not increase in speed when I move the slider. I'm not sure what's wrong.

    I appreciate any help offered. Here is my code:

    ViewController.h:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIAccelerometerDelegate> {
    
    }
    
    - (IBAction)sliderValueChanged:(UISlider *)slider;
    
    @end
    ViewController.m:

    Code:
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    @synthesize XAxis, YAxis, delta;
    
    float value;
    
    - (void)viewWillAppear:(BOOL)animated
    {   
        UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
        accel.delegate = self;
        accel.updateInterval = 1.0f * value;
        
        NSLog(@"Value: %f",value);
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    
    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
    {
        NSLog(@"X: %g",acceleration.x);
        NSLog(@"Y: %g",acceleration.y);
        NSLog(@"Z: %g",acceleration.z);
    }
    
    - (IBAction) sliderValueChanged:(UISlider *)slider 
    {  
        value = slider.value;
        NSLog(@"Value: %f",slider.value);
    } 
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    @end
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Think like a computer. Manually walk through your code, from the point where the slider changes, and find the point where you actually execute the code that changes the update interval.

    Example:
    sliderValueChanged: is called when the slider changes. This method gets the slider value and stores it in the global variable called 'value'. It then shows that value. It then returns.

    After that method returns, what else happens? Nothing, until the slider changes. At that point, the same sequence happens, resulting in the global variable being changed, but nothing else.

    Change the code so something else happens.
     
  3. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #3
    How could I go about making the accelerometer as a property instead?
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Please explain what that would accomplish.

    You're proposing a solution (make the accelerometer a property), without explaining how or why that would solve the problem, or what the overall solution is.

    http://www.perlmonks.org/index.pl?node_id=542341
    .. You want to do X, and you think Y is the best way of doing it.
    .. Instead of asking about X, you ask about Y.
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5


    chown is asking you to solve the problem for yourself. The problem is glaringly obvious to most people who read your post.

    Do me a favor. Tell me what line of your code takes the slider value and uses to change the accelerometer's update interval? Show me that line in your code.
     
  6. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #6
    I guess it doesn't exist? I'm not sure what to do that's why I'm asking how I should go about it.
     
  7. chown33, Jul 24, 2012
    Last edited: Jul 24, 2012

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    What does this code from your first post do?
    Code:
    float value;
    
    - (void)viewWillAppear:(BOOL)animated
    {   
        UIAccelerometer *accel = [UIAccelerometer sharedAccelerometer];
        accel.delegate = self;
    [COLOR="Red"]    accel.updateInterval = 1.0f * value;
    [/COLOR]    
        NSLog(@"Value: %f",value);
    }
    
    Did you write it yourself? If so, explain what you want the code to do.

    Did you copy it from somewhere (where, exactly)?

    Did you copy it and change it (exactly what did you change)?


    What does this code do?
    Code:
    - (IBAction) sliderValueChanged:(UISlider *)slider 
    {  
        value = slider.value;
        NSLog(@"Value: %f",slider.value);
    } 
    
    It clearly gets the value of the slider, which is half of what you want to happen. It then stores that value in the global variable 'value', which is completely unrelated to "trying to change my UIAccelerometer's updateInterval".

    Since changing the UIAccelerometer updateInterval is the second half of what you want to happen, connect the dots and figure out what code is needed, and exactly where it should be placed in order to do the whole thing at the right time.

    You already have the description of what should happen: when the slider value changes, it should change the updateInterval of the UIAccelerometer.
     
  8. troop231, Jul 24, 2012
    Last edited: Jul 24, 2012

    troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #8
    I think you led me on the right path sir :)

    I believe this works correctly: If something looks odd, please let me know.

    My slider initial value is 5.0f which was made in the .xib, so when the view loads, I tell it to set my value at 5.0f.

    .h:

    Code:
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIAccelerometerDelegate> {
    
        UIAccelerometer *accelerometer;
    }
    
    - (IBAction)sliderValueChanged:(UISlider *)slider;
    
    @property (nonatomic) UIAccelerometer *accelerometer;
    
    @end
    .m:

    Code:
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    @synthesize accelerometer;
    
    float value;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        value = 5.0f;
        self.accelerometer = [UIAccelerometer sharedAccelerometer];
        self.accelerometer.delegate = self;
        self.accelerometer.updateInterval = 1.0f / value;
    }
    
    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
    {
        NSLog(@"X: %g",acceleration.x);
        NSLog(@"Y: %g",acceleration.y);
        NSLog(@"Z: %g",acceleration.z);
    }
    
    - (IBAction) sliderValueChanged:(UISlider *)slider 
    {  
        NSLog(@"Value: %f",value);
        self.accelerometer.updateInterval = 1.0f / slider.value;
    } 
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    @end
     
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    Woo hoo! He's figured out that he actually needs to change the update interval!

    Now, to the next issue. Why are you using 1/slider value? I would think you would want to set your slider value range to a good range (.01 to 2.0 or some such) and then assign the slider value directly to self.accelerometer.updateInterval.


    If you use 1/slider value, then a 0 slider value is going to cause you to crash, and slider values less than 1 are going to give you very large update intervals.

    It's pretty clear from your posts that you are thrashing around in the dark with little idea what you are doing. I suggest you spend some quality time with a good iOS development book before trying to do much more. Work through some exercises and learn the fundamentals or you are going to (a)get very frustrated that nothing works and (b) burn through all the goodwill on the various developer sites and people will start ignoring your questions.
     
  10. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    The accelerometer property is superfluous (unnecessary and irrelevant). Every place you use the property can just as easily use:
    Code:
    [UIAccelerometer sharedAccelerometer]
    
    and it would work exactly the same.

    This is why I asked you to explain why you thought a property would solve anything. Because it really doesn't solve anything, and is completely irrelevant to the problem.

    The problem was that the method that executed when the slider value changed wasn't setting the updateInterval of any UIAccelerometer object. Change that and the problem is solved.
     
  11. troop231 thread starter macrumors 603

    Joined:
    Jan 20, 2010
    #11
    My slider's minimum value in the .xib is 1, and it's maximum value is 10. UIAccelerometer will be completely removed in the final app, the only reason I'm using it now is for testing purposes. The actual data will be coming from an aircraft instrument when everything is said and done.

    The slider is for changing the refresh rate of the data, i.e sensitivity of the instrument.
     

Share This Page