How do I compare a constant value to a continuously-updated Accelerometer value each time round a loop? - objective-c

As it was suggested to me in a previous post of mine, the following code takes the data coming from the accelerometer the "minute" the assignment : CMAccelerometerData* data = [manager accelerometerData]; is performed, and then extracts from that data the acceleration exercised on the x-Axis and stores its value in a double (double x) :
CMMotionManager* manager = [[CMMotionManager alloc] init];
CMAccelerometerData* data = [manager accelerometerData];
double x = [data acceleration].x;
Suppose the value stored is 0.03 and suppose that I want to use it in a while loop as follows :
while (x > 0)
{
// do something
}
the above loop will obviously run forever
However, what if I used the following code instead :
CMMotionManager* manager = [[CMMotionManager alloc] init];
while([[manager accelerometerData] acceleration].x > 0)
{
// do something
}
wouldn't I be now comparing zero to a different value each time round the loop?
(which is what I'm going for in my project anyway..)
any thoughts?
the reason I'm asking this is the following :
I want to check the values coming from the x-Axis over a certain period of time, rather than keep checking them at regular intervals, so I basically want to write a loop that would look something like this :
if ([[manager accelerometerData] acceleration].x > 0 )
{
// initialiseTimer
}
while ([[manager accelerometerData] acceleration].x > 0 )
{
if( checkTimer >=250ms )
{
stopTimer;
printOut("X-Axis acceleration was greater than zero for at least 250ms");
breakFromLoop;
}
}
I know the code in my 2nd if-block isn't valid Objective-C..This was just to give you an idea of what I'm going for..

This has a simple solution.
1)Declare an instance variable x that you update each time the accelerometer tell you to.
2)Compare this x to whatever value you need in the loop .
Hope this helps.
Regards,
George

Related

Returning the smallest value within an Array List

I need to write a method that returns me the smallest distance (which is a whole number value) within an Array List called "babyTurtles". There are 5 turtles within this array list and they all move a random distance each time the program is ran.
I've been trying to figure out how to do it for an hour and all I've accomplished is making myself frustrated and coming here.
p.s.
In my class we wrote this code to find the average distance moved by the baby turtles:
public double getAverageDistanceMovedByChildren() {
if (this.babyTurtles.size() == 0) {
return 0;
}
double sum = 0;
for (Turtle currentTurtle : this.babyTurtles) {
sum = sum + currentTurtle.getDistanceMoved();
}
double average = sum / this.babyTurtles.size();
return average;
}
That's all I've got to work on, but I just can't seem to find out how to do it.
I'd really appreciate it if you could assist me.
This will give you the index in the array list of the smallest number:
int lowestIndex = distanceList.indexOf(Collections.min(distanceList));
You can then get the value using this:
int lowestDistance = distanceList.get(lowestIndex);

Selection a bool through randomizer

I have a total of 6 booleans and the only thing separating them is a number. They're named checker0 though 5.
So checker0, checker1, checker2, checker3, checker4 and checker5.
All of these grants or denies access to certain parts of the app wether the bool is true or false.
I then have a randomiser using:
randomQuestionNumber = arc4random_uniform(5);
So say we get number 3, checker3 = true;
But my question now is would it be possible to set this one to true without having to go thru if statements.
My idea was to implement the way you print a int to say the NSLog using the %d.
NSLog(#"The number is: %d", randomQuestionNumber);
So something like:
checker%d, randomQuestionNumber = true.
Would something like that be possible? So i won't have to do like this:
if (randomQuestionNumber == 0) {
checker0 = true;
}
else if (randomQuestionNumber == 1)
{
checker1 = true;
}
Thanks you very much! :)
Every time you find yourself in a situation when you name three or more variables checkerN you know with a high degree of probability that you've missed a place in code where you should have declared an array. This becomes especially apparent when you need to choose one of N based on an integer index.
The best solution would be to change the declaration to checker[6], and using an index instead of changing the name. If this is not possible for some reason, you could still make an array of pointers, and use it to make modifications to your values, like this:
BOOL *ptrChecker[] = {&checker0, &checker1, &checker2, ...};
...
*ptrChecker[randomQuestionNumber] = true;

game maker random cave generation

