How to call accelerator method using IBAction method? - iphone

I want to add a start button which will start the accelerometer, how can I set an IBAction to do that, in my code I use:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
x.text = [NSString stringWithFormat:#"X is: %f", acceleration.x];
y.text = [NSString stringWithFormat:#"Y is: %f", acceleration.y];
z.text = [NSString stringWithFormat:#"Z is: %f", acceleration.z];
NSLog (#"Y = #%f", y.text);
}
-(IBAction)startPedometerPressed {
[startButton accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration];
}
This returns an error because of the undeclared identifier 'accelerometer'.
I'm sure there is something wrong in how I call the method but not sure what is it!

you have to write your accelerometer initialization code in the IBAction function. remove it from ViewDidLoad. now accelerometer works after the button pressed.
-(IBAction)startPedometerPressed
{
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
Hope this helps.

Related

How to move a object by titling the screen in cocos2d

i have his code for making movemnt of the object through horizontal postion.
#define kAccelerationSpeed 10
#define kAccelerometerFrequency 200
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
CGPoint pt = thePlayer.position ;
CGFloat accel = acceleration.x * kAccelerationSpeed;
pt = [[CCDirector sharedDirector] convertToGL:pt];
pt = ccp (pt.x+accel>0 , yLocationOfPlayer+accel<768 );
}
in init statment
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / kAccelerometerFrequency)];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
whats wrong with the above code,it is not working,when i tilt the screen,it is not making any movemnt for the "theplayer" object.please help me to do this.
thanks in advance.
Are you sure your delegates are setup to report accelerometer information? Are you also assigning thePlayer.position in the accelerometer callback? I can't recall why but I do recall trying assignment within the callback and the results were rather stuttered and problematic. The solution for me was to have a variable the accelerometer delegate call will modify and assign the position of thePlayer inside of a tick script based on the variable update in the delegate callback.
you're not assigning the position of the player sprite. Try this:
NSLog(#"position before = %#", [NSValue valueWithCGPoint:thePlayer.position]);
thePlayer.position = ccp (pt.x+accel>0 , yLocationOfPlayer+accel<768 );
NSLog(#"position after = %#", [NSValue valueWithCGPoint:thePlayer.position]);
The logging will help you to determine if 1) the delegate method is getting fired and 2) the positions are as you are expecting.

iPhone accelerometer changing senstivity - Cocoa Touch

Hello
I want the sensitivity changing, for when the user moves the device. At the moment its not very sensitive, I believe its on default. I want it so its more sensitive, so when the user shakes the phone a little bit the sound plays.
Here is the code
Thanks
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(motion == UIEventSubtypeMotionShake)
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"whip" ofType:#"wav"];
if (theAudio) [theAudio release];
NSError *error = nil;
theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
if (error)
NSLog(#"%#",[error localizedDescription]);
theAudio.delegate = self;
[theAudio play];
}
}
First, make sure your interface adopts the UIAccelerobeterDelegate protocol.
#interface MainViewController : UIViewController <UIAccelerometerDelegate>
Now in your implementation:
//get the accelerometer
self.accelerometer = [UIAccelerometer sharedAccelerometer];
self.accelerometer.updateInterval = .1;
self.accelerometer.delegate = self;
Implement the delegate method:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
float x = acceleration.x;
float y = acceleration.y;
float b = acceleration.z;
// here you can write simple change threshold logic
// so you can call trigger your method if you detect the movement you're after
}
The values that the accelerometer returns for x,y, and z will always be a float between -1.0 and positive 1.0. You should call NSLog and output to the console x,y, and z values so you can get a feel for what they mean. Then you can develop a simple way to detect movement.
You cannot change how the shake events work. If you need something different you will have to write your own shake-detection code based on the x/y/z forces given to you by [UIAccelerometer sharedAccelerometer]; If you set the sharedAccelerometer's delegate to your class, you can get the accelerometer's accelerometer:didAccelerate: delegate callback.
If you're trying to recreate the shake gesture, here are a few things to bear in mind:
A shake is a motion in a direction, followed by a motion in the generally opposite direction. That means you'll have to keep track of your previous acceleration vectors, so that you know when they change. So that you don't miss it, you'll have to sample from the accelerometer pretty frequently.
CGFloat samplesPerSecond = 30;
[[UIAccelerometer sharedAccelerometer] setDelegate: self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1.0 / samplesPerSecond];
Then in your delegate callback:
- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration {
// TODO add this acceleration to a list of accelerations and keep the most recent (perhaps 30)
// TODO analyze accelerations and determine if a direction change occurred
// TODO if it did, then a shake occurred! Clear the list of accelerations and perform your action
}

Retrieving Accelerometer Values?

I want to retrieve the iPhone's accelerometer values and print them into a label.
Any help is appreciated.
Put this in viewDidLoad method of your ViewController which is UIAccelerometerDelegate:
UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
accelerometer.updateInterval = 1.0f/60.0f;
accelerometer.delegate = self;
Then add this method:
-(void)accelerometer:(UIAccelerometer *)acelerometer didAccelerate:(UIAcceleration*)acceleration
{
// here you can use acceleration.x, acceleration.y, acceleration.z
}

Apply loop on accelerometer reading?

Actually i want to apply a loop on accelerometer, means i want to start a accelerometer on behalf of loop. if i want to start loop one then want to perform accelerometer reading one time. if loop will run twice then want to run accelerometer twice. But it not happens. What should i do to control accelerometer.
Have a quick look on code
in viewdidload
for (int i=0; i<3; i++)
{
NSLog(#"Hellooooooooo",i);
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
}
and in accelerometer didAccelerate
{
float xx = -[acceleration x];
float yy = [acceleration y];
}
I don't know what is going wrong ?
help me if u have some idea about it.
thanks in advance for any help.
set the update interval to something not 0.
And if you don't need the accelerometer anymore set it back to 0 and remove your delegate. If you want 3 measurements you have to change your code, setting the delegate 3 times has no effect.
You could use something like this:
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
static int count = 0;
count++;
float xx = -[acceleration x];
float yy = [acceleration y];
if (count >= 3) {
[accelerometer setUpdateInterval:0];
[accelerometer setDelegate:nil];
count = 0;
}
NSLog(#"%f %f", xx, yy);
}
- (void)viewDidLoad {
[super viewDidLoad];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/measurementsPerSecond];
}
I have solved this issue .
Just apply a int count variable and assign the count value initialize. Take the value from the user, according to your needs and check out the condition in the accelerometer delegate. Increase the count variable every time and stop the accelerometer at specific count which u want.
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:
(UIAcceleration *)acceleration
{
// Get the current device angle
float xx = -[acceleration x];
float yy = [acceleration y];
if(count <= userChoice)
{
if(deviceOrientation != UIInterfaceOrientationPortraitUpsideDown)
{
// here we will start NStimer to calcualte actual time
NSLog(#"Count Before = %d",count);
alert.hidden=TRUE;
deviceOrientation = UIInterfaceOrientationPortraitUpsideDown;
[self startTimer];
count++;
NSLog(#"Count after = %d",count);
}
}
if (count <= 1)
{
count++;
}
else
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:#"Stop" message:#"Your rounds are ended." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}

didAccelerate doesn't fire?

I can't get didAccelerate to trigger. I have the delegate implemented in my header file. I've set a breakpoint in the method:
- (void) accelerometer: (UIAccelerometer *)accelerometer didAccelerate: (UIAcceleration *)acceleration {
int i = 0;
}
I've tried it with two different apps but still nothing. Any ideas on what I'm doing wrong?
try to set accelerometer's delegate to self. BTW, did you try to check it on a real device?