MMA8452 3 axis accelerometer.
Dependencies: MMA8452 N5110 mbed
main.cpp@2:a659621a83b0, 2015-05-10 (annotated)
- Committer:
- a1163480319
- Date:
- Sun May 10 15:41:32 2015 +0000
- Revision:
- 2:a659621a83b0
- Parent:
- 0:bfe6cbe4dd56
add led
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a1163480319 | 0:bfe6cbe4dd56 | 1 | #include "mbed.h" |
a1163480319 | 0:bfe6cbe4dd56 | 2 | #include "N5110.h" |
a1163480319 | 0:bfe6cbe4dd56 | 3 | #include "MMA8452.h" |
a1163480319 | 0:bfe6cbe4dd56 | 4 | |
a1163480319 | 0:bfe6cbe4dd56 | 5 | |
a1163480319 | 0:bfe6cbe4dd56 | 6 | |
a1163480319 | 0:bfe6cbe4dd56 | 7 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
a1163480319 | 0:bfe6cbe4dd56 | 8 | MMA8452 mma8452(p28,p27); // SDA, SCL |
a1163480319 | 0:bfe6cbe4dd56 | 9 | Serial serial(USBTX,USBRX);//serial port |
a1163480319 | 0:bfe6cbe4dd56 | 10 | InterruptIn button(p17);//button |
a1163480319 | 2:a659621a83b0 | 11 | BusOut leds(p24);//led |
a1163480319 | 0:bfe6cbe4dd56 | 12 | Ticker buzzer_timer;//buzzer timer |
a1163480319 | 0:bfe6cbe4dd56 | 13 | PwmOut buzzer(p21);//buzzer |
a1163480319 | 0:bfe6cbe4dd56 | 14 | |
a1163480319 | 0:bfe6cbe4dd56 | 15 | AnalogIn pot(p20); /* Potentiometer middle pin connected to P0_11, other two ends connected to GND and 3.3V */ |
a1163480319 | 0:bfe6cbe4dd56 | 16 | |
a1163480319 | 0:bfe6cbe4dd56 | 17 | |
a1163480319 | 0:bfe6cbe4dd56 | 18 | |
a1163480319 | 0:bfe6cbe4dd56 | 19 | |
a1163480319 | 0:bfe6cbe4dd56 | 20 | int fsm[4] = {1,2,4,8}; // simple FSM counter has 4 states |
a1163480319 | 0:bfe6cbe4dd56 | 21 | int state = 0; // initial state 0 |
a1163480319 | 0:bfe6cbe4dd56 | 22 | // defines for direction |
a1163480319 | 0:bfe6cbe4dd56 | 23 | #define UP 0 |
a1163480319 | 0:bfe6cbe4dd56 | 24 | #define DOWN 1 |
a1163480319 | 0:bfe6cbe4dd56 | 25 | #define MID 2 |
a1163480319 | 0:bfe6cbe4dd56 | 26 | |
a1163480319 | 0:bfe6cbe4dd56 | 27 | int direction = UP; // initially count up |
a1163480319 | 0:bfe6cbe4dd56 | 28 | |
a1163480319 | 0:bfe6cbe4dd56 | 29 | float frequency=659; |
a1163480319 | 0:bfe6cbe4dd56 | 30 | |
a1163480319 | 0:bfe6cbe4dd56 | 31 | |
a1163480319 | 0:bfe6cbe4dd56 | 32 | int k=0; |
a1163480319 | 0:bfe6cbe4dd56 | 33 | |
a1163480319 | 0:bfe6cbe4dd56 | 34 | void buttonPressed() // toggle direction in ISR |
a1163480319 | 0:bfe6cbe4dd56 | 35 | { |
a1163480319 | 0:bfe6cbe4dd56 | 36 | k++; |
a1163480319 | 0:bfe6cbe4dd56 | 37 | |
a1163480319 | 0:bfe6cbe4dd56 | 38 | direction=k%3; |
a1163480319 | 0:bfe6cbe4dd56 | 39 | |
a1163480319 | 0:bfe6cbe4dd56 | 40 | |
a1163480319 | 0:bfe6cbe4dd56 | 41 | } |
a1163480319 | 0:bfe6cbe4dd56 | 42 | |
a1163480319 | 0:bfe6cbe4dd56 | 43 | int main() |
a1163480319 | 0:bfe6cbe4dd56 | 44 | { |
a1163480319 | 0:bfe6cbe4dd56 | 45 | lcd.init(); |
a1163480319 | 0:bfe6cbe4dd56 | 46 | mma8452.init(); // 100 Hz update rate, ±4g scale |
a1163480319 | 0:bfe6cbe4dd56 | 47 | |
a1163480319 | 0:bfe6cbe4dd56 | 48 | float ain; |
a1163480319 | 0:bfe6cbe4dd56 | 49 | Acceleration acceleration; // Accleration structure declared in MMA8452 class |
a1163480319 | 0:bfe6cbe4dd56 | 50 | |
a1163480319 | 0:bfe6cbe4dd56 | 51 | |
a1163480319 | 0:bfe6cbe4dd56 | 52 | button.rise(&buttonPressed); // Use internal pullup for pushbutton |
a1163480319 | 0:bfe6cbe4dd56 | 53 | |
a1163480319 | 0:bfe6cbe4dd56 | 54 | |
a1163480319 | 0:bfe6cbe4dd56 | 55 | wait(.001);// Delay for initial pullup to take effect |
a1163480319 | 0:bfe6cbe4dd56 | 56 | |
a1163480319 | 0:bfe6cbe4dd56 | 57 | |
a1163480319 | 0:bfe6cbe4dd56 | 58 | while(1) |
a1163480319 | 0:bfe6cbe4dd56 | 59 | { |
a1163480319 | 0:bfe6cbe4dd56 | 60 | |
a1163480319 | 0:bfe6cbe4dd56 | 61 | acceleration = mma8452.readValues();// read current values and print over serial port |
a1163480319 | 0:bfe6cbe4dd56 | 62 | |
a1163480319 | 0:bfe6cbe4dd56 | 63 | |
a1163480319 | 0:bfe6cbe4dd56 | 64 | |
a1163480319 | 0:bfe6cbe4dd56 | 65 | //calculate the angle |
a1163480319 | 0:bfe6cbe4dd56 | 66 | float angleX = atan(acceleration.x/sqrt(pow(acceleration.y,2)+pow(acceleration.z,2)))*64; |
a1163480319 | 0:bfe6cbe4dd56 | 67 | float angleY = atan(acceleration.y/sqrt(pow(acceleration.x,2)+pow(acceleration.z,2)))*64; |
a1163480319 | 0:bfe6cbe4dd56 | 68 | float angleZ = atan(acceleration.z/sqrt(pow(acceleration.y,2)+pow(acceleration.x,2)))*64; |
a1163480319 | 0:bfe6cbe4dd56 | 69 | |
a1163480319 | 0:bfe6cbe4dd56 | 70 | |
a1163480319 | 0:bfe6cbe4dd56 | 71 | |
a1163480319 | 0:bfe6cbe4dd56 | 72 | //store the acceleration data |
a1163480319 | 0:bfe6cbe4dd56 | 73 | char buffer1[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 74 | int length = sprintf(buffer1,"x = %.2f g",acceleration.x); |
a1163480319 | 0:bfe6cbe4dd56 | 75 | char buffer2[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 76 | length = sprintf(buffer2,"y = %.2f g",acceleration.y); |
a1163480319 | 0:bfe6cbe4dd56 | 77 | char buffer3[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 78 | length = sprintf(buffer3,"z = %.2f g",acceleration.z); |
a1163480319 | 0:bfe6cbe4dd56 | 79 | |
a1163480319 | 0:bfe6cbe4dd56 | 80 | |
a1163480319 | 0:bfe6cbe4dd56 | 81 | |
a1163480319 | 0:bfe6cbe4dd56 | 82 | |
a1163480319 | 0:bfe6cbe4dd56 | 83 | //store angle data |
a1163480319 | 0:bfe6cbe4dd56 | 84 | char buffer4[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 85 | length = sprintf(buffer4,"X=%.3gDegree",angleX); |
a1163480319 | 0:bfe6cbe4dd56 | 86 | char buffer5[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 87 | length = sprintf(buffer5,"Y=%.3gDegree",angleY); |
a1163480319 | 0:bfe6cbe4dd56 | 88 | char buffer6[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 89 | length = sprintf(buffer6,"Z=%.3gDegree",angleZ); |
a1163480319 | 0:bfe6cbe4dd56 | 90 | char buffer7[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 91 | length = sprintf(buffer7,"ACCELEROMETER"); |
a1163480319 | 0:bfe6cbe4dd56 | 92 | char buffer8[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 93 | length = sprintf(buffer8,"PROTRACTOR"); |
a1163480319 | 0:bfe6cbe4dd56 | 94 | char buffer9[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 95 | length = sprintf(buffer9,"Y"); |
a1163480319 | 0:bfe6cbe4dd56 | 96 | char buffer10[14]; |
a1163480319 | 0:bfe6cbe4dd56 | 97 | length = sprintf(buffer10,"X"); |
a1163480319 | 0:bfe6cbe4dd56 | 98 | |
a1163480319 | 0:bfe6cbe4dd56 | 99 | |
a1163480319 | 0:bfe6cbe4dd56 | 100 | |
a1163480319 | 0:bfe6cbe4dd56 | 101 | |
a1163480319 | 0:bfe6cbe4dd56 | 102 | wait(0.1); |
a1163480319 | 0:bfe6cbe4dd56 | 103 | lcd.clear(); |
a1163480319 | 0:bfe6cbe4dd56 | 104 | |
a1163480319 | 0:bfe6cbe4dd56 | 105 | |
a1163480319 | 0:bfe6cbe4dd56 | 106 | |
a1163480319 | 0:bfe6cbe4dd56 | 107 | if (angleY>=90||angleY<=-90||angleX>=90||angleX<=-90){ |
a1163480319 | 0:bfe6cbe4dd56 | 108 | |
a1163480319 | 0:bfe6cbe4dd56 | 109 | buzzer.period(1/frequency); // set PWM period |
a1163480319 | 0:bfe6cbe4dd56 | 110 | buzzer=0.5; // set duty cycle |
a1163480319 | 0:bfe6cbe4dd56 | 111 | |
a1163480319 | 0:bfe6cbe4dd56 | 112 | |
a1163480319 | 0:bfe6cbe4dd56 | 113 | } |
a1163480319 | 0:bfe6cbe4dd56 | 114 | |
a1163480319 | 0:bfe6cbe4dd56 | 115 | |
a1163480319 | 0:bfe6cbe4dd56 | 116 | else |
a1163480319 | 0:bfe6cbe4dd56 | 117 | { |
a1163480319 | 0:bfe6cbe4dd56 | 118 | buzzer.period(1/frequency); // set PWM period |
a1163480319 | 0:bfe6cbe4dd56 | 119 | buzzer=0; // set duty cycle |
a1163480319 | 0:bfe6cbe4dd56 | 120 | } |
a1163480319 | 0:bfe6cbe4dd56 | 121 | |
a1163480319 | 0:bfe6cbe4dd56 | 122 | |
a1163480319 | 0:bfe6cbe4dd56 | 123 | |
a1163480319 | 0:bfe6cbe4dd56 | 124 | |
a1163480319 | 0:bfe6cbe4dd56 | 125 | switch(direction)//button case when pressed |
a1163480319 | 0:bfe6cbe4dd56 | 126 | { |
a1163480319 | 0:bfe6cbe4dd56 | 127 | case UP://first pressed |
a1163480319 | 0:bfe6cbe4dd56 | 128 | direction = 0; |
a1163480319 | 0:bfe6cbe4dd56 | 129 | lcd.printString(buffer7,4,0); |
a1163480319 | 0:bfe6cbe4dd56 | 130 | lcd.printString(buffer1,3,2); |
a1163480319 | 0:bfe6cbe4dd56 | 131 | lcd.printString(buffer2,3,3); |
a1163480319 | 0:bfe6cbe4dd56 | 132 | lcd.printString(buffer3,3,4); |
a1163480319 | 0:bfe6cbe4dd56 | 133 | break; |
a1163480319 | 0:bfe6cbe4dd56 | 134 | |
a1163480319 | 0:bfe6cbe4dd56 | 135 | case DOWN://second pressed |
a1163480319 | 0:bfe6cbe4dd56 | 136 | direction = 1; |
a1163480319 | 0:bfe6cbe4dd56 | 137 | lcd.printString(buffer8,13,0); |
a1163480319 | 0:bfe6cbe4dd56 | 138 | lcd.printString(buffer4,3,2); |
a1163480319 | 0:bfe6cbe4dd56 | 139 | lcd.printString(buffer5,3,3); |
a1163480319 | 0:bfe6cbe4dd56 | 140 | lcd.printString(buffer6,3,4); |
a1163480319 | 0:bfe6cbe4dd56 | 141 | break; |
a1163480319 | 0:bfe6cbe4dd56 | 142 | case MID://third presssed |
a1163480319 | 0:bfe6cbe4dd56 | 143 | direction = 2; |
a1163480319 | 0:bfe6cbe4dd56 | 144 | |
a1163480319 | 0:bfe6cbe4dd56 | 145 | // x0,y0,x1,y1,type 0-white,1-black,2-dotted |
a1163480319 | 0:bfe6cbe4dd56 | 146 | lcd.drawLine(0,23,83,23,1); |
a1163480319 | 0:bfe6cbe4dd56 | 147 | |
a1163480319 | 0:bfe6cbe4dd56 | 148 | lcd.drawLine(41,0,41,48,1); |
a1163480319 | 0:bfe6cbe4dd56 | 149 | |
a1163480319 | 0:bfe6cbe4dd56 | 150 | lcd.drawLine(64,0,17,47,1); |
a1163480319 | 0:bfe6cbe4dd56 | 151 | |
a1163480319 | 0:bfe6cbe4dd56 | 152 | lcd.drawLine(18,0,65,47,1); |
a1163480319 | 0:bfe6cbe4dd56 | 153 | |
a1163480319 | 0:bfe6cbe4dd56 | 154 | lcd.drawCircle(41,23,13,0); // x,y,radius,black fill |
a1163480319 | 0:bfe6cbe4dd56 | 155 | lcd.drawCircle(41,23,25,0); |
a1163480319 | 0:bfe6cbe4dd56 | 156 | |
a1163480319 | 0:bfe6cbe4dd56 | 157 | |
a1163480319 | 0:bfe6cbe4dd56 | 158 | if (angleX<0||angleY>0){ //move right and forward |
a1163480319 | 0:bfe6cbe4dd56 | 159 | |
a1163480319 | 0:bfe6cbe4dd56 | 160 | |
a1163480319 | 0:bfe6cbe4dd56 | 161 | lcd.setPixel(41-angleX/3.6,23+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 162 | lcd.setPixel(41-angleX/3.6,22+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 163 | lcd.setPixel(41-angleX/3.6,24+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 164 | lcd.setPixel(42-angleX/3.6,23+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 165 | lcd.setPixel(40-angleX/3.6,23+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 166 | |
a1163480319 | 0:bfe6cbe4dd56 | 167 | } |
a1163480319 | 0:bfe6cbe4dd56 | 168 | lcd.refresh(); |
a1163480319 | 0:bfe6cbe4dd56 | 169 | |
a1163480319 | 0:bfe6cbe4dd56 | 170 | |
a1163480319 | 0:bfe6cbe4dd56 | 171 | |
a1163480319 | 0:bfe6cbe4dd56 | 172 | |
a1163480319 | 0:bfe6cbe4dd56 | 173 | if (angleX>0||angleY>0) //move left and backward |
a1163480319 | 0:bfe6cbe4dd56 | 174 | { |
a1163480319 | 0:bfe6cbe4dd56 | 175 | lcd.setPixel(41-angleX/3.6,23+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 176 | lcd.setPixel(41-angleX/3.6,22+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 177 | lcd.setPixel(41-angleX/3.6,24+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 178 | lcd.setPixel(40-angleX/3.6,23+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 179 | lcd.setPixel(42-angleX/3.6,23+angleY/3.6); |
a1163480319 | 0:bfe6cbe4dd56 | 180 | |
a1163480319 | 0:bfe6cbe4dd56 | 181 | lcd.refresh(); |
a1163480319 | 0:bfe6cbe4dd56 | 182 | |
a1163480319 | 0:bfe6cbe4dd56 | 183 | } |
a1163480319 | 0:bfe6cbe4dd56 | 184 | |
a1163480319 | 0:bfe6cbe4dd56 | 185 | |
a1163480319 | 0:bfe6cbe4dd56 | 186 | lcd.printString(buffer9,35,0); |
a1163480319 | 0:bfe6cbe4dd56 | 187 | lcd.printString(buffer10,75,2); |
a1163480319 | 0:bfe6cbe4dd56 | 188 | |
a1163480319 | 0:bfe6cbe4dd56 | 189 | break; |
a1163480319 | 0:bfe6cbe4dd56 | 190 | |
a1163480319 | 0:bfe6cbe4dd56 | 191 | |
a1163480319 | 0:bfe6cbe4dd56 | 192 | default: |
a1163480319 | 0:bfe6cbe4dd56 | 193 | |
a1163480319 | 0:bfe6cbe4dd56 | 194 | break; |
a1163480319 | 0:bfe6cbe4dd56 | 195 | |
a1163480319 | 0:bfe6cbe4dd56 | 196 | } |
a1163480319 | 0:bfe6cbe4dd56 | 197 | |
a1163480319 | 2:a659621a83b0 | 198 | leds=1; |
a1163480319 | 0:bfe6cbe4dd56 | 199 | |
a1163480319 | 0:bfe6cbe4dd56 | 200 | ain = pot.read(); /* Read analog value (output will be any value between 0 and 1 */ |
a1163480319 | 0:bfe6cbe4dd56 | 201 | |
a1163480319 | 0:bfe6cbe4dd56 | 202 | |
a1163480319 | 0:bfe6cbe4dd56 | 203 | lcd.setBrightness(ain); //tune the screen brightness |
a1163480319 | 0:bfe6cbe4dd56 | 204 | lcd.refresh(); |
a1163480319 | 0:bfe6cbe4dd56 | 205 | |
a1163480319 | 0:bfe6cbe4dd56 | 206 | |
a1163480319 | 0:bfe6cbe4dd56 | 207 | } |
a1163480319 | 0:bfe6cbe4dd56 | 208 | } |
a1163480319 | 0:bfe6cbe4dd56 | 209 |