[Tutorial] Simple Accelerometer [Source Code]

Discussion in 'iOS Development' started by JoshuaCaputo, Feb 28, 2009.

  1. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    A few weeks ago I was very curious about how to work the accelerometer. I found many ways to do it but I wasn't using it that much in my application so I decided I would go with the easy way. There are many ways to complicate this accelerometer's functions but this is just the basics. Not to much math included, or needed.

    Here is the core of the Accelerometer Application I have made to show you how to do it this way:

    viewController.h
    Code:
    #import <UIKit/UIKit.h>
    #define kAccelerometerFrequency     40
    
    @interface simpleTiltViewController : UIViewController <UIAccelerometerDelegate> {
    
    	CGPoint deviceTilt;
    }
    
    @end
    viewController.m
    Code:
    #import "simpleTiltViewController.h"
    
    @implementation simpleTiltViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    
    	
        [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    }
    
    // UIAccelerometerDelegate method, called when the device accelerates.
    - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
        // Update the accelerometer graph view
    	deviceTilt.x = acceleration.x;
    	deviceTilt.y = acceleration.y;
    }
    
    @end
    

    Source Code / Demonstration

    Please Register or Log in to view images


    I did run this one in instruments and had no leaks so I guess thats a start

    Please Register or Log in to view images

    Attached Files:

  2. Tyr0

    Tyr0 New Member

    Joined:
    Dec 17, 2007
    Messages:
    533
    Likes Received:
    6
    Device:
    iPhone 3GS (White)
    Sweet! This could be useful for one of my apps, or just for fun!
    +rep(i wish)
  3. flips30240

    flips30240 New Member

    Joined:
    Feb 5, 2009
    Messages:
    195
    Likes Received:
    0
    nice. would you be interested in writing a program for an external camera for the ipod touch?
  4. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    Pshh, we haven't had rep in forever. Abusers Abusers Abusers : (.
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Haha I just had to laugh when I saw that. Sorry.
  6. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    In the tutorial it is landscape moving left to right i want the iphone to be vertical and move left to right how do you change it ?

    look and testing all day no luck
  7. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Hold on. Seriously?

    It's as easy as setting the device orientation in your app delegate.
  8. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Simply. Make an image with the track in the dead centre at 320x480 (Vertical), going from left to right. Put a UIButton on the middle of the track at the centre and use JoshuaCaputo's code:

    [OBJC]-(void)onTimer{

    int speed;
    speed = 5;

    Object.center = CGPointMake(Object.center.x+position.x,Object.center.y+position.y);

    //Moves the name to wherever the Object is.
    NAME.center = CGPointMake(Object.center.x, 131);

    //These two stop the Object from moving off the line.
    if (Object.center.x > 444){Object.center = CGPointMake(444,160);}
    if (Object.center.x < 36){Object.center = CGPointMake(36,160);}


    if (deviceTilt.y > 0.3f) {position.x = -speed;}
    else if (deviceTilt.y < -0.3f) {position.x = speed;}
    else if (0.2f < deviceTilt.y > -0.2f){position.x = 0;}

    // These just change the image when it gets offset from the center.
    if (Object.center.x > 242){[Object setImage:[UIImage imageNamed

    Please Register or Log in to view images

    "Add.png"] forState:UIControlStateNormal];}
    if (Object.center.x < 238){[Object setImage:[UIImage imageNamed

    Please Register or Log in to view images

    "Subtract.png"] forState:UIControlStateNormal];}

    //If the Object is in the 4 center most x integers.
    /*if (Object.center.x > 238){
    if (Object.center.x < 242){[Object setImage:[UIImage imageNamed

    Please Register or Log in to view images

    "Smiley.png"] forState:UIControlStateNormal];
    }
    }
    */
    }[/OBJC]

    And change:

    NAME.center = CGPointMake(Object.center.x, 131); to wherever you put the "NAME" label on the Y Axis (This can be found in IB under the Ruler Tab). And change:
    [OBJC]if (Object.center.x > 444(change to 209 or 280)){Object.center = CGPointMake(444,160);}[/OBJC] -- Edit the above 444,160 respectively.
    [OBJC]if (Object.center.x < 36){Object.center = CGPointMake(36,160);}[/OBJC] to however far you want the button/name to slide across the 320 pixels. preferably 290 or 280.

    Done.

    It should definitely work.
  9. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    Very Nice addition.
  10. IPHONE DEVELOPER

    IPHONE DEVELOPER New Member

    Joined:
    Oct 26, 2009
    Messages:
    1
    Likes Received:
    0
    Device:
    3G iPod touch
    neatly explained

    thank you senior...

Share This Page