Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 4 months ago.
tap detection using 3-axis accelerometer
sir,i worked for tapdetection. here is the code please go through the code..i am geting an error when i run this code sir ,,
#include "mbed.h" #include "MMA7660.h" #include "C12832_lcd.h" C12832_LCD lcd; MMA7660 MMA(p28, p27); DigitalOut connectionLed(LED1); int main() { int n=0; while(n<10) { int z; if (MMA.testConnection()) connectionLed = 1; z= MMA.tapDetection() ; if(z==5) { lcd.printf("there is a tap"); } Else { lcd.printf("There is no tap"); } n++; } }
Sir ,In MMA7660.cpp i added a function int tapDetection()
int MMA7660::tapDetection() { int k; char intreg = read(MMA7660_INT_R); intreg &= 0x04; write(MMA7660_INT_R,intreg); //for enabling the interrupt char sample = read(MMA7660_SR_R); sample &= 0x00; // for seting samplerate 120samples/second write(MMA7660_SR_R,sample); char detectreg = read(MMA7660_PDET_R); detectreg &= 0x00; //for enabling tap in all directions char tiltreg = read(MMA7660_TILT_R); if(tiltreg == 0x32) { k=5; return (k); } else (tiltreg == 0x00) { k=10; return (k); } }
2 Answers
11 years, 4 months ago.
you have a capital 'E' on line 18 for your 'else' statement. Try changing this?
11 years, 4 months ago.
In the second listing - the compilation error is caused by a missing 'if' after the 'else' on line 19, the bit in brackets on line 20 is really the contents of the if clause. In order to compile, that line should read either:
} else if(tiltreg == 0x00)
(assuming you meant to check if tiltreg is zero), or should delete line 20 if you didn't.
However, going into MMA7660.cpp directly to add new functions isn't really the right thing to do, becomes much harder to maintain doing it that way, would have been less messy to leave the library alone and subclass it, i.e. create a new class that inherits from it and extends with your new functionality.
There are plenty of other problems with the second listing, beyond the obvious awful formatting - what are you expecting "sample &= 0x00;" will do? It means "take the current value called 'sample', and do a bitwise logical AND with the value '0'". I.e., it sets 'sample' to 0, no matter what it was before - but if that was what you intended, why would you not have just written "write(MMA7660_SR_R, 0);" and not bothered reading it first? The similar lines about reading the value of the 'detect' register have you fetching the value, setting it to zero, then not even writing it back to the register, so all you did in that part is set a variable to zero and then not even do anything with it, no effect on registers. Which is probably just as well, since I don't see MMA7660_PDET_R defined in the mbed library anyway - you've probably copied that from somewhere else, need to define it as 0x09. And the bit reading the tilt register seems to simply ask if it is the value '0x32', i.e. decimal 50 or binary 00110010, which seems a bit specific - my reading of the MMA7660 library code suggests that different bitwise parts of this register mean different things, look at how it's used in the getOrientation and getSide functions, where it uses masks and bit shifts to get at specific bitwise sections of the register - I'm not sure why it would return just a specific value of 0x32. But then, I'm not sure why your function is trying to return values of either 5 or 10 either.
Since the tap detection features in this chip are intended for it being used inside a mobile phone, I'd be quite surprised if it also worked for detecting a footstep with the board strapped to your ankle, so there's a good chance that even if you get this code working from a coding perspective, it may still not actually do what you want. Luckily though, I did post a suggested algorithm as an answer to one of your various other 'please help urgent accelerometer' questions.
What is the error message?
posted by Stephen Paulger 03 May 2013