I want to make a cave explorer game in game maker 8.0.
I've made a block object and an generator But I'm stuck. Here is my code for the generator
var r;
r = random_range(0, 1);
repeat(room_width/16) {
repeat(room_height/16) {
if (r == 1) {
instance_create(x, y, obj_block)
}
y += 16;
}
x += 16;
}
now i always get a blank frame
You need to use irandom(1) so you get an integer. You also should put it inside the loop so it generates a new value each time.
In the second statement, you are generating a random real value and storing it in r. What you actually require is choosing one of the two values. I recommend that you use the function choose(...) for this. Here goes the corrected statement:
r = choose(0,1); //Choose either 0 or 1 and store it in r
Also, move the above statement to the inner loop. (Because you want to decide whether you want to place a block at the said (x,y) location at every spot, right?)
Also, I recommend that you substitute sprite_width and sprite_height instead of using the value 16 directly, so that any changes you make to the sprite will adjust the resulting layout of the blocks accordingly.
Here is the code with corrections:
var r;
repeat(room_width/sprite_width) {
repeat(room_height/sprite_height) {
r = choose(0, 1);
if (r == 1)
instance_create(x, y, obj_block);
y += sprite_height;
}
x += sprite_width;
}
That should work. I hope that helps!
Looks like you are only creating a instance if r==1. Shouldn't you create a instance every time?
Variable assignment r = random_range(0, 1); is outside the loop. Therefore performed only once before starting the loop.
random_range(0, 1) returns a random real number between 0 and 1 (not integer!). But you have if (r == 1) - the probability of getting 1 is a very small.
as example:
repeat(room_width/16) {
repeat(room_height/16) {
if (irandom(1)) {
instance_create(x, y, obj_block)
}
y += 16;
}
x += 16;
}
Here's a possible, maybe even better solution:
length = room_width/16;
height = room_height/16;
for(xx = 0; xx < length; xx+=1)
{
for(yy = 0; yy < height; yy+=1)
{
if choose(0, 1) = 1 {
instance_create(xx*16, yy*16, obj_block); }
}
}
if you want random caves, you should probably delete random sections of those blocks,
not just single ones.
For bonus points, you could use a seed value for the random cave generation. You can also have a pathway random generation that will have a guaranteed path to the finish with random openings and fake paths that generate randomly from that path. Then you can fill in the extra spaces with other random pieces.
But in regards to your code, you must redefine the random number each time you are placing a block, which is why all of them are the same. It should be called inside of the loops, and should be an integer instead of a decimal value.
Problem is on the first line, you need to put r = something in the for cycle

integer that becomes 0 in a strange way

I have an integer : (using xcode for iPhone ) .
int wordCounter=1;
Later on, when i fill arrays with it-as a pointer,in a for loop, it somehow becomes 0 after it becomes a certain number . i have checked my program many times for bugs, and i realize that i dont even have a decrement on it anywhere . i do have wordCounter++.
Now i have seen that i have many lines like this,that after them it becomes 0 :
if(tempBinary[countWords-1][j] != tempBinary[countWords][j])
so i was thinking that countWords-1 decrement it down again and again, is that possible?
i dont have any other decrement or initialization on this variable in my whole program.
i COULD see that when the tempBinary defined as size 7, it initialize at 5, and if tempBinary is 5, it zeroing on 3 ..
is it possible that the array is overflow and is zeroing it? i dont think so..
whats wrong here ?
thanks .
EDIT (one of the problematic states)
int countWords=1;
int stabilityK=0;
tempBinary[0][0]= tempBinary[0][1]=tempBinary[0][2]=tempBinary[0][3]=tempBinary[0][4]=tempBinary[0][5]=tempBinary[0][6]=tempBinary[0][7]=1 ;
for(int k=0;k<numOfBuffers;k++)
{
NSLog(#"countwords:%d",countWords-1);
float *temp=getFFT(buffersRing[k],buffersRing[k][0]);
for(int j=0;j<wordSize;j++)
{
switch(state_on_signal)
{
case WAIT_FOR_SECOND_CHANGE:
//get new word
if(temp[goodBins[j]] > decisionLine[j])
tempBinary[countWords][j]=1;
else
tempBinary[countWords][j]=0;
if(tempBinary[countWords-1][j] != tempBinary[countWords][j])
newData=1;
NSLog(#"s1: countwords:%d",countWords-1);
if(j==wordSize-1)
{
NSLog(#"s2: countwords:%d",countWords-1);
NSLog(#"PRE TEMP:%d%d%d%d%d%d%d%d",tempBinary[countWords-1][0],tempBinary[countWords-1][1],tempBinary[countWords-1][2],tempBinary[countWords-1][3],tempBinary[countWords-1][4],tempBinary[countWords-1][5],tempBinary[countWords-1][6],tempBinary[countWords-1][7] );
NSLog(#"NEW TEMP-WAIT FOR SECOND CHANGE :%d%d%d%d%d%d%d%d",tempBinary[countWords][0],tempBinary[countWords][1],tempBinary[countWords][2],tempBinary[countWords][3],tempBinary[countWords][4],tempBinary[countWords][5],tempBinary[countWords][6],tempBinary[countWords][7] );
NSLog(#"s3: countwords:%d",countWords-1);
//TAKE NEW DATA
if(newData==1)
{
NSLog(#" TOOK new BINARY at current k:%d, so took data at: %d",k,(k+markedK)/2);
for(int s=0;s<wordSize;s++)
{
if( getFFT(buffersRing[(k+markedK)/2],buffersRing[(k+markedK)/2][0])[goodBins[s] ] >decisionLine[s] )
binary[countWords-1][s]= 1;
else
binary[countWords-1][s]= 0;
}
NSLog(#"s4: countwords:%d",countWords-1);
NSLog(#"BINARY%d: %d%d%d%d%d%d%d%d :%d",(countWords-1), binary[(countWords-1)][0], binary[(countWords-1)][1], binary[(countWords-1)][2], binary[(countWords-1)][3], binary[(countWords-1)][4], binary[(countWords-1)][5], binary[(countWords-1)][6], binary[(countWords-1)][7],[self getDecimal:binary[countWords-1]]);
countWords++;
markedK=k;
state_on_signal=WAIT_FOR_STABILITY;
}
newData=0;
}
break;
so i was thinking that countWords-1 decrement it down again and again,
is that possible?
No
Usually this happens when you misuse a pointer and write a value to the wrong address in memory.
For example: where and how is declared tempBinary ?
As it's not possible (at least for me) to see the problem just looking at your code I'd suggest to step through your code with the debugger and see when countWords gets changed. Here lies the problem (at least one of them).

How do you check if an NSInteger is greater than another NSinteger?

I'm trying to write code that detects if an integer is greater than another integer. Is this possible?
Here is what i've done so far.
if (NumCorrect >> NumWrong) {
btnCool.title = #"Awww";
}
else {
btnCool.title = #"Cool!";
}
All its doing is going to the else
EDIT:
NSString *numCorrect = [NSString stringWithFormat:#"%d",NumCorrect];
NSString *numWrong = [NSString stringWithFormat:#"%d", NumWrong];
lblWrong.text = numWrong;
lblCorrect.text = numCorrect;
if (NumCorrect > NumWrong) {
btnCool.title = #"Awww";
} else {
btnCool.title = #"Cool!";
}
Use single >
if (NumCorrect > NumWrong) {
btnCool.title = #"Awww";
} else {
btnCool.title = #"Cool!";
}
Double >> is a bit shift operation. You shift every bit in the binary representation of your variable NumCorrect NumWrong amount of bytes to the right. In almost all cases this will return in a number other then 0, which will then treated as a false value and thus the else block is executed.
Almost perfect - just take off one of those >'s. >> and << are for "bit-shifting", a weird hold-over from the earliest days of programming. You're not gonna use them much. What you really want is > and <, which is for testing if numbers are greater than each other or less than each other.
In addition, you may remember from math class that ≥ and ≤ (greater-than-or-equal-to and less-than-or-equal-to) are useful operations as well. Because there's no symbols for those on most keyboards, however, C and Xcode use >= and <= instead.
Finally, you may already know this, but to check if two numbers are exactly equal to each other you can use == (because = is used for setting the contents of variables).
Hope that's helpful!