├── .gitignore ├── README.md ├── materials └── arduino mega port registers.jpeg ├── sources ├── accelerometer_adaptation_mega_final │ └── accelerometer_adaptation_mega_final.ino ├── accelerometer_adaptation_uno │ └── accelerometer_adaptation_uno.ino ├── accelerometer_midi.ino ├── accelerometer_midi │ └── accelerometer_midi.ino ├── accelerometer_read_adaptation_uno.ino ├── accelerometer_read_adaptation_uno │ └── accelerometer_read_adaptation_uno.ino ├── accelerometer_read_mega.ino ├── accelerometer_read_uno.ino ├── accelerometer_xbee_midi_mega.ino ├── accelerometer_xbee_midi_mega │ └── accelerometer_xbee_midi_mega.ino ├── coordinator_receive_softserial_uno.ino ├── coordinator_receive_softserial_uno │ └── coordinator_receive_softserial_uno.ino ├── encoder_controller │ ├── Release │ │ ├── encoder_controller.d │ │ ├── encoder_controller.eep │ │ ├── encoder_controller.elf │ │ ├── encoder_controller.hex │ │ ├── encoder_controller.lss │ │ ├── makefile │ │ ├── objects.mk │ │ ├── sources.mk │ │ └── subdir.mk │ ├── encoder_controller.cpp │ └── encoder_controller.h ├── knock_sensor_uno │ └── knock_sensor_uno.ino ├── multiplexing_analog_potentiometers_mega │ └── multiplexing_analog_potentiometers_mega.ino ├── overdrive_send_midi_via_xbee_uno.ino ├── rotary_encoder_test_uno.ino ├── rotary_encoder_test_uno │ └── rotary_encoder_test_uno.ino ├── rotary_encoders_with_lib_mega │ └── rotary_encoders_with_lib_mega.ino ├── rotary_pinchangeint_uno │ └── rotary_pinchangeint_uno.ino ├── router_send_test_signal_mega.ino ├── router_send_test_signal_uno.ino └── serial1_read_mega │ └── serial1_read_mega.ino └── sources_examples └── accelerometer_adaptation_uno └── accelerometer_adaptation_uno.ino /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | 9 | # Compiled Static libraries 10 | *.lai 11 | *.la 12 | *.a 13 | .DS_Store 14 | .DS_Store? 15 | ._* 16 | .Spotlight-V100 17 | .Trashes 18 | Icon? 19 | ehthumbs.db 20 | Thumbs.db 21 | *.pydevproject 22 | .project 23 | .metadata 24 | bin/** 25 | tmp/** 26 | tmp/**/* 27 | *.tmp 28 | *.bak 29 | *.swp 30 | *~.nib 31 | local.properties 32 | .classpath 33 | .settings/ 34 | .loadpath 35 | 36 | # External tool builders 37 | .externalToolBuilders/ 38 | 39 | # Locally stored "Eclipse launch configurations" 40 | *.launch 41 | 42 | # CDT-specific 43 | .cproject 44 | 45 | # PDT-specific 46 | .buildpath 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Arduino-Playground 2 | ================== 3 | 4 | Project for all files regarding Arduino -------------------------------------------------------------------------------- /materials/arduino mega port registers.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wzin/Arduino-Playground/efdbc3422c215d862269c7e7fba6ee9a61119f9d/materials/arduino mega port registers.jpeg -------------------------------------------------------------------------------- /sources/accelerometer_adaptation_mega_final/accelerometer_adaptation_mega_final.ino: -------------------------------------------------------------------------------- 1 | #ifndef AcceleroMMA7361_h 2 | #define AcceleroMMA7361_h 3 | #include 4 | 5 | class AcceleroMMA7361 6 | { 7 | public: 8 | AcceleroMMA7361(); 9 | void begin(); 10 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 11 | gSelectPin, int xPin, int yPin, int zPin); 12 | int getXRaw(); 13 | int getYRaw(); 14 | int getZRaw(); 15 | int getXVolt(); 16 | int getYVolt(); 17 | int getZVolt(); 18 | int getXAccel(); 19 | int getYAccel(); 20 | int getZAccel(); 21 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 22 | int getTotalVector(); 23 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 24 | void calibrate(); // only to be executed when Z-axis is oriented to 25 | 26 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 27 | void setARefVoltage(double _refV); 28 | void setAveraging(int avg); 29 | int getOrientation(); 30 | void setSensitivity(boolean sensi); 31 | void sleep(); 32 | void wake(); 33 | 34 | private: 35 | int _mapMMA7361V(int value); 36 | int _mapMMA7361G(int value); 37 | int _sleepPin; 38 | int _selfTestPin; 39 | int _zeroGPin; 40 | int _gSelectPin; 41 | int _xPin; 42 | int _yPin; 43 | int _zPin; 44 | int _offSets[3]; 45 | int _polarities[3]; 46 | double _refVoltage; 47 | int _average; 48 | boolean _sleep; 49 | boolean _sensi; 50 | }; 51 | #endif 52 | 53 | AcceleroMMA7361::AcceleroMMA7361() 54 | { 55 | } 56 | 57 | void AcceleroMMA7361::begin() 58 | { 59 | begin(13, 12, 11, 10, A0, A1, A2); 60 | } 61 | 62 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 63 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 64 | { 65 | pinMode(sleepPin, OUTPUT); 66 | pinMode(selfTestPin, OUTPUT); 67 | pinMode(zeroGPin, INPUT); 68 | pinMode(gSelectPin, INPUT); 69 | pinMode(xPin, INPUT); 70 | pinMode(yPin, INPUT); 71 | pinMode(zPin, INPUT); 72 | digitalWrite(sleepPin,HIGH); 73 | digitalWrite(selfTestPin,LOW); 74 | _sleepPin = sleepPin; 75 | _selfTestPin = selfTestPin; 76 | _zeroGPin = zeroGPin; 77 | _gSelectPin = gSelectPin; 78 | _xPin = xPin; 79 | _yPin = yPin; 80 | _zPin = zPin; 81 | _sleep = false; 82 | setOffSets(0,0,0); 83 | setARefVoltage(5); 84 | setAveraging(10); 85 | setSensitivity(HIGH); 86 | } 87 | 88 | /// 89 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 90 | 91 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 92 | /// Offsets are added to the raw datafunctions 93 | /// 94 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 95 | { 96 | if (_refVoltage==3.3) 97 | { 98 | _offSets[0]= map(xOffSet,0,3300,0,1024); 99 | _offSets[1]= map(yOffSet,0,3300,0,1024); 100 | _offSets[2]= map(zOffSet,0,3300,0,1024); 101 | } 102 | else 103 | { 104 | _offSets[0]= map(xOffSet,0,5000,0,1024); 105 | _offSets[1]= map(yOffSet,0,5000,0,1024); 106 | _offSets[2]= map(zOffSet,0,5000,0,1024); 107 | } 108 | } 109 | 110 | /// 111 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 112 | 113 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 114 | 115 | /// 3.3 V VCC pin. This increases accuracy 116 | /// 117 | void AcceleroMMA7361::setARefVoltage(double refV) 118 | { 119 | _refVoltage = refV; 120 | if (refV == 3.3) 121 | { 122 | analogReference(EXTERNAL); 123 | } 124 | } 125 | 126 | /// 127 | /// setAveraging(int avg): Sets how many samples have to be averaged 128 | 129 | /// 130 | void AcceleroMMA7361::setAveraging(int avg) 131 | { 132 | _average = avg; 133 | } 134 | 135 | /// 136 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 137 | 138 | /// 139 | void AcceleroMMA7361::setSensitivity(boolean sensi) 140 | { 141 | _sensi = sensi; 142 | digitalWrite(_gSelectPin, !sensi); 143 | } 144 | 145 | /// 146 | /// sleep lets the device sleep (when device is sleeping already this 147 | 148 | /// 149 | void AcceleroMMA7361::sleep() 150 | { 151 | if (!_sleep) 152 | { 153 | digitalWrite(_sleepPin, LOW); 154 | _sleep = true; 155 | } 156 | } 157 | 158 | /// 159 | /// wake enables the device after sleep (when device is not sleeping 160 | 161 | 162 | /// 163 | void AcceleroMMA7361::wake() 164 | { 165 | if (_sleep == true) 166 | { 167 | digitalWrite(_sleepPin, HIGH); 168 | _sleep = false; 169 | delay(2); 170 | } 171 | } 172 | 173 | /// 174 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 175 | 176 | /// 177 | int AcceleroMMA7361::getXRaw() 178 | { 179 | return analogRead(_xPin)+_offSets[0]+2; 180 | } 181 | 182 | /// 183 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 184 | 185 | /// 186 | int AcceleroMMA7361::getYRaw() 187 | { 188 | return analogRead(_yPin)+_offSets[1]+2; 189 | } 190 | 191 | /// 192 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 193 | 194 | /// 195 | int AcceleroMMA7361::getZRaw() 196 | { 197 | return analogRead(_zPin)+_offSets[2]; 198 | } 199 | 200 | /// 201 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 202 | 203 | /// 204 | int AcceleroMMA7361::getXVolt() 205 | { 206 | return _mapMMA7361V(getXRaw()); 207 | } 208 | 209 | /// 210 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 211 | 212 | /// 213 | int AcceleroMMA7361::getYVolt() 214 | { 215 | return _mapMMA7361V(getYRaw()); 216 | } 217 | 218 | /// 219 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 220 | 221 | /// 222 | int AcceleroMMA7361::getZVolt() 223 | { 224 | return _mapMMA7361V(getZRaw()); 225 | } 226 | 227 | /// 228 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 229 | /// 230 | int AcceleroMMA7361::getXAccel() 231 | { 232 | int sum = 0; 233 | for (int i = 0;i<_average;i++) 234 | { 235 | sum = sum + _mapMMA7361G(getXRaw()); 236 | } 237 | return sum/_average; 238 | } 239 | 240 | /// 241 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 242 | /// 243 | int AcceleroMMA7361::getYAccel() 244 | { 245 | int sum = 0; 246 | for (int i = 0;i<_average;i++) 247 | { 248 | sum = sum + _mapMMA7361G(getYRaw()); 249 | } 250 | return sum/_average; 251 | } 252 | 253 | /// 254 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 255 | /// 256 | int AcceleroMMA7361::getZAccel() 257 | { 258 | int sum = 0; 259 | for (int i = 0;i<_average;i++) 260 | { 261 | sum = sum + _mapMMA7361G(getZRaw()); 262 | } 263 | return sum/_average; 264 | } 265 | 266 | /// 267 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 268 | 269 | /// 270 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 271 | { 272 | int sum[3]; 273 | sum[0] = 0; 274 | sum[1] = 0; 275 | sum[2] = 0; 276 | for (int i = 0;i<_average;i++) 277 | { 278 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 279 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 280 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 281 | } 282 | *_XAxis = sum[0]/_average; 283 | *_YAxis = sum[1]/_average; 284 | *_ZAxis = sum[2]/_average; 285 | } 286 | 287 | /// 288 | /// mapMMA7361V: calculates and returns the voltage value derived from 289 | 290 | /// 291 | int AcceleroMMA7361::_mapMMA7361V(int value) 292 | { 293 | if (_refVoltage==3.3) 294 | { 295 | return map(value,0,1024,0,3300); 296 | } 297 | else 298 | { 299 | return map(value,0,1024,0,5000); 300 | } 301 | } 302 | 303 | /// 304 | /// mapMMA7361G: calculates and returns the accelerometer value in 305 | 306 | 307 | /// 308 | int AcceleroMMA7361::_mapMMA7361G(int value) 309 | { 310 | if(_sensi == false) 311 | { 312 | if (_refVoltage==3.3) 313 | { 314 | return map(value,0,1024,-825,800); 315 | } 316 | else 317 | { 318 | return map(value,0,1024,-800,1600); 319 | } 320 | } 321 | else 322 | { 323 | if (_refVoltage==3.3) 324 | { 325 | return map(value,0,1024,-206,206); 326 | } 327 | else 328 | { 329 | return map(value,0,1024,-260,419); 330 | } 331 | } 332 | } 333 | 334 | /// 335 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 336 | 337 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 338 | 339 | /// 340 | void AcceleroMMA7361::calibrate() 341 | { 342 | Serial.println(getOrientation()); 343 | Serial.print("\nCalibrating MMA7361011"); 344 | double var = 5000; 345 | double sumX = 0; 346 | double sumY = 0; 347 | double sumZ = 0; 348 | for (int i = 0;i 378 | /// getOrientation returns which axis perpendicular with the earths 379 | 380 | /// negative depending on which side of the axis is pointing downwards 381 | /// 382 | int AcceleroMMA7361::getOrientation() 383 | { 384 | int gemiddelde = 10; 385 | int x = 0; 386 | int y = 0; 387 | int z = 0; 388 | int xAbs = 0; 389 | int yAbs = 0; 390 | int zAbs = 0; 391 | for(int i = 0; i0) 407 | { 408 | return 1; 409 | } 410 | return -1; 411 | } 412 | if (yAbs0) 415 | { 416 | return 2; 417 | } 418 | return -2; 419 | } 420 | if (zAbs0) 423 | { 424 | return 3; 425 | } 426 | return -3; 427 | } 428 | return 0; 429 | } 430 | /// 431 | /// getTotalVector returns the magnitude of the total acceleration 432 | 433 | /// 434 | int AcceleroMMA7361::getTotalVector() 435 | { 436 | return sqrt(square(_mapMMA7361G(getXRaw())) + 437 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 438 | } 439 | 440 | 441 | AcceleroMMA7361 accelero; 442 | 443 | int x; 444 | int y; 445 | int z; 446 | int i; 447 | int counter = 0; 448 | int current_coordinates[] = {0,0,0}; 449 | int scale_x[] = {-50,50}; // needs tuning 450 | int scale_y[] = {-50,50}; // needs tuning 451 | int scale_z[] = {-50,50}; // needs tuning 452 | int previous_scale_x[] = {-0,0}; //tuning needed 453 | int previous_scale_y[] = {-0,0}; //tuning needed 454 | int previous_scale_z[] = {-0,0}; //tuning needed 455 | 456 | void setup() 457 | { 458 | Serial.begin(9600); 459 | Serial2.begin(9600); 460 | accelero.begin(13, 12, 11, 10, A0, A1, A2); // (int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 461 | // WAŻNE: podłączyć 3V3 do AREF'u!!!! 462 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 463 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 464 | accelero.calibrate(); 465 | 466 | } 467 | 468 | void loop() 469 | { 470 | //Reading the accelerometer coordinates values 471 | current_coordinates[0] = accelero.getXAccel(); 472 | current_coordinates[1] = accelero.getYAccel(); 473 | current_coordinates[2] = accelero.getZAccel(); 474 | 475 | //Normalization 476 | for(i=0; i++ ; i < 3) { 477 | current_coordinates[i] = normalize(current_coordinates[i]); 478 | } 479 | 480 | //Adapting the scale for mapping 481 | 482 | scale_x[0] = min(current_coordinates[0],previous_scale_x[0]); 483 | scale_y[0] = min(current_coordinates[1],previous_scale_x[0]); 484 | scale_z[0] = min(current_coordinates[2],previous_scale_x[0]); 485 | scale_x[1] = max(current_coordinates[0],previous_scale_x[1]); 486 | scale_y[1] = max(current_coordinates[1],previous_scale_x[1]); 487 | scale_z[1] = max(current_coordinates[2],previous_scale_x[1]); 488 | 489 | //Mapowanie 490 | x = map(current_coordinates[0],scale_x[0],scale_x[1],0,127); 491 | y = map(current_coordinates[1],scale_y[0],scale_y[1],0,127); 492 | z = map(current_coordinates[2],scale_z[0],scale_z[1],0,127); 493 | 494 | //Zapisanie stanu 495 | //min 496 | previous_scale_x[0] = scale_x[0] ; 497 | previous_scale_y[0] = scale_y[0] ; 498 | previous_scale_z[0] = scale_z[0] ; 499 | //max 500 | previous_scale_x[1] = scale_x[1] ; 501 | previous_scale_y[1] = scale_y[1] ; 502 | previous_scale_z[1] = scale_z[1] ; 503 | 504 | if(counter == 10 ) { //set period of adaptation 505 | //min 506 | previous_scale_x[0] = previous_scale_x[0] +1 ; 507 | previous_scale_y[0] = previous_scale_y[0] +1 ; 508 | previous_scale_z[0] = previous_scale_z[0] +1 ; 509 | //max 510 | previous_scale_x[1] = previous_scale_x[1] -2 ; 511 | previous_scale_y[1] = previous_scale_y[1] -2 ; 512 | previous_scale_z[1] = previous_scale_z[1] -2 ; 513 | //min 514 | scale_x[0] = scale_x[0] +1 ; 515 | scale_y[0] = scale_y[0] +1 ; 516 | scale_z[0] = scale_z[0] +1 ; 517 | //max 518 | scale_x[1] = scale_x[1] -2 ; 519 | scale_y[1] = scale_y[1] -2 ; 520 | scale_z[1] = scale_z[1] -2 ; 521 | // Serial.println("Adaptation commited"); 522 | counter = 0; 523 | } 524 | /* 525 | Serial.println(); 526 | Serial.print("X: "); 527 | Serial.print(scale_x[0]); 528 | Serial.print("->"); 529 | Serial.print(scale_x[1]); 530 | Serial.print("/previous:"); 531 | Serial.print(previous_scale_x[0]); 532 | Serial.print("->"); 533 | Serial.print(previous_scale_x[1]); 534 | Serial.println(); 535 | Serial.print("Y: "); 536 | Serial.print(scale_y[0]); 537 | Serial.print("->"); 538 | Serial.print(scale_y[1]); 539 | Serial.print("/previous:"); 540 | Serial.print(previous_scale_y[0]); 541 | Serial.print("->"); 542 | Serial.print(previous_scale_y[1]); 543 | Serial.println(); 544 | Serial.print("Z: "); 545 | Serial.print(scale_z[0]); 546 | Serial.print("->"); 547 | Serial.print(scale_z[1]); 548 | Serial.print("/previous:"); 549 | Serial.print(previous_scale_z[0]); 550 | Serial.print("->"); 551 | Serial.print(previous_scale_z[1]); 552 | Serial.println(); 553 | 554 | Serial.print("Current -> X: "); 555 | Serial.print(current_coordinates[0]); 556 | Serial.print(",Y: "); 557 | Serial.print(current_coordinates[1]); 558 | Serial.print(",Z: "); 559 | Serial.println(current_coordinates[2]); 560 | Serial.print("Our x,y,z ->"); 561 | Serial.print(x); 562 | Serial.print(','); 563 | Serial.print(y); 564 | Serial.print(','); 565 | Serial.print(z); 566 | Serial.println(); 567 | Serial.println("-------------"); 568 | 569 | delay(1000); 570 | counter++; 571 | */ 572 | /* 573 | Serial.print("\nx: "); 574 | Serial.print(x); 575 | Serial.print(" \ty: "); 576 | Serial.print(y); 577 | Serial.print(" \tz: "); 578 | Serial.print(z); 579 | Serial.print("\tG*10^-2"); 580 | delay(500); //make it readable 581 | */ 582 | Serial2.write(0xB0); // MIDI control change; channel 3 583 | Serial2.write(0x2A); // MIDI controller #1 584 | Serial2.write(x); // MIDI controller value of 127 585 | 586 | Serial2.write(0xB0); // MIDI control change; channel 3 587 | Serial2.write(0x2B); // MIDI controller #1 588 | Serial2.write(y); // MIDI controller value of 127 589 | 590 | Serial2.write(0xB0); // MIDI control change; channel 3 591 | Serial2.write(0x2C); // MIDI controller #1 592 | Serial2.write(z); // MIDI controller value of 127 593 | delay(70); 594 | } 595 | 596 | 597 | 598 | 599 | 600 | 601 | int normalize(int newreading) { 602 | if (newreading > 180){ 603 | return 180; 604 | } else { 605 | return newreading; 606 | } 607 | if (newreading < -180){ 608 | return -180 ; 609 | } else { 610 | return newreading; 611 | } 612 | } 613 | 614 | 615 | 616 | 617 | -------------------------------------------------------------------------------- /sources/accelerometer_adaptation_uno/accelerometer_adaptation_uno.ino: -------------------------------------------------------------------------------- 1 | #ifndef AcceleroMMA7361_h 2 | #define AcceleroMMA7361_h 3 | #include 4 | 5 | class AcceleroMMA7361 6 | { 7 | public: 8 | AcceleroMMA7361(); 9 | void begin(); 10 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 11 | gSelectPin, int xPin, int yPin, int zPin); 12 | int getXRaw(); 13 | int getYRaw(); 14 | int getZRaw(); 15 | int getXVolt(); 16 | int getYVolt(); 17 | int getZVolt(); 18 | int getXAccel(); 19 | int getYAccel(); 20 | int getZAccel(); 21 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 22 | int getTotalVector(); 23 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 24 | void calibrate(); // only to be executed when Z-axis is oriented to 25 | 26 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 27 | void setARefVoltage(double _refV); 28 | void setAveraging(int avg); 29 | int getOrientation(); 30 | void setSensitivity(boolean sensi); 31 | void sleep(); 32 | void wake(); 33 | 34 | private: 35 | int _mapMMA7361V(int value); 36 | int _mapMMA7361G(int value); 37 | int _sleepPin; 38 | int _selfTestPin; 39 | int _zeroGPin; 40 | int _gSelectPin; 41 | int _xPin; 42 | int _yPin; 43 | int _zPin; 44 | int _offSets[3]; 45 | int _polarities[3]; 46 | double _refVoltage; 47 | int _average; 48 | boolean _sleep; 49 | boolean _sensi; 50 | }; 51 | #endif 52 | 53 | AcceleroMMA7361::AcceleroMMA7361() 54 | { 55 | } 56 | 57 | void AcceleroMMA7361::begin() 58 | { 59 | begin(13, 12, 11, 10, A0, A1, A2); 60 | } 61 | 62 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 63 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 64 | { 65 | pinMode(sleepPin, OUTPUT); 66 | pinMode(selfTestPin, OUTPUT); 67 | pinMode(zeroGPin, INPUT); 68 | pinMode(gSelectPin, INPUT); 69 | pinMode(xPin, INPUT); 70 | pinMode(yPin, INPUT); 71 | pinMode(zPin, INPUT); 72 | digitalWrite(sleepPin,HIGH); 73 | digitalWrite(selfTestPin,LOW); 74 | _sleepPin = sleepPin; 75 | _selfTestPin = selfTestPin; 76 | _zeroGPin = zeroGPin; 77 | _gSelectPin = gSelectPin; 78 | _xPin = xPin; 79 | _yPin = yPin; 80 | _zPin = zPin; 81 | _sleep = false; 82 | setOffSets(0,0,0); 83 | setARefVoltage(5); 84 | setAveraging(10); 85 | setSensitivity(HIGH); 86 | } 87 | 88 | /// 89 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 90 | 91 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 92 | /// Offsets are added to the raw datafunctions 93 | /// 94 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 95 | { 96 | if (_refVoltage==3.3) 97 | { 98 | _offSets[0]= map(xOffSet,0,3300,0,1024); 99 | _offSets[1]= map(yOffSet,0,3300,0,1024); 100 | _offSets[2]= map(zOffSet,0,3300,0,1024); 101 | } 102 | else 103 | { 104 | _offSets[0]= map(xOffSet,0,5000,0,1024); 105 | _offSets[1]= map(yOffSet,0,5000,0,1024); 106 | _offSets[2]= map(zOffSet,0,5000,0,1024); 107 | } 108 | } 109 | 110 | /// 111 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 112 | 113 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 114 | 115 | /// 3.3 V VCC pin. This increases accuracy 116 | /// 117 | void AcceleroMMA7361::setARefVoltage(double refV) 118 | { 119 | _refVoltage = refV; 120 | if (refV == 3.3) 121 | { 122 | analogReference(EXTERNAL); 123 | } 124 | } 125 | 126 | /// 127 | /// setAveraging(int avg): Sets how many samples have to be averaged 128 | 129 | /// 130 | void AcceleroMMA7361::setAveraging(int avg) 131 | { 132 | _average = avg; 133 | } 134 | 135 | /// 136 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 137 | 138 | /// 139 | void AcceleroMMA7361::setSensitivity(boolean sensi) 140 | { 141 | _sensi = sensi; 142 | digitalWrite(_gSelectPin, !sensi); 143 | } 144 | 145 | /// 146 | /// sleep lets the device sleep (when device is sleeping already this 147 | 148 | /// 149 | void AcceleroMMA7361::sleep() 150 | { 151 | if (!_sleep) 152 | { 153 | digitalWrite(_sleepPin, LOW); 154 | _sleep = true; 155 | } 156 | } 157 | 158 | /// 159 | /// wake enables the device after sleep (when device is not sleeping 160 | 161 | 162 | /// 163 | void AcceleroMMA7361::wake() 164 | { 165 | if (_sleep == true) 166 | { 167 | digitalWrite(_sleepPin, HIGH); 168 | _sleep = false; 169 | delay(2); 170 | } 171 | } 172 | 173 | /// 174 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 175 | 176 | /// 177 | int AcceleroMMA7361::getXRaw() 178 | { 179 | return analogRead(_xPin)+_offSets[0]+2; 180 | } 181 | 182 | /// 183 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 184 | 185 | /// 186 | int AcceleroMMA7361::getYRaw() 187 | { 188 | return analogRead(_yPin)+_offSets[1]+2; 189 | } 190 | 191 | /// 192 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 193 | 194 | /// 195 | int AcceleroMMA7361::getZRaw() 196 | { 197 | return analogRead(_zPin)+_offSets[2]; 198 | } 199 | 200 | /// 201 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 202 | 203 | /// 204 | int AcceleroMMA7361::getXVolt() 205 | { 206 | return _mapMMA7361V(getXRaw()); 207 | } 208 | 209 | /// 210 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 211 | 212 | /// 213 | int AcceleroMMA7361::getYVolt() 214 | { 215 | return _mapMMA7361V(getYRaw()); 216 | } 217 | 218 | /// 219 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 220 | 221 | /// 222 | int AcceleroMMA7361::getZVolt() 223 | { 224 | return _mapMMA7361V(getZRaw()); 225 | } 226 | 227 | /// 228 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 229 | /// 230 | int AcceleroMMA7361::getXAccel() 231 | { 232 | int sum = 0; 233 | for (int i = 0;i<_average;i++) 234 | { 235 | sum = sum + _mapMMA7361G(getXRaw()); 236 | } 237 | return sum/_average; 238 | } 239 | 240 | /// 241 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 242 | /// 243 | int AcceleroMMA7361::getYAccel() 244 | { 245 | int sum = 0; 246 | for (int i = 0;i<_average;i++) 247 | { 248 | sum = sum + _mapMMA7361G(getYRaw()); 249 | } 250 | return sum/_average; 251 | } 252 | 253 | /// 254 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 255 | /// 256 | int AcceleroMMA7361::getZAccel() 257 | { 258 | int sum = 0; 259 | for (int i = 0;i<_average;i++) 260 | { 261 | sum = sum + _mapMMA7361G(getZRaw()); 262 | } 263 | return sum/_average; 264 | } 265 | 266 | /// 267 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 268 | 269 | /// 270 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 271 | { 272 | int sum[3]; 273 | sum[0] = 0; 274 | sum[1] = 0; 275 | sum[2] = 0; 276 | for (int i = 0;i<_average;i++) 277 | { 278 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 279 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 280 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 281 | } 282 | *_XAxis = sum[0]/_average; 283 | *_YAxis = sum[1]/_average; 284 | *_ZAxis = sum[2]/_average; 285 | } 286 | 287 | /// 288 | /// mapMMA7361V: calculates and returns the voltage value derived from 289 | 290 | /// 291 | int AcceleroMMA7361::_mapMMA7361V(int value) 292 | { 293 | if (_refVoltage==3.3) 294 | { 295 | return map(value,0,1024,0,3300); 296 | } 297 | else 298 | { 299 | return map(value,0,1024,0,5000); 300 | } 301 | } 302 | 303 | /// 304 | /// mapMMA7361G: calculates and returns the accelerometer value in 305 | 306 | 307 | /// 308 | int AcceleroMMA7361::_mapMMA7361G(int value) 309 | { 310 | if(_sensi == false) 311 | { 312 | if (_refVoltage==3.3) 313 | { 314 | return map(value,0,1024,-825,800); 315 | } 316 | else 317 | { 318 | return map(value,0,1024,-800,1600); 319 | } 320 | } 321 | else 322 | { 323 | if (_refVoltage==3.3) 324 | { 325 | return map(value,0,1024,-206,206); 326 | } 327 | else 328 | { 329 | return map(value,0,1024,-260,419); 330 | } 331 | } 332 | } 333 | 334 | /// 335 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 336 | 337 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 338 | 339 | /// 340 | void AcceleroMMA7361::calibrate() 341 | { 342 | Serial.println(getOrientation()); 343 | Serial.print("\nCalibrating MMA7361011"); 344 | double var = 5000; 345 | double sumX = 0; 346 | double sumY = 0; 347 | double sumZ = 0; 348 | for (int i = 0;i 378 | /// getOrientation returns which axis perpendicular with the earths 379 | 380 | /// negative depending on which side of the axis is pointing downwards 381 | /// 382 | int AcceleroMMA7361::getOrientation() 383 | { 384 | int gemiddelde = 10; 385 | int x = 0; 386 | int y = 0; 387 | int z = 0; 388 | int xAbs = 0; 389 | int yAbs = 0; 390 | int zAbs = 0; 391 | for(int i = 0; i0) 407 | { 408 | return 1; 409 | } 410 | return -1; 411 | } 412 | if (yAbs0) 415 | { 416 | return 2; 417 | } 418 | return -2; 419 | } 420 | if (zAbs0) 423 | { 424 | return 3; 425 | } 426 | return -3; 427 | } 428 | return 0; 429 | } 430 | /// 431 | /// getTotalVector returns the magnitude of the total acceleration 432 | 433 | /// 434 | int AcceleroMMA7361::getTotalVector() 435 | { 436 | return sqrt(square(_mapMMA7361G(getXRaw())) + 437 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 438 | } 439 | 440 | 441 | AcceleroMMA7361 accelero; 442 | 443 | int x; 444 | int y; 445 | int z; 446 | int i; 447 | int counter = 0; 448 | int current_coordinates[] = {0,0,0}; 449 | int scale_x[] = {-50,50}; // needs tuning 450 | int scale_y[] = {-50,50}; // needs tuning 451 | int scale_z[] = {-50,50}; // needs tuning 452 | int previous_scale_x[] = {-0,0}; //tuning needed 453 | int previous_scale_y[] = {-0,0}; //tuning needed 454 | int previous_scale_z[] = {-0,0}; //tuning needed 455 | 456 | void setup() 457 | { 458 | Serial.begin(9600); 459 | accelero.begin(13, 12, 11, 10, A0, A1, A2); // (int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 460 | // WAŻNE: podłączyć 3V3 do AREF'u!!!! 461 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 462 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 463 | accelero.calibrate(); 464 | 465 | } 466 | 467 | void loop() 468 | { 469 | //Reading the accelerometer coordinates values 470 | current_coordinates[0] = accelero.getXAccel(); 471 | current_coordinates[1] = accelero.getYAccel(); 472 | current_coordinates[2] = accelero.getZAccel(); 473 | 474 | //Normalization 475 | for(i=0; i++ ; i < 3) { 476 | current_coordinates[i] = normalize(current_coordinates[i]); 477 | } 478 | 479 | //Adapting the scale for mapping 480 | 481 | scale_x[0] = min(current_coordinates[0],previous_scale_x[0]); 482 | scale_y[0] = min(current_coordinates[1],previous_scale_x[0]); 483 | scale_z[0] = min(current_coordinates[2],previous_scale_x[0]); 484 | scale_x[1] = max(current_coordinates[0],previous_scale_x[1]); 485 | scale_y[1] = max(current_coordinates[1],previous_scale_x[1]); 486 | scale_z[1] = max(current_coordinates[2],previous_scale_x[1]); 487 | 488 | //Mapowanie 489 | x = map(current_coordinates[0],scale_x[0],scale_x[1],0,127); 490 | y = map(current_coordinates[1],scale_y[0],scale_y[1],0,127); 491 | z = map(current_coordinates[2],scale_z[0],scale_z[1],0,127); 492 | 493 | //Zapisanie stanu 494 | //min 495 | previous_scale_x[0] = scale_x[0] ; 496 | previous_scale_y[0] = scale_y[0] ; 497 | previous_scale_z[0] = scale_z[0] ; 498 | //max 499 | previous_scale_x[1] = scale_x[1] ; 500 | previous_scale_y[1] = scale_y[1] ; 501 | previous_scale_z[1] = scale_z[1] ; 502 | 503 | if(counter == 10 ) { //set period of adaptation 504 | //min 505 | previous_scale_x[0] = previous_scale_x[0] +1 ; 506 | previous_scale_y[0] = previous_scale_y[0] +1 ; 507 | previous_scale_z[0] = previous_scale_z[0] +1 ; 508 | //max 509 | previous_scale_x[1] = previous_scale_x[1] -2 ; 510 | previous_scale_y[1] = previous_scale_y[1] -2 ; 511 | previous_scale_z[1] = previous_scale_z[1] -2 ; 512 | //min 513 | scale_x[0] = scale_x[0] +1 ; 514 | scale_y[0] = scale_y[0] +1 ; 515 | scale_z[0] = scale_z[0] +1 ; 516 | //max 517 | scale_x[1] = scale_x[1] -2 ; 518 | scale_y[1] = scale_y[1] -2 ; 519 | scale_z[1] = scale_z[1] -2 ; 520 | // Serial.println("Adaptation commited"); 521 | counter = 0; 522 | } 523 | /* 524 | Serial.println(); 525 | Serial.print("X: "); 526 | Serial.print(scale_x[0]); 527 | Serial.print("->"); 528 | Serial.print(scale_x[1]); 529 | Serial.print("/previous:"); 530 | Serial.print(previous_scale_x[0]); 531 | Serial.print("->"); 532 | Serial.print(previous_scale_x[1]); 533 | Serial.println(); 534 | Serial.print("Y: "); 535 | Serial.print(scale_y[0]); 536 | Serial.print("->"); 537 | Serial.print(scale_y[1]); 538 | Serial.print("/previous:"); 539 | Serial.print(previous_scale_y[0]); 540 | Serial.print("->"); 541 | Serial.print(previous_scale_y[1]); 542 | Serial.println(); 543 | Serial.print("Z: "); 544 | Serial.print(scale_z[0]); 545 | Serial.print("->"); 546 | Serial.print(scale_z[1]); 547 | Serial.print("/previous:"); 548 | Serial.print(previous_scale_z[0]); 549 | Serial.print("->"); 550 | Serial.print(previous_scale_z[1]); 551 | Serial.println(); 552 | 553 | Serial.print("Current -> X: "); 554 | Serial.print(current_coordinates[0]); 555 | Serial.print(",Y: "); 556 | Serial.print(current_coordinates[1]); 557 | Serial.print(",Z: "); 558 | Serial.println(current_coordinates[2]); 559 | Serial.print("Our x,y,z ->"); 560 | Serial.print(x); 561 | Serial.print(','); 562 | Serial.print(y); 563 | Serial.print(','); 564 | Serial.print(z); 565 | Serial.println(); 566 | Serial.println("-------------"); 567 | 568 | delay(1000); 569 | counter++; 570 | */ 571 | /* 572 | Serial.print("\nx: "); 573 | Serial.print(x); 574 | Serial.print(" \ty: "); 575 | Serial.print(y); 576 | Serial.print(" \tz: "); 577 | Serial.print(z); 578 | Serial.print("\tG*10^-2"); 579 | delay(500); //make it readable 580 | */ 581 | Serial1.write(0xB0); // MIDI control change; channel 3 582 | Serial1.write(0x2A); // MIDI controller #1 583 | Serial1.write(x); // MIDI controller value of 127 584 | 585 | Serial1.write(0xB0); // MIDI control change; channel 3 586 | Serial1.write(0x2B); // MIDI controller #1 587 | Serial1.write(y); // MIDI controller value of 127 588 | 589 | Serial1.write(0xB0); // MIDI control change; channel 3 590 | Serial1.write(0x2C); // MIDI controller #1 591 | Serial1.write(z); // MIDI controller value of 127 592 | delay(10); 593 | } 594 | 595 | 596 | 597 | 598 | 599 | 600 | int normalize(int newreading) { 601 | if (newreading > 180){ 602 | return 180; 603 | } else { 604 | return newreading; 605 | } 606 | if (newreading < -180){ 607 | return -180 ; 608 | } else { 609 | return newreading; 610 | } 611 | } 612 | 613 | 614 | 615 | 616 | -------------------------------------------------------------------------------- /sources/accelerometer_midi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #define MIDI_BAUDRATE 9600 3 | #define USE_SERIAL_PORT Serial1 4 | #define COMPILE_MIDI_OUT 1 5 | 6 | /**************************************************************************************************************** 7 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 8 | accelerometer. * 9 | * Copyright 2011 Jef Neefs (neefs@gmail.com) and Jeroen Doggen 10 | (jeroendoggen@gmail.com) * 11 | * DATASHEET: http://www.sparkfun.com/datasheets/Components/General/MMA7361L.pdf 12 | * 13 | **************************************************************************************************************** 14 | * Version History: 15 | * 16 | * Version 0.1: -get raw values 17 | * 18 | * Version 0.2: -get voltages and G forces 19 | * 20 | * Version 0.3: -removed begin parameters offset 21 | * 22 | * -added public function setOffSets(int,int,int) 23 | * 24 | * -added a private variable _offSets[3] containing the 25 | offset on each axis * 26 | * -changed long and double return values of private and 27 | public functions to int * 28 | * Version 0.4: -added calibrate 29 | * 30 | * Version 0.5: -added setARefVoltage 31 | * 32 | * -added setAveraging 33 | * 34 | * -added a default begin function 35 | * 36 | * Version 0.6: -added getAccelXYZ to get all axis in one call 37 | * 38 | * -added getTotalVector returns the magnitude of the 39 | total vector as an integer * 40 | * -added getOrientation returns which axis 41 | perpendicular with the earths surface x=1,y=2,z=3 * 42 | * is positive or negative depending on which side of 43 | the axis is pointing downwards * 44 | * Version 0.7: -added setSensitivity 45 | * 46 | * -added sleep & wake 47 | * 48 | * Version 0.8: -converted to Arduino 1.0 library 49 | * 50 | * -changed license to LGPL 51 | * 52 | * Roadmap: 53 | * 54 | * Version 0.x: auto zero calibration 55 | http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf 56 | * 57 | * Version 0.x: We asumed the output to be linear, it is nearly 58 | linear but not exectly... . * 59 | **************************************************************************************************************** 60 | * This library is free software; you can redistribute it and/or 61 | * 62 | * modify it under the terms of the GNU Lesser General Public 63 | * 64 | * License as published by the Free Software Foundation; either 65 | * 66 | * version 2.1 of the License, or (at your option) any later version. 67 | * 68 | * 69 | * 70 | * This library is distributed in the hope that it will be useful, 71 | * 72 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 73 | * 74 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 75 | * 76 | * Lesser General Public License for more details. 77 | * 78 | * 79 | * 80 | * You should have received a copy of the GNU Lesser General Public 81 | * 82 | * License along with this library; if not, write to the Free Software 83 | * 84 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 85 | 02110-1301 USA * 86 | ***************************************************************************************************************/ 87 | #ifndef AcceleroMMA7361_h 88 | #define AcceleroMMA7361_h 89 | #include 90 | 91 | class AcceleroMMA7361 92 | { 93 | public: 94 | AcceleroMMA7361(); 95 | void begin(); 96 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 97 | gSelectPin, int xPin, int yPin, int zPin); 98 | int getXRaw(); 99 | int getYRaw(); 100 | int getZRaw(); 101 | int getXVolt(); 102 | int getYVolt(); 103 | int getZVolt(); 104 | int getXAccel(); 105 | int getYAccel(); 106 | int getZAccel(); 107 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 108 | int getTotalVector(); 109 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 110 | void calibrate(); // only to be executed when Z-axis is oriented to the ground 111 | 112 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 113 | void setARefVoltage(double _refV); 114 | void setAveraging(int avg); 115 | int getOrientation(); 116 | void setSensitivity(boolean sensi); 117 | void sleep(); 118 | void wake(); 119 | 120 | private: 121 | int _mapMMA7361V(int value); 122 | int _mapMMA7361G(int value); 123 | int _sleepPin; 124 | int _selfTestPin; 125 | int _zeroGPin; 126 | int _gSelectPin; 127 | int _xPin; 128 | int _yPin; 129 | int _zPin; 130 | int _offSets[3]; 131 | int _polarities[3]; 132 | double _refVoltage; 133 | int _average; 134 | boolean _sleep; 135 | boolean _sensi; 136 | }; 137 | #endif 138 | /**************************************************************************************** 139 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 140 | accelerometer. * 141 | * For more information: variable declaration, changelog,... see 142 | AcceleroMMA7361.h * 143 | **************************************************************************************** 144 | * This library is free software; you can redistribute it and/or 145 | * 146 | * modify it under the terms of the GNU Lesser General Public 147 | * 148 | * License as published by the Free Software Foundation; either 149 | * 150 | * version 2.1 of the License, or (at your option) any later version. 151 | * 152 | * 153 | * 154 | * This library is distributed in the hope that it will be useful, 155 | * 156 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 157 | * 158 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 159 | * 160 | * Lesser General Public License for more details. 161 | * 162 | * 163 | * 164 | * You should have received a copy of the GNU Lesser General Public 165 | * 166 | * License along with this library; if not, write to the Free Software 167 | * 168 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 169 | 02110-1301 USA * 170 | ***************************************************************************************/ 171 | /// 172 | /// acceleroMMA7361.cpp - Library for retrieving data from the MMA7361 accelerometer. 173 | 174 | /// For more information: variable declaration, changelog,... see AcceleroMMA7361.h 175 | 176 | /// 177 | 178 | 179 | /// 180 | /// constructor 181 | /// 182 | AcceleroMMA7361::AcceleroMMA7361() 183 | { 184 | } 185 | 186 | /// 187 | /// begin function to set pins: sleepPin = 13, selfTestPin = 12, 188 | /// zeroGPin = 11, gSelectPin = 10, xPin = A0, yPin = A1, zPin = A2. 189 | /// When you use begin() with an empty parameter list, these standard 190 | /// values are used 191 | /// 192 | void AcceleroMMA7361::begin() 193 | { 194 | begin(13, 12, 11, 10, A0, A1, A2); 195 | } 196 | 197 | /// 198 | /// begin variables 199 | /// - int sleepPin: number indicating to which pin the sleep port is 200 | /// attached. DIGITAL OUT 201 | /// - int selfTestPin: number indicating to which pin the selftest 202 | /// port is attached. DIGITAL OUT 203 | /// - int zeroGPin: number indicating to which pin the ZeroGpin is 204 | /// connected to. DIGITAL IN 205 | /// - int gSelectPin: number indication to which pin the Gselect is 206 | /// connected to. DIGITAL OUT 207 | /// - int xPin: number indicating to which pin the x-axis pin is 208 | /// connected to. ANALOG IN 209 | /// - int yPin: number indicating to which pin the y-axis pin is 210 | /// connected to. ANALOG IN 211 | /// - int zPin: number indicating to which pin the z-axis pin is 212 | /// connected to. ANALOG IN 213 | /// - int offset: array indicating the G offset on the x,y and z-axis 214 | /// When you use begin() without variables standard values are loaded: 215 | // A0,A1,A2 as input for X,Y,Z and digital pins 13,12,11,10 for sleep, 216 | /// selftest, zeroG and gSelect 217 | /// 218 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 219 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 220 | { 221 | pinMode(sleepPin, OUTPUT); 222 | pinMode(selfTestPin, OUTPUT); 223 | pinMode(zeroGPin, INPUT); 224 | pinMode(gSelectPin, INPUT); 225 | pinMode(xPin, INPUT); 226 | pinMode(yPin, INPUT); 227 | pinMode(zPin, INPUT); 228 | digitalWrite(sleepPin,HIGH); 229 | digitalWrite(selfTestPin,LOW); 230 | _sleepPin = sleepPin; 231 | _selfTestPin = selfTestPin; 232 | _zeroGPin = zeroGPin; 233 | _gSelectPin = gSelectPin; 234 | _xPin = xPin; 235 | _yPin = yPin; 236 | _zPin = zPin; 237 | _sleep = false; 238 | setOffSets(0,0,0); 239 | setARefVoltage(5); 240 | setAveraging(10); 241 | setSensitivity(HIGH); 242 | } 243 | 244 | /// 245 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 246 | /// offset values for the x,y,z axis. 247 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 248 | /// Offsets are added to the raw datafunctions 249 | /// 250 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 251 | { 252 | if (_refVoltage==3.3) 253 | { 254 | _offSets[0]= map(xOffSet,0,3300,0,1024); 255 | _offSets[1]= map(yOffSet,0,3300,0,1024); 256 | _offSets[2]= map(zOffSet,0,3300,0,1024); 257 | } 258 | else 259 | { 260 | _offSets[0]= map(xOffSet,0,5000,0,1024); 261 | _offSets[1]= map(yOffSet,0,5000,0,1024); 262 | _offSets[2]= map(zOffSet,0,5000,0,1024); 263 | } 264 | } 265 | 266 | /// 267 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 268 | /// (now only takes 3.3 or 5 as parameter) 269 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 270 | /// put a wire between the AREF pin and the 271 | /// 3.3 V VCC pin. This increases accuracy 272 | /// 273 | void AcceleroMMA7361::setARefVoltage(double refV) 274 | { 275 | _refVoltage = refV; 276 | if (refV == 3.3) 277 | { 278 | analogReference(EXTERNAL); 279 | } 280 | } 281 | 282 | /// 283 | /// setAveraging(int avg): Sets how many samples have to be averaged 284 | /// in getAccel default is 10. 285 | /// 286 | void AcceleroMMA7361::setAveraging(int avg) 287 | { 288 | _average = avg; 289 | } 290 | 291 | /// 292 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 293 | /// (LOW) using a boolean HIGH (1.5 G) or LOW (6 G) 294 | /// 295 | void AcceleroMMA7361::setSensitivity(boolean sensi) 296 | { 297 | _sensi = sensi; 298 | digitalWrite(_gSelectPin, !sensi); 299 | } 300 | 301 | /// 302 | /// sleep lets the device sleep (when device is sleeping already this 303 | /// does nothing) 304 | /// 305 | void AcceleroMMA7361::sleep() 306 | { 307 | if (!_sleep) 308 | { 309 | digitalWrite(_sleepPin, LOW); 310 | _sleep = true; 311 | } 312 | } 313 | 314 | /// 315 | /// wake enables the device after sleep (when device is not sleeping 316 | /// this does nothing) there is a 2 ms delay, due to enable response time 317 | /// (datasheet: typ 0.5 ms, max 2 ms) 318 | /// 319 | void AcceleroMMA7361::wake() 320 | { 321 | if (_sleep == true) 322 | { 323 | digitalWrite(_sleepPin, HIGH); 324 | _sleep = false; 325 | delay(2); 326 | } 327 | } 328 | 329 | /// 330 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 331 | /// the Arduino as an integer 332 | /// 333 | int AcceleroMMA7361::getXRaw() 334 | { 335 | return analogRead(_xPin)+_offSets[0]+2; 336 | } 337 | 338 | /// 339 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 340 | /// the Arduino as an integer 341 | /// 342 | int AcceleroMMA7361::getYRaw() 343 | { 344 | return analogRead(_yPin)+_offSets[1]+2; 345 | } 346 | 347 | /// 348 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 349 | /// the Arduino as an integer 350 | /// 351 | int AcceleroMMA7361::getZRaw() 352 | { 353 | return analogRead(_zPin)+_offSets[2]; 354 | } 355 | 356 | /// 357 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 358 | /// port of the Arduino as a integer 359 | /// 360 | int AcceleroMMA7361::getXVolt() 361 | { 362 | return _mapMMA7361V(getXRaw()); 363 | } 364 | 365 | /// 366 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 367 | /// port of the Arduino as a integer 368 | /// 369 | int AcceleroMMA7361::getYVolt() 370 | { 371 | return _mapMMA7361V(getYRaw()); 372 | } 373 | 374 | /// 375 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 376 | /// port of the Arduino as a integer 377 | /// 378 | int AcceleroMMA7361::getZVolt() 379 | { 380 | return _mapMMA7361V(getZRaw()); 381 | } 382 | 383 | /// 384 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 385 | /// 386 | int AcceleroMMA7361::getXAccel() 387 | { 388 | int sum = 0; 389 | for (int i = 0;i<_average;i++) 390 | { 391 | sum = sum + _mapMMA7361G(getXRaw()); 392 | } 393 | return sum/_average; 394 | } 395 | 396 | /// 397 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 398 | /// 399 | int AcceleroMMA7361::getYAccel() 400 | { 401 | int sum = 0; 402 | for (int i = 0;i<_average;i++) 403 | { 404 | sum = sum + _mapMMA7361G(getYRaw()); 405 | } 406 | return sum/_average; 407 | } 408 | 409 | /// 410 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 411 | /// 412 | int AcceleroMMA7361::getZAccel() 413 | { 414 | int sum = 0; 415 | for (int i = 0;i<_average;i++) 416 | { 417 | sum = sum + _mapMMA7361G(getZRaw()); 418 | } 419 | return sum/_average; 420 | } 421 | 422 | /// 423 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 424 | /// axis at once as pointers 425 | /// 426 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 427 | { 428 | int sum[3]; 429 | sum[0] = 0; 430 | sum[1] = 0; 431 | sum[2] = 0; 432 | for (int i = 0;i<_average;i++) 433 | { 434 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 435 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 436 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 437 | } 438 | *_XAxis = sum[0]/_average; 439 | *_YAxis = sum[1]/_average; 440 | *_ZAxis = sum[2]/_average; 441 | } 442 | 443 | /// 444 | /// mapMMA7361V: calculates and returns the voltage value derived from 445 | /// the raw data. Used in getXVoltage, getYVoltage, getZVoltage 446 | /// 447 | int AcceleroMMA7361::_mapMMA7361V(int value) 448 | { 449 | if (_refVoltage==3.3) 450 | { 451 | return map(value,0,1024,0,3300); 452 | } 453 | else 454 | { 455 | return map(value,0,1024,0,5000); 456 | } 457 | } 458 | 459 | /// 460 | /// mapMMA7361G: calculates and returns the accelerometer value in 461 | /// degrees derived from the raw data. Used in getXAccel, getYAccel, 462 | /// getZAccel 463 | /// 464 | int AcceleroMMA7361::_mapMMA7361G(int value) 465 | { 466 | if(_sensi == false) 467 | { 468 | if (_refVoltage==3.3) 469 | { 470 | return map(value,0,1024,-825,800); 471 | } 472 | else 473 | { 474 | return map(value,0,1024,-800,1600); 475 | } 476 | } 477 | else 478 | { 479 | if (_refVoltage==3.3) 480 | { 481 | return map(value,0,1024,-206,206); 482 | } 483 | else 484 | { 485 | return map(value,0,1024,-260,419); 486 | } 487 | } 488 | } 489 | 490 | /// 491 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 492 | /// axis will be set to 100 = 1G 493 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 494 | /// PERPENDICULAR WITH THE EARTHS SURFACE 495 | /// 496 | void AcceleroMMA7361::calibrate() 497 | { 498 | Serial.println(getOrientation()); 499 | Serial.print("\nCalibrating MMA7361011"); 500 | double var = 5000; 501 | double sumX = 0; 502 | double sumY = 0; 503 | double sumZ = 0; 504 | for (int i = 0;i 534 | /// getOrientation returns which axis perpendicular with the earths 535 | /// surface x=1,y=2,z=3 is positive or 536 | /// negative depending on which side of the axis is pointing downwards 537 | /// 538 | int AcceleroMMA7361::getOrientation() 539 | { 540 | int gemiddelde = 10; 541 | int x = 0; 542 | int y = 0; 543 | int z = 0; 544 | int xAbs = 0; 545 | int yAbs = 0; 546 | int zAbs = 0; 547 | for(int i = 0; i0) 563 | { 564 | return 1; 565 | } 566 | return -1; 567 | } 568 | if (yAbs0) 571 | { 572 | return 2; 573 | } 574 | return -2; 575 | } 576 | if (zAbs0) 579 | { 580 | return 3; 581 | } 582 | return -3; 583 | } 584 | return 0; 585 | } 586 | 587 | /// 588 | /// getTotalVector returns the magnitude of the total acceleration 589 | /// vector as an integer 590 | /// 591 | int AcceleroMMA7361::getTotalVector() 592 | { 593 | return sqrt(square(_mapMMA7361G(getXRaw())) + 594 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 595 | } 596 | 597 | 598 | AcceleroMMA7361 accelero; 599 | int x; 600 | int y; 601 | int z; 602 | 603 | 604 | void setup() 605 | { 606 | Serial1.begin(9600); 607 | Serial.begin(9600); 608 | accelero.begin(13, 12, 11, 10, A0, A1, A2); 609 | // void begin(int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 610 | // WA¯NE: do tego jeszcze pod³±czyæV3 do AREF'u!!!! 611 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 612 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 613 | accelero.calibrate(); 614 | MIDI.begin(4); 615 | } 616 | 617 | void loop() 618 | { 619 | x = accelero.getXAccel(); 620 | y = accelero.getYAccel(); 621 | z = accelero.getZAccel(); 622 | //Serial1.print("\nx: "); 623 | //Serial1.println(x); 624 | //Serial1.print(" \ty: "); 625 | //Serial1.println(y); 626 | //Serial1.print(" \tz: "); 627 | //Serial1.println(z); 628 | //Serial.print("\nx: "); 629 | //Serial.println(x); 630 | //Serial.print(" \ty: "); 631 | //Serial.println(y); 632 | //Serial.print(" \tz: "); 633 | //Serial.println(z); 634 | x=abs(x); 635 | MIDI.sendNoteOn(42,127,1); // Send a Note (pitch 42, velo 127 on channel 1) 636 | delay(1000); // Wait for a second 637 | MIDI.sendNoteOff(42,0,1); // Stop the note 638 | 639 | 640 | delay(1000); //make it readable 641 | 642 | } 643 | -------------------------------------------------------------------------------- /sources/accelerometer_midi/accelerometer_midi.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #define MIDI_BAUDRATE 9600 3 | #define USE_SERIAL_PORT Serial1 4 | #define COMPILE_MIDI_OUT 1 5 | 6 | /**************************************************************************************************************** 7 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 8 | accelerometer. * 9 | * Copyright 2011 Jef Neefs (neefs@gmail.com) and Jeroen Doggen 10 | (jeroendoggen@gmail.com) * 11 | * DATASHEET: http://www.sparkfun.com/datasheets/Components/General/MMA7361L.pdf 12 | * 13 | **************************************************************************************************************** 14 | * Version History: 15 | * 16 | * Version 0.1: -get raw values 17 | * 18 | * Version 0.2: -get voltages and G forces 19 | * 20 | * Version 0.3: -removed begin parameters offset 21 | * 22 | * -added public function setOffSets(int,int,int) 23 | * 24 | * -added a private variable _offSets[3] containing the 25 | offset on each axis * 26 | * -changed long and double return values of private and 27 | public functions to int * 28 | * Version 0.4: -added calibrate 29 | * 30 | * Version 0.5: -added setARefVoltage 31 | * 32 | * -added setAveraging 33 | * 34 | * -added a default begin function 35 | * 36 | * Version 0.6: -added getAccelXYZ to get all axis in one call 37 | * 38 | * -added getTotalVector returns the magnitude of the 39 | total vector as an integer * 40 | * -added getOrientation returns which axis 41 | perpendicular with the earths surface x=1,y=2,z=3 * 42 | * is positive or negative depending on which side of 43 | the axis is pointing downwards * 44 | * Version 0.7: -added setSensitivity 45 | * 46 | * -added sleep & wake 47 | * 48 | * Version 0.8: -converted to Arduino 1.0 library 49 | * 50 | * -changed license to LGPL 51 | * 52 | * Roadmap: 53 | * 54 | * Version 0.x: auto zero calibration 55 | http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf 56 | * 57 | * Version 0.x: We asumed the output to be linear, it is nearly 58 | linear but not exectly... . * 59 | **************************************************************************************************************** 60 | * This library is free software; you can redistribute it and/or 61 | * 62 | * modify it under the terms of the GNU Lesser General Public 63 | * 64 | * License as published by the Free Software Foundation; either 65 | * 66 | * version 2.1 of the License, or (at your option) any later version. 67 | * 68 | * 69 | * 70 | * This library is distributed in the hope that it will be useful, 71 | * 72 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 73 | * 74 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 75 | * 76 | * Lesser General Public License for more details. 77 | * 78 | * 79 | * 80 | * You should have received a copy of the GNU Lesser General Public 81 | * 82 | * License along with this library; if not, write to the Free Software 83 | * 84 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 85 | 02110-1301 USA * 86 | ***************************************************************************************************************/ 87 | #ifndef AcceleroMMA7361_h 88 | #define AcceleroMMA7361_h 89 | #include 90 | 91 | class AcceleroMMA7361 92 | { 93 | public: 94 | AcceleroMMA7361(); 95 | void begin(); 96 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 97 | gSelectPin, int xPin, int yPin, int zPin); 98 | int getXRaw(); 99 | int getYRaw(); 100 | int getZRaw(); 101 | int getXVolt(); 102 | int getYVolt(); 103 | int getZVolt(); 104 | int getXAccel(); 105 | int getYAccel(); 106 | int getZAccel(); 107 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 108 | int getTotalVector(); 109 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 110 | void calibrate(); // only to be executed when Z-axis is oriented to the ground 111 | 112 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 113 | void setARefVoltage(double _refV); 114 | void setAveraging(int avg); 115 | int getOrientation(); 116 | void setSensitivity(boolean sensi); 117 | void sleep(); 118 | void wake(); 119 | 120 | private: 121 | int _mapMMA7361V(int value); 122 | int _mapMMA7361G(int value); 123 | int _sleepPin; 124 | int _selfTestPin; 125 | int _zeroGPin; 126 | int _gSelectPin; 127 | int _xPin; 128 | int _yPin; 129 | int _zPin; 130 | int _offSets[3]; 131 | int _polarities[3]; 132 | double _refVoltage; 133 | int _average; 134 | boolean _sleep; 135 | boolean _sensi; 136 | }; 137 | #endif 138 | /**************************************************************************************** 139 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 140 | accelerometer. * 141 | * For more information: variable declaration, changelog,... see 142 | AcceleroMMA7361.h * 143 | **************************************************************************************** 144 | * This library is free software; you can redistribute it and/or 145 | * 146 | * modify it under the terms of the GNU Lesser General Public 147 | * 148 | * License as published by the Free Software Foundation; either 149 | * 150 | * version 2.1 of the License, or (at your option) any later version. 151 | * 152 | * 153 | * 154 | * This library is distributed in the hope that it will be useful, 155 | * 156 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 157 | * 158 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 159 | * 160 | * Lesser General Public License for more details. 161 | * 162 | * 163 | * 164 | * You should have received a copy of the GNU Lesser General Public 165 | * 166 | * License along with this library; if not, write to the Free Software 167 | * 168 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 169 | 02110-1301 USA * 170 | ***************************************************************************************/ 171 | /// 172 | /// acceleroMMA7361.cpp - Library for retrieving data from the MMA7361 accelerometer. 173 | 174 | /// For more information: variable declaration, changelog,... see AcceleroMMA7361.h 175 | 176 | /// 177 | 178 | 179 | /// 180 | /// constructor 181 | /// 182 | AcceleroMMA7361::AcceleroMMA7361() 183 | { 184 | } 185 | 186 | /// 187 | /// begin function to set pins: sleepPin = 13, selfTestPin = 12, 188 | /// zeroGPin = 11, gSelectPin = 10, xPin = A0, yPin = A1, zPin = A2. 189 | /// When you use begin() with an empty parameter list, these standard 190 | /// values are used 191 | /// 192 | void AcceleroMMA7361::begin() 193 | { 194 | begin(13, 12, 11, 10, A0, A1, A2); 195 | } 196 | 197 | /// 198 | /// begin variables 199 | /// - int sleepPin: number indicating to which pin the sleep port is 200 | /// attached. DIGITAL OUT 201 | /// - int selfTestPin: number indicating to which pin the selftest 202 | /// port is attached. DIGITAL OUT 203 | /// - int zeroGPin: number indicating to which pin the ZeroGpin is 204 | /// connected to. DIGITAL IN 205 | /// - int gSelectPin: number indication to which pin the Gselect is 206 | /// connected to. DIGITAL OUT 207 | /// - int xPin: number indicating to which pin the x-axis pin is 208 | /// connected to. ANALOG IN 209 | /// - int yPin: number indicating to which pin the y-axis pin is 210 | /// connected to. ANALOG IN 211 | /// - int zPin: number indicating to which pin the z-axis pin is 212 | /// connected to. ANALOG IN 213 | /// - int offset: array indicating the G offset on the x,y and z-axis 214 | /// When you use begin() without variables standard values are loaded: 215 | // A0,A1,A2 as input for X,Y,Z and digital pins 13,12,11,10 for sleep, 216 | /// selftest, zeroG and gSelect 217 | /// 218 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 219 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 220 | { 221 | pinMode(sleepPin, OUTPUT); 222 | pinMode(selfTestPin, OUTPUT); 223 | pinMode(zeroGPin, INPUT); 224 | pinMode(gSelectPin, INPUT); 225 | pinMode(xPin, INPUT); 226 | pinMode(yPin, INPUT); 227 | pinMode(zPin, INPUT); 228 | digitalWrite(sleepPin,HIGH); 229 | digitalWrite(selfTestPin,LOW); 230 | _sleepPin = sleepPin; 231 | _selfTestPin = selfTestPin; 232 | _zeroGPin = zeroGPin; 233 | _gSelectPin = gSelectPin; 234 | _xPin = xPin; 235 | _yPin = yPin; 236 | _zPin = zPin; 237 | _sleep = false; 238 | setOffSets(0,0,0); 239 | setARefVoltage(5); 240 | setAveraging(10); 241 | setSensitivity(HIGH); 242 | } 243 | 244 | /// 245 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 246 | /// offset values for the x,y,z axis. 247 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 248 | /// Offsets are added to the raw datafunctions 249 | /// 250 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 251 | { 252 | if (_refVoltage==3.3) 253 | { 254 | _offSets[0]= map(xOffSet,0,3300,0,1024); 255 | _offSets[1]= map(yOffSet,0,3300,0,1024); 256 | _offSets[2]= map(zOffSet,0,3300,0,1024); 257 | } 258 | else 259 | { 260 | _offSets[0]= map(xOffSet,0,5000,0,1024); 261 | _offSets[1]= map(yOffSet,0,5000,0,1024); 262 | _offSets[2]= map(zOffSet,0,5000,0,1024); 263 | } 264 | } 265 | 266 | /// 267 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 268 | /// (now only takes 3.3 or 5 as parameter) 269 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 270 | /// put a wire between the AREF pin and the 271 | /// 3.3 V VCC pin. This increases accuracy 272 | /// 273 | void AcceleroMMA7361::setARefVoltage(double refV) 274 | { 275 | _refVoltage = refV; 276 | if (refV == 3.3) 277 | { 278 | analogReference(EXTERNAL); 279 | } 280 | } 281 | 282 | /// 283 | /// setAveraging(int avg): Sets how many samples have to be averaged 284 | /// in getAccel default is 10. 285 | /// 286 | void AcceleroMMA7361::setAveraging(int avg) 287 | { 288 | _average = avg; 289 | } 290 | 291 | /// 292 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 293 | /// (LOW) using a boolean HIGH (1.5 G) or LOW (6 G) 294 | /// 295 | void AcceleroMMA7361::setSensitivity(boolean sensi) 296 | { 297 | _sensi = sensi; 298 | digitalWrite(_gSelectPin, !sensi); 299 | } 300 | 301 | /// 302 | /// sleep lets the device sleep (when device is sleeping already this 303 | /// does nothing) 304 | /// 305 | void AcceleroMMA7361::sleep() 306 | { 307 | if (!_sleep) 308 | { 309 | digitalWrite(_sleepPin, LOW); 310 | _sleep = true; 311 | } 312 | } 313 | 314 | /// 315 | /// wake enables the device after sleep (when device is not sleeping 316 | /// this does nothing) there is a 2 ms delay, due to enable response time 317 | /// (datasheet: typ 0.5 ms, max 2 ms) 318 | /// 319 | void AcceleroMMA7361::wake() 320 | { 321 | if (_sleep == true) 322 | { 323 | digitalWrite(_sleepPin, HIGH); 324 | _sleep = false; 325 | delay(2); 326 | } 327 | } 328 | 329 | /// 330 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 331 | /// the Arduino as an integer 332 | /// 333 | int AcceleroMMA7361::getXRaw() 334 | { 335 | return analogRead(_xPin)+_offSets[0]+2; 336 | } 337 | 338 | /// 339 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 340 | /// the Arduino as an integer 341 | /// 342 | int AcceleroMMA7361::getYRaw() 343 | { 344 | return analogRead(_yPin)+_offSets[1]+2; 345 | } 346 | 347 | /// 348 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 349 | /// the Arduino as an integer 350 | /// 351 | int AcceleroMMA7361::getZRaw() 352 | { 353 | return analogRead(_zPin)+_offSets[2]; 354 | } 355 | 356 | /// 357 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 358 | /// port of the Arduino as a integer 359 | /// 360 | int AcceleroMMA7361::getXVolt() 361 | { 362 | return _mapMMA7361V(getXRaw()); 363 | } 364 | 365 | /// 366 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 367 | /// port of the Arduino as a integer 368 | /// 369 | int AcceleroMMA7361::getYVolt() 370 | { 371 | return _mapMMA7361V(getYRaw()); 372 | } 373 | 374 | /// 375 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 376 | /// port of the Arduino as a integer 377 | /// 378 | int AcceleroMMA7361::getZVolt() 379 | { 380 | return _mapMMA7361V(getZRaw()); 381 | } 382 | 383 | /// 384 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 385 | /// 386 | int AcceleroMMA7361::getXAccel() 387 | { 388 | int sum = 0; 389 | for (int i = 0;i<_average;i++) 390 | { 391 | sum = sum + _mapMMA7361G(getXRaw()); 392 | } 393 | return sum/_average; 394 | } 395 | 396 | /// 397 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 398 | /// 399 | int AcceleroMMA7361::getYAccel() 400 | { 401 | int sum = 0; 402 | for (int i = 0;i<_average;i++) 403 | { 404 | sum = sum + _mapMMA7361G(getYRaw()); 405 | } 406 | return sum/_average; 407 | } 408 | 409 | /// 410 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 411 | /// 412 | int AcceleroMMA7361::getZAccel() 413 | { 414 | int sum = 0; 415 | for (int i = 0;i<_average;i++) 416 | { 417 | sum = sum + _mapMMA7361G(getZRaw()); 418 | } 419 | return sum/_average; 420 | } 421 | 422 | /// 423 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 424 | /// axis at once as pointers 425 | /// 426 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 427 | { 428 | int sum[3]; 429 | sum[0] = 0; 430 | sum[1] = 0; 431 | sum[2] = 0; 432 | for (int i = 0;i<_average;i++) 433 | { 434 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 435 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 436 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 437 | } 438 | *_XAxis = sum[0]/_average; 439 | *_YAxis = sum[1]/_average; 440 | *_ZAxis = sum[2]/_average; 441 | } 442 | 443 | /// 444 | /// mapMMA7361V: calculates and returns the voltage value derived from 445 | /// the raw data. Used in getXVoltage, getYVoltage, getZVoltage 446 | /// 447 | int AcceleroMMA7361::_mapMMA7361V(int value) 448 | { 449 | if (_refVoltage==3.3) 450 | { 451 | return map(value,0,1024,0,3300); 452 | } 453 | else 454 | { 455 | return map(value,0,1024,0,5000); 456 | } 457 | } 458 | 459 | /// 460 | /// mapMMA7361G: calculates and returns the accelerometer value in 461 | /// degrees derived from the raw data. Used in getXAccel, getYAccel, 462 | /// getZAccel 463 | /// 464 | int AcceleroMMA7361::_mapMMA7361G(int value) 465 | { 466 | if(_sensi == false) 467 | { 468 | if (_refVoltage==3.3) 469 | { 470 | return map(value,0,1024,-825,800); 471 | } 472 | else 473 | { 474 | return map(value,0,1024,-800,1600); 475 | } 476 | } 477 | else 478 | { 479 | if (_refVoltage==3.3) 480 | { 481 | return map(value,0,1024,-206,206); 482 | } 483 | else 484 | { 485 | return map(value,0,1024,-260,419); 486 | } 487 | } 488 | } 489 | 490 | /// 491 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 492 | /// axis will be set to 100 = 1G 493 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 494 | /// PERPENDICULAR WITH THE EARTHS SURFACE 495 | /// 496 | void AcceleroMMA7361::calibrate() 497 | { 498 | Serial.println(getOrientation()); 499 | Serial.print("\nCalibrating MMA7361011"); 500 | double var = 5000; 501 | double sumX = 0; 502 | double sumY = 0; 503 | double sumZ = 0; 504 | for (int i = 0;i 534 | /// getOrientation returns which axis perpendicular with the earths 535 | /// surface x=1,y=2,z=3 is positive or 536 | /// negative depending on which side of the axis is pointing downwards 537 | /// 538 | int AcceleroMMA7361::getOrientation() 539 | { 540 | int gemiddelde = 10; 541 | int x = 0; 542 | int y = 0; 543 | int z = 0; 544 | int xAbs = 0; 545 | int yAbs = 0; 546 | int zAbs = 0; 547 | for(int i = 0; i0) 563 | { 564 | return 1; 565 | } 566 | return -1; 567 | } 568 | if (yAbs0) 571 | { 572 | return 2; 573 | } 574 | return -2; 575 | } 576 | if (zAbs0) 579 | { 580 | return 3; 581 | } 582 | return -3; 583 | } 584 | return 0; 585 | } 586 | 587 | /// 588 | /// getTotalVector returns the magnitude of the total acceleration 589 | /// vector as an integer 590 | /// 591 | int AcceleroMMA7361::getTotalVector() 592 | { 593 | return sqrt(square(_mapMMA7361G(getXRaw())) + 594 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 595 | } 596 | 597 | 598 | AcceleroMMA7361 accelero; 599 | int x; 600 | int y; 601 | int z; 602 | 603 | 604 | void setup() 605 | { 606 | Serial1.begin(9600); 607 | Serial.begin(9600); 608 | accelero.begin(13, 12, 11, 10, A0, A1, A2); 609 | // void begin(int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 610 | // WA¯NE: do tego jeszcze pod³±czyæV3 do AREF'u!!!! 611 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 612 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 613 | accelero.calibrate(); 614 | MIDI.begin(4); 615 | } 616 | 617 | void loop() 618 | { 619 | x = accelero.getXAccel(); 620 | y = accelero.getYAccel(); 621 | z = accelero.getZAccel(); 622 | //Serial1.print("\nx: "); 623 | //Serial1.println(x); 624 | //Serial1.print(" \ty: "); 625 | //Serial1.println(y); 626 | //Serial1.print(" \tz: "); 627 | //Serial1.println(z); 628 | //Serial.print("\nx: "); 629 | //Serial.println(x); 630 | //Serial.print(" \ty: "); 631 | //Serial.println(y); 632 | //Serial.print(" \tz: "); 633 | //Serial.println(z); 634 | x=abs(x); 635 | MIDI.sendNoteOn(42,127,1); // Send a Note (pitch 42, velo 127 on channel 1) 636 | delay(1000); // Wait for a second 637 | MIDI.sendNoteOff(42,0,1); // Stop the note 638 | 639 | 640 | delay(1000); //make it readable 641 | 642 | } 643 | -------------------------------------------------------------------------------- /sources/accelerometer_read_adaptation_uno.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************************************************** 2 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 3 | accelerometer. * 4 | * Copyright 2011 Jef Neefs (neefs@gmail.com) and Jeroen Doggen 5 | (jeroendoggen@gmail.com) * 6 | * DATASHEET: http://www.sparkfun.com/datasheets/Components/General/MMA7361L.pdf 7 | * 8 | **************************************************************************************************************** 9 | * Version History: 10 | * 11 | * Version 0.1: -get raw values 12 | * 13 | * Version 0.2: -get voltages and G forces 14 | * 15 | * Version 0.3: -removed begin parameters offset 16 | * 17 | * -added public function setOffSets(int,int,int) 18 | * 19 | * -added a private variable _offSets[3] containing the 20 | offset on each axis * 21 | * -changed long and double return values of private and 22 | public functions to int * 23 | * Version 0.4: -added calibrate 24 | * 25 | * Version 0.5: -added setARefVoltage 26 | * 27 | * -added setAveraging 28 | * 29 | * -added a default begin function 30 | * 31 | * Version 0.6: -added getAccelXYZ to get all axis in one call 32 | * 33 | * -added getTotalVector returns the magnitude of the 34 | total vector as an integer * 35 | * -added getOrientation returns which axis 36 | perpendicular with the earths surface x=1,y=2,z=3 * 37 | * is positive or negative depending on which side of 38 | the axis is pointing downwards * 39 | * Version 0.7: -added setSensitivity 40 | * 41 | * -added sleep & wake 42 | * 43 | * Version 0.8: -converted to Arduino 1.0 library 44 | * 45 | * -changed license to LGPL 46 | * 47 | * Roadmap: 48 | * 49 | * Version 0.x: auto zero calibration 50 | http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf 51 | * 52 | * Version 0.x: We asumed the output to be linear, it is nearly 53 | linear but not exectly... . * 54 | **************************************************************************************************************** 55 | * This library is free software; you can redistribute it and/or 56 | * 57 | * modify it under the terms of the GNU Lesser General Public 58 | * 59 | * License as published by the Free Software Foundation; either 60 | * 61 | * version 2.1 of the License, or (at your option) any later version. 62 | * 63 | * 64 | * 65 | * This library is distributed in the hope that it will be useful, 66 | * 67 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 68 | * 69 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 70 | * 71 | * Lesser General Public License for more details. 72 | * 73 | * 74 | * 75 | * You should have received a copy of the GNU Lesser General Public 76 | * 77 | * License along with this library; if not, write to the Free Software 78 | * 79 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 80 | 02110-1301 USA * 81 | ***************************************************************************************************************/ 82 | #ifndef AcceleroMMA7361_h 83 | #define AcceleroMMA7361_h 84 | #include 85 | 86 | class AcceleroMMA7361 87 | { 88 | public: 89 | AcceleroMMA7361(); 90 | void begin(); 91 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 92 | gSelectPin, int xPin, int yPin, int zPin); 93 | int getXRaw(); 94 | int getYRaw(); 95 | int getZRaw(); 96 | int getXVolt(); 97 | int getYVolt(); 98 | int getZVolt(); 99 | int getXAccel(); 100 | int getYAccel(); 101 | int getZAccel(); 102 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 103 | int getTotalVector(); 104 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 105 | void calibrate(); // only to be executed when Z-axis is oriented to 106 | 107 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 108 | void setARefVoltage(double _refV); 109 | void setAveraging(int avg); 110 | int getOrientation(); 111 | void setSensitivity(boolean sensi); 112 | void sleep(); 113 | void wake(); 114 | 115 | private: 116 | int _mapMMA7361V(int value); 117 | int _mapMMA7361G(int value); 118 | int _sleepPin; 119 | int _selfTestPin; 120 | int _zeroGPin; 121 | int _gSelectPin; 122 | int _xPin; 123 | int _yPin; 124 | int _zPin; 125 | int _offSets[3]; 126 | int _polarities[3]; 127 | double _refVoltage; 128 | int _average; 129 | boolean _sleep; 130 | boolean _sensi; 131 | }; 132 | #endif 133 | /**************************************************************************************** 134 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 135 | accelerometer. * 136 | * For more information: variable declaration, changelog,... see 137 | AcceleroMMA7361.h * 138 | **************************************************************************************** 139 | * This library is free software; you can redistribute it and/or 140 | * 141 | * modify it under the terms of the GNU Lesser General Public 142 | * 143 | * License as published by the Free Software Foundation; either 144 | * 145 | * version 2.1 of the License, or (at your option) any later version. 146 | * 147 | * 148 | * 149 | * This library is distributed in the hope that it will be useful, 150 | * 151 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 152 | * 153 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 154 | * 155 | * Lesser General Public License for more details. 156 | * 157 | * 158 | * 159 | * You should have received a copy of the GNU Lesser General Public 160 | * 161 | * License along with this library; if not, write to the Free Software 162 | * 163 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 164 | 02110-1301 USA * 165 | ***************************************************************************************/ 166 | /// 167 | /// acceleroMMA7361.cpp - Library for retrieving data from the MMA7361 168 | 169 | /// For more information: variable declaration, changelog,... see 170 | 171 | /// 172 | 173 | 174 | /// 175 | /// constructor 176 | /// 177 | AcceleroMMA7361::AcceleroMMA7361() 178 | { 179 | } 180 | 181 | /// 182 | /// begin function to set pins: sleepPin = 13, selfTestPin = 12, 183 | 184 | /// When you use begin() with an empty parameter list, these standard 185 | 186 | /// 187 | void AcceleroMMA7361::begin() 188 | { 189 | begin(13, 12, 11, 10, A0, A1, A2); 190 | } 191 | 192 | /// 193 | /// begin variables 194 | /// - int sleepPin: number indicating to which pin the sleep port is 195 | 196 | /// - int selfTestPin: number indicating to which pin the selftest 197 | 198 | /// - int zeroGPin: number indicating to which pin the ZeroGpin is 199 | 200 | /// - int gSelectPin: number indication to which pin the Gselect is 201 | 202 | /// - int xPin: number indicating to which pin the x-axis pin is 203 | 204 | /// - int yPin: number indicating to which pin the y-axis pin is 205 | 206 | /// - int zPin: number indicating to which pin the z-axis pin is 207 | 208 | /// - int offset: array indicating the G offset on the x,y and z-axis 209 | /// When you use begin() without variables standard values are loaded: 210 | 211 | 212 | /// 213 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 214 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 215 | { 216 | pinMode(sleepPin, OUTPUT); 217 | pinMode(selfTestPin, OUTPUT); 218 | pinMode(zeroGPin, INPUT); 219 | pinMode(gSelectPin, INPUT); 220 | pinMode(xPin, INPUT); 221 | pinMode(yPin, INPUT); 222 | pinMode(zPin, INPUT); 223 | digitalWrite(sleepPin,HIGH); 224 | digitalWrite(selfTestPin,LOW); 225 | _sleepPin = sleepPin; 226 | _selfTestPin = selfTestPin; 227 | _zeroGPin = zeroGPin; 228 | _gSelectPin = gSelectPin; 229 | _xPin = xPin; 230 | _yPin = yPin; 231 | _zPin = zPin; 232 | _sleep = false; 233 | setOffSets(0,0,0); 234 | setARefVoltage(5); 235 | setAveraging(10); 236 | setSensitivity(HIGH); 237 | } 238 | 239 | /// 240 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 241 | 242 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 243 | /// Offsets are added to the raw datafunctions 244 | /// 245 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 246 | { 247 | if (_refVoltage==3.3) 248 | { 249 | _offSets[0]= map(xOffSet,0,3300,0,1024); 250 | _offSets[1]= map(yOffSet,0,3300,0,1024); 251 | _offSets[2]= map(zOffSet,0,3300,0,1024); 252 | } 253 | else 254 | { 255 | _offSets[0]= map(xOffSet,0,5000,0,1024); 256 | _offSets[1]= map(yOffSet,0,5000,0,1024); 257 | _offSets[2]= map(zOffSet,0,5000,0,1024); 258 | } 259 | } 260 | 261 | /// 262 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 263 | 264 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 265 | 266 | /// 3.3 V VCC pin. This increases accuracy 267 | /// 268 | void AcceleroMMA7361::setARefVoltage(double refV) 269 | { 270 | _refVoltage = refV; 271 | if (refV == 3.3) 272 | { 273 | analogReference(EXTERNAL); 274 | } 275 | } 276 | 277 | /// 278 | /// setAveraging(int avg): Sets how many samples have to be averaged 279 | 280 | /// 281 | void AcceleroMMA7361::setAveraging(int avg) 282 | { 283 | _average = avg; 284 | } 285 | 286 | /// 287 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 288 | 289 | /// 290 | void AcceleroMMA7361::setSensitivity(boolean sensi) 291 | { 292 | _sensi = sensi; 293 | digitalWrite(_gSelectPin, !sensi); 294 | } 295 | 296 | /// 297 | /// sleep lets the device sleep (when device is sleeping already this 298 | 299 | /// 300 | void AcceleroMMA7361::sleep() 301 | { 302 | if (!_sleep) 303 | { 304 | digitalWrite(_sleepPin, LOW); 305 | _sleep = true; 306 | } 307 | } 308 | 309 | /// 310 | /// wake enables the device after sleep (when device is not sleeping 311 | 312 | 313 | /// 314 | void AcceleroMMA7361::wake() 315 | { 316 | if (_sleep == true) 317 | { 318 | digitalWrite(_sleepPin, HIGH); 319 | _sleep = false; 320 | delay(2); 321 | } 322 | } 323 | 324 | /// 325 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 326 | 327 | /// 328 | int AcceleroMMA7361::getXRaw() 329 | { 330 | return analogRead(_xPin)+_offSets[0]+2; 331 | } 332 | 333 | /// 334 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 335 | 336 | /// 337 | int AcceleroMMA7361::getYRaw() 338 | { 339 | return analogRead(_yPin)+_offSets[1]+2; 340 | } 341 | 342 | /// 343 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 344 | 345 | /// 346 | int AcceleroMMA7361::getZRaw() 347 | { 348 | return analogRead(_zPin)+_offSets[2]; 349 | } 350 | 351 | /// 352 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 353 | 354 | /// 355 | int AcceleroMMA7361::getXVolt() 356 | { 357 | return _mapMMA7361V(getXRaw()); 358 | } 359 | 360 | /// 361 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 362 | 363 | /// 364 | int AcceleroMMA7361::getYVolt() 365 | { 366 | return _mapMMA7361V(getYRaw()); 367 | } 368 | 369 | /// 370 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 371 | 372 | /// 373 | int AcceleroMMA7361::getZVolt() 374 | { 375 | return _mapMMA7361V(getZRaw()); 376 | } 377 | 378 | /// 379 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 380 | /// 381 | int AcceleroMMA7361::getXAccel() 382 | { 383 | int sum = 0; 384 | for (int i = 0;i<_average;i++) 385 | { 386 | sum = sum + _mapMMA7361G(getXRaw()); 387 | } 388 | return sum/_average; 389 | } 390 | 391 | /// 392 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 393 | /// 394 | int AcceleroMMA7361::getYAccel() 395 | { 396 | int sum = 0; 397 | for (int i = 0;i<_average;i++) 398 | { 399 | sum = sum + _mapMMA7361G(getYRaw()); 400 | } 401 | return sum/_average; 402 | } 403 | 404 | /// 405 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 406 | /// 407 | int AcceleroMMA7361::getZAccel() 408 | { 409 | int sum = 0; 410 | for (int i = 0;i<_average;i++) 411 | { 412 | sum = sum + _mapMMA7361G(getZRaw()); 413 | } 414 | return sum/_average; 415 | } 416 | 417 | /// 418 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 419 | 420 | /// 421 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 422 | { 423 | int sum[3]; 424 | sum[0] = 0; 425 | sum[1] = 0; 426 | sum[2] = 0; 427 | for (int i = 0;i<_average;i++) 428 | { 429 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 430 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 431 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 432 | } 433 | *_XAxis = sum[0]/_average; 434 | *_YAxis = sum[1]/_average; 435 | *_ZAxis = sum[2]/_average; 436 | } 437 | 438 | /// 439 | /// mapMMA7361V: calculates and returns the voltage value derived from 440 | 441 | /// 442 | int AcceleroMMA7361::_mapMMA7361V(int value) 443 | { 444 | if (_refVoltage==3.3) 445 | { 446 | return map(value,0,1024,0,3300); 447 | } 448 | else 449 | { 450 | return map(value,0,1024,0,5000); 451 | } 452 | } 453 | 454 | /// 455 | /// mapMMA7361G: calculates and returns the accelerometer value in 456 | 457 | 458 | /// 459 | int AcceleroMMA7361::_mapMMA7361G(int value) 460 | { 461 | if(_sensi == false) 462 | { 463 | if (_refVoltage==3.3) 464 | { 465 | return map(value,0,1024,-825,800); 466 | } 467 | else 468 | { 469 | return map(value,0,1024,-800,1600); 470 | } 471 | } 472 | else 473 | { 474 | if (_refVoltage==3.3) 475 | { 476 | return map(value,0,1024,-206,206); 477 | } 478 | else 479 | { 480 | return map(value,0,1024,-260,419); 481 | } 482 | } 483 | } 484 | 485 | /// 486 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 487 | 488 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 489 | 490 | /// 491 | void AcceleroMMA7361::calibrate() 492 | { 493 | Serial.println(getOrientation()); 494 | Serial.print("\nCalibrating MMA7361011"); 495 | double var = 5000; 496 | double sumX = 0; 497 | double sumY = 0; 498 | double sumZ = 0; 499 | for (int i = 0;i 529 | /// getOrientation returns which axis perpendicular with the earths 530 | 531 | /// negative depending on which side of the axis is pointing downwards 532 | /// 533 | int AcceleroMMA7361::getOrientation() 534 | { 535 | int gemiddelde = 10; 536 | int x = 0; 537 | int y = 0; 538 | int z = 0; 539 | int xAbs = 0; 540 | int yAbs = 0; 541 | int zAbs = 0; 542 | for(int i = 0; i0) 558 | { 559 | return 1; 560 | } 561 | return -1; 562 | } 563 | if (yAbs0) 566 | { 567 | return 2; 568 | } 569 | return -2; 570 | } 571 | if (zAbs0) 574 | { 575 | return 3; 576 | } 577 | return -3; 578 | } 579 | return 0; 580 | } 581 | 582 | /// 583 | /// getTotalVector returns the magnitude of the total acceleration 584 | 585 | /// 586 | int AcceleroMMA7361::getTotalVector() 587 | { 588 | return sqrt(square(_mapMMA7361G(getXRaw())) + 589 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 590 | } 591 | 592 | 593 | AcceleroMMA7361 accelero; 594 | int x; 595 | int y; 596 | int z; 597 | 598 | void setup() 599 | { 600 | Serial.begin(9600); 601 | accelero.begin(13, 12, 11, 10, A0, A1, A2); // (int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 602 | // WAŻNE: podłączyć 3V3 do AREF'u!!!! 603 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 604 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 605 | accelero.calibrate(); 606 | } 607 | 608 | void loop() 609 | { 610 | x = accelero.getXAccel(); 611 | y = accelero.getYAccel(); 612 | z = accelero.getZAccel(); 613 | Serial.print("\nx: "); 614 | Serial.print(x); 615 | Serial.print(" \ty: "); 616 | Serial.print(y); 617 | Serial.print(" \tz: "); 618 | Serial.print(z); 619 | Serial.print("\tBLAH"); 620 | delay(500); //make it readable 621 | } 622 | -------------------------------------------------------------------------------- /sources/accelerometer_read_adaptation_uno/accelerometer_read_adaptation_uno.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************************************************** 2 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 3 | accelerometer. * 4 | * Copyright 2011 Jef Neefs (neefs@gmail.com) and Jeroen Doggen 5 | (jeroendoggen@gmail.com) * 6 | * DATASHEET: http://www.sparkfun.com/datasheets/Components/General/MMA7361L.pdf 7 | * 8 | **************************************************************************************************************** 9 | * Version History: 10 | * 11 | * Version 0.1: -get raw values 12 | * 13 | * Version 0.2: -get voltages and G forces 14 | * 15 | * Version 0.3: -removed begin parameters offset 16 | * 17 | * -added public function setOffSets(int,int,int) 18 | * 19 | * -added a private variable _offSets[3] containing the 20 | offset on each axis * 21 | * -changed long and double return values of private and 22 | public functions to int * 23 | * Version 0.4: -added calibrate 24 | * 25 | * Version 0.5: -added setARefVoltage 26 | * 27 | * -added setAveraging 28 | * 29 | * -added a default begin function 30 | * 31 | * Version 0.6: -added getAccelXYZ to get all axis in one call 32 | * 33 | * -added getTotalVector returns the magnitude of the 34 | total vector as an integer * 35 | * -added getOrientation returns which axis 36 | perpendicular with the earths surface x=1,y=2,z=3 * 37 | * is positive or negative depending on which side of 38 | the axis is pointing downwards * 39 | * Version 0.7: -added setSensitivity 40 | * 41 | * -added sleep & wake 42 | * 43 | * Version 0.8: -converted to Arduino 1.0 library 44 | * 45 | * -changed license to LGPL 46 | * 47 | * Roadmap: 48 | * 49 | * Version 0.x: auto zero calibration 50 | http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf 51 | * 52 | * Version 0.x: We asumed the output to be linear, it is nearly 53 | linear but not exectly... . * 54 | **************************************************************************************************************** 55 | * This library is free software; you can redistribute it and/or 56 | * 57 | * modify it under the terms of the GNU Lesser General Public 58 | * 59 | * License as published by the Free Software Foundation; either 60 | * 61 | * version 2.1 of the License, or (at your option) any later version. 62 | * 63 | * 64 | * 65 | * This library is distributed in the hope that it will be useful, 66 | * 67 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 68 | * 69 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 70 | * 71 | * Lesser General Public License for more details. 72 | * 73 | * 74 | * 75 | * You should have received a copy of the GNU Lesser General Public 76 | * 77 | * License along with this library; if not, write to the Free Software 78 | * 79 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 80 | 02110-1301 USA * 81 | ***************************************************************************************************************/ 82 | #ifndef AcceleroMMA7361_h 83 | #define AcceleroMMA7361_h 84 | #include 85 | 86 | class AcceleroMMA7361 87 | { 88 | public: 89 | AcceleroMMA7361(); 90 | void begin(); 91 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 92 | gSelectPin, int xPin, int yPin, int zPin); 93 | int getXRaw(); 94 | int getYRaw(); 95 | int getZRaw(); 96 | int getXVolt(); 97 | int getYVolt(); 98 | int getZVolt(); 99 | int getXAccel(); 100 | int getYAccel(); 101 | int getZAccel(); 102 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 103 | int getTotalVector(); 104 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 105 | void calibrate(); // only to be executed when Z-axis is oriented to 106 | 107 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 108 | void setARefVoltage(double _refV); 109 | void setAveraging(int avg); 110 | int getOrientation(); 111 | void setSensitivity(boolean sensi); 112 | void sleep(); 113 | void wake(); 114 | 115 | private: 116 | int _mapMMA7361V(int value); 117 | int _mapMMA7361G(int value); 118 | int _sleepPin; 119 | int _selfTestPin; 120 | int _zeroGPin; 121 | int _gSelectPin; 122 | int _xPin; 123 | int _yPin; 124 | int _zPin; 125 | int _offSets[3]; 126 | int _polarities[3]; 127 | double _refVoltage; 128 | int _average; 129 | boolean _sleep; 130 | boolean _sensi; 131 | }; 132 | #endif 133 | /**************************************************************************************** 134 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 135 | accelerometer. * 136 | * For more information: variable declaration, changelog,... see 137 | AcceleroMMA7361.h * 138 | **************************************************************************************** 139 | * This library is free software; you can redistribute it and/or 140 | * 141 | * modify it under the terms of the GNU Lesser General Public 142 | * 143 | * License as published by the Free Software Foundation; either 144 | * 145 | * version 2.1 of the License, or (at your option) any later version. 146 | * 147 | * 148 | * 149 | * This library is distributed in the hope that it will be useful, 150 | * 151 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 152 | * 153 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 154 | * 155 | * Lesser General Public License for more details. 156 | * 157 | * 158 | * 159 | * You should have received a copy of the GNU Lesser General Public 160 | * 161 | * License along with this library; if not, write to the Free Software 162 | * 163 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 164 | 02110-1301 USA * 165 | ***************************************************************************************/ 166 | /// 167 | /// acceleroMMA7361.cpp - Library for retrieving data from the MMA7361 168 | 169 | /// For more information: variable declaration, changelog,... see 170 | 171 | /// 172 | 173 | 174 | /// 175 | /// constructor 176 | /// 177 | AcceleroMMA7361::AcceleroMMA7361() 178 | { 179 | } 180 | 181 | /// 182 | /// begin function to set pins: sleepPin = 13, selfTestPin = 12, 183 | 184 | /// When you use begin() with an empty parameter list, these standard 185 | 186 | /// 187 | void AcceleroMMA7361::begin() 188 | { 189 | begin(13, 12, 11, 10, A0, A1, A2); 190 | } 191 | 192 | /// 193 | /// begin variables 194 | /// - int sleepPin: number indicating to which pin the sleep port is 195 | 196 | /// - int selfTestPin: number indicating to which pin the selftest 197 | 198 | /// - int zeroGPin: number indicating to which pin the ZeroGpin is 199 | 200 | /// - int gSelectPin: number indication to which pin the Gselect is 201 | 202 | /// - int xPin: number indicating to which pin the x-axis pin is 203 | 204 | /// - int yPin: number indicating to which pin the y-axis pin is 205 | 206 | /// - int zPin: number indicating to which pin the z-axis pin is 207 | 208 | /// - int offset: array indicating the G offset on the x,y and z-axis 209 | /// When you use begin() without variables standard values are loaded: 210 | 211 | 212 | /// 213 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 214 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 215 | { 216 | pinMode(sleepPin, OUTPUT); 217 | pinMode(selfTestPin, OUTPUT); 218 | pinMode(zeroGPin, INPUT); 219 | pinMode(gSelectPin, INPUT); 220 | pinMode(xPin, INPUT); 221 | pinMode(yPin, INPUT); 222 | pinMode(zPin, INPUT); 223 | digitalWrite(sleepPin,HIGH); 224 | digitalWrite(selfTestPin,LOW); 225 | _sleepPin = sleepPin; 226 | _selfTestPin = selfTestPin; 227 | _zeroGPin = zeroGPin; 228 | _gSelectPin = gSelectPin; 229 | _xPin = xPin; 230 | _yPin = yPin; 231 | _zPin = zPin; 232 | _sleep = false; 233 | setOffSets(0,0,0); 234 | setARefVoltage(5); 235 | setAveraging(10); 236 | setSensitivity(HIGH); 237 | } 238 | 239 | /// 240 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 241 | 242 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 243 | /// Offsets are added to the raw datafunctions 244 | /// 245 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 246 | { 247 | if (_refVoltage==3.3) 248 | { 249 | _offSets[0]= map(xOffSet,0,3300,0,1024); 250 | _offSets[1]= map(yOffSet,0,3300,0,1024); 251 | _offSets[2]= map(zOffSet,0,3300,0,1024); 252 | } 253 | else 254 | { 255 | _offSets[0]= map(xOffSet,0,5000,0,1024); 256 | _offSets[1]= map(yOffSet,0,5000,0,1024); 257 | _offSets[2]= map(zOffSet,0,5000,0,1024); 258 | } 259 | } 260 | 261 | /// 262 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 263 | 264 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 265 | 266 | /// 3.3 V VCC pin. This increases accuracy 267 | /// 268 | void AcceleroMMA7361::setARefVoltage(double refV) 269 | { 270 | _refVoltage = refV; 271 | if (refV == 3.3) 272 | { 273 | analogReference(EXTERNAL); 274 | } 275 | } 276 | 277 | /// 278 | /// setAveraging(int avg): Sets how many samples have to be averaged 279 | 280 | /// 281 | void AcceleroMMA7361::setAveraging(int avg) 282 | { 283 | _average = avg; 284 | } 285 | 286 | /// 287 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 288 | 289 | /// 290 | void AcceleroMMA7361::setSensitivity(boolean sensi) 291 | { 292 | _sensi = sensi; 293 | digitalWrite(_gSelectPin, !sensi); 294 | } 295 | 296 | /// 297 | /// sleep lets the device sleep (when device is sleeping already this 298 | 299 | /// 300 | void AcceleroMMA7361::sleep() 301 | { 302 | if (!_sleep) 303 | { 304 | digitalWrite(_sleepPin, LOW); 305 | _sleep = true; 306 | } 307 | } 308 | 309 | /// 310 | /// wake enables the device after sleep (when device is not sleeping 311 | 312 | 313 | /// 314 | void AcceleroMMA7361::wake() 315 | { 316 | if (_sleep == true) 317 | { 318 | digitalWrite(_sleepPin, HIGH); 319 | _sleep = false; 320 | delay(2); 321 | } 322 | } 323 | 324 | /// 325 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 326 | 327 | /// 328 | int AcceleroMMA7361::getXRaw() 329 | { 330 | return analogRead(_xPin)+_offSets[0]+2; 331 | } 332 | 333 | /// 334 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 335 | 336 | /// 337 | int AcceleroMMA7361::getYRaw() 338 | { 339 | return analogRead(_yPin)+_offSets[1]+2; 340 | } 341 | 342 | /// 343 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 344 | 345 | /// 346 | int AcceleroMMA7361::getZRaw() 347 | { 348 | return analogRead(_zPin)+_offSets[2]; 349 | } 350 | 351 | /// 352 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 353 | 354 | /// 355 | int AcceleroMMA7361::getXVolt() 356 | { 357 | return _mapMMA7361V(getXRaw()); 358 | } 359 | 360 | /// 361 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 362 | 363 | /// 364 | int AcceleroMMA7361::getYVolt() 365 | { 366 | return _mapMMA7361V(getYRaw()); 367 | } 368 | 369 | /// 370 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 371 | 372 | /// 373 | int AcceleroMMA7361::getZVolt() 374 | { 375 | return _mapMMA7361V(getZRaw()); 376 | } 377 | 378 | /// 379 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 380 | /// 381 | int AcceleroMMA7361::getXAccel() 382 | { 383 | int sum = 0; 384 | for (int i = 0;i<_average;i++) 385 | { 386 | sum = sum + _mapMMA7361G(getXRaw()); 387 | } 388 | return sum/_average; 389 | } 390 | 391 | /// 392 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 393 | /// 394 | int AcceleroMMA7361::getYAccel() 395 | { 396 | int sum = 0; 397 | for (int i = 0;i<_average;i++) 398 | { 399 | sum = sum + _mapMMA7361G(getYRaw()); 400 | } 401 | return sum/_average; 402 | } 403 | 404 | /// 405 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 406 | /// 407 | int AcceleroMMA7361::getZAccel() 408 | { 409 | int sum = 0; 410 | for (int i = 0;i<_average;i++) 411 | { 412 | sum = sum + _mapMMA7361G(getZRaw()); 413 | } 414 | return sum/_average; 415 | } 416 | 417 | /// 418 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 419 | 420 | /// 421 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 422 | { 423 | int sum[3]; 424 | sum[0] = 0; 425 | sum[1] = 0; 426 | sum[2] = 0; 427 | for (int i = 0;i<_average;i++) 428 | { 429 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 430 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 431 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 432 | } 433 | *_XAxis = sum[0]/_average; 434 | *_YAxis = sum[1]/_average; 435 | *_ZAxis = sum[2]/_average; 436 | } 437 | 438 | /// 439 | /// mapMMA7361V: calculates and returns the voltage value derived from 440 | 441 | /// 442 | int AcceleroMMA7361::_mapMMA7361V(int value) 443 | { 444 | if (_refVoltage==3.3) 445 | { 446 | return map(value,0,1024,0,3300); 447 | } 448 | else 449 | { 450 | return map(value,0,1024,0,5000); 451 | } 452 | } 453 | 454 | /// 455 | /// mapMMA7361G: calculates and returns the accelerometer value in 456 | 457 | 458 | /// 459 | int AcceleroMMA7361::_mapMMA7361G(int value) 460 | { 461 | if(_sensi == false) 462 | { 463 | if (_refVoltage==3.3) 464 | { 465 | return map(value,0,1024,-825,800); 466 | } 467 | else 468 | { 469 | return map(value,0,1024,-800,1600); 470 | } 471 | } 472 | else 473 | { 474 | if (_refVoltage==3.3) 475 | { 476 | return map(value,0,1024,-206,206); 477 | } 478 | else 479 | { 480 | return map(value,0,1024,-260,419); 481 | } 482 | } 483 | } 484 | 485 | /// 486 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 487 | 488 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 489 | 490 | /// 491 | void AcceleroMMA7361::calibrate() 492 | { 493 | Serial.println(getOrientation()); 494 | Serial.print("\nCalibrating MMA7361011"); 495 | double var = 5000; 496 | double sumX = 0; 497 | double sumY = 0; 498 | double sumZ = 0; 499 | for (int i = 0;i 529 | /// getOrientation returns which axis perpendicular with the earths 530 | 531 | /// negative depending on which side of the axis is pointing downwards 532 | /// 533 | int AcceleroMMA7361::getOrientation() 534 | { 535 | int gemiddelde = 10; 536 | int x = 0; 537 | int y = 0; 538 | int z = 0; 539 | int xAbs = 0; 540 | int yAbs = 0; 541 | int zAbs = 0; 542 | for(int i = 0; i0) 558 | { 559 | return 1; 560 | } 561 | return -1; 562 | } 563 | if (yAbs0) 566 | { 567 | return 2; 568 | } 569 | return -2; 570 | } 571 | if (zAbs0) 574 | { 575 | return 3; 576 | } 577 | return -3; 578 | } 579 | return 0; 580 | } 581 | 582 | /// 583 | /// getTotalVector returns the magnitude of the total acceleration 584 | 585 | /// 586 | int AcceleroMMA7361::getTotalVector() 587 | { 588 | return sqrt(square(_mapMMA7361G(getXRaw())) + 589 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 590 | } 591 | 592 | 593 | AcceleroMMA7361 accelero; 594 | int x; 595 | int y; 596 | int z; 597 | 598 | void setup() 599 | { 600 | Serial.begin(9600); 601 | accelero.begin(13, 12, 11, 10, A0, A1, A2); // (int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 602 | // WAŻNE: podłączyć 3V3 do AREF'u!!!! 603 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 604 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 605 | accelero.calibrate(); 606 | } 607 | 608 | void loop() 609 | { 610 | x = accelero.getXAccel(); 611 | y = accelero.getYAccel(); 612 | z = accelero.getZAccel(); 613 | Serial.print("\nx: "); 614 | Serial.print(x); 615 | Serial.print(" \ty: "); 616 | Serial.print(y); 617 | Serial.print(" \tz: "); 618 | Serial.print(z); 619 | Serial.print("\tBLAH"); 620 | delay(500); //make it readable 621 | } 622 | -------------------------------------------------------------------------------- /sources/accelerometer_read_uno.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************************************************** 2 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 3 | accelerometer. * 4 | * Copyright 2011 Jef Neefs (neefs@gmail.com) and Jeroen Doggen 5 | (jeroendoggen@gmail.com) * 6 | * DATASHEET: http://www.sparkfun.com/datasheets/Components/General/MMA7361L.pdf 7 | * 8 | **************************************************************************************************************** 9 | * Version History: 10 | * 11 | * Version 0.1: -get raw values 12 | * 13 | * Version 0.2: -get voltages and G forces 14 | * 15 | * Version 0.3: -removed begin parameters offset 16 | * 17 | * -added public function setOffSets(int,int,int) 18 | * 19 | * -added a private variable _offSets[3] containing the 20 | offset on each axis * 21 | * -changed long and double return values of private and 22 | public functions to int * 23 | * Version 0.4: -added calibrate 24 | * 25 | * Version 0.5: -added setARefVoltage 26 | * 27 | * -added setAveraging 28 | * 29 | * -added a default begin function 30 | * 31 | * Version 0.6: -added getAccelXYZ to get all axis in one call 32 | * 33 | * -added getTotalVector returns the magnitude of the 34 | total vector as an integer * 35 | * -added getOrientation returns which axis 36 | perpendicular with the earths surface x=1,y=2,z=3 * 37 | * is positive or negative depending on which side of 38 | the axis is pointing downwards * 39 | * Version 0.7: -added setSensitivity 40 | * 41 | * -added sleep & wake 42 | * 43 | * Version 0.8: -converted to Arduino 1.0 library 44 | * 45 | * -changed license to LGPL 46 | * 47 | * Roadmap: 48 | * 49 | * Version 0.x: auto zero calibration 50 | http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf 51 | * 52 | * Version 0.x: We asumed the output to be linear, it is nearly 53 | linear but not exectly... . * 54 | **************************************************************************************************************** 55 | * This library is free software; you can redistribute it and/or 56 | * 57 | * modify it under the terms of the GNU Lesser General Public 58 | * 59 | * License as published by the Free Software Foundation; either 60 | * 61 | * version 2.1 of the License, or (at your option) any later version. 62 | * 63 | * 64 | * 65 | * This library is distributed in the hope that it will be useful, 66 | * 67 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 68 | * 69 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 70 | * 71 | * Lesser General Public License for more details. 72 | * 73 | * 74 | * 75 | * You should have received a copy of the GNU Lesser General Public 76 | * 77 | * License along with this library; if not, write to the Free Software 78 | * 79 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 80 | 02110-1301 USA * 81 | ***************************************************************************************************************/ 82 | #ifndef AcceleroMMA7361_h 83 | #define AcceleroMMA7361_h 84 | #include 85 | 86 | class AcceleroMMA7361 87 | { 88 | public: 89 | AcceleroMMA7361(); 90 | void begin(); 91 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 92 | gSelectPin, int xPin, int yPin, int zPin); 93 | int getXRaw(); 94 | int getYRaw(); 95 | int getZRaw(); 96 | int getXVolt(); 97 | int getYVolt(); 98 | int getZVolt(); 99 | int getXAccel(); 100 | int getYAccel(); 101 | int getZAccel(); 102 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 103 | int getTotalVector(); 104 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 105 | void calibrate(); // only to be executed when Z-axis is oriented to 106 | 107 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 108 | void setARefVoltage(double _refV); 109 | void setAveraging(int avg); 110 | int getOrientation(); 111 | void setSensitivity(boolean sensi); 112 | void sleep(); 113 | void wake(); 114 | 115 | private: 116 | int _mapMMA7361V(int value); 117 | int _mapMMA7361G(int value); 118 | int _sleepPin; 119 | int _selfTestPin; 120 | int _zeroGPin; 121 | int _gSelectPin; 122 | int _xPin; 123 | int _yPin; 124 | int _zPin; 125 | int _offSets[3]; 126 | int _polarities[3]; 127 | double _refVoltage; 128 | int _average; 129 | boolean _sleep; 130 | boolean _sensi; 131 | }; 132 | #endif 133 | /**************************************************************************************** 134 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 135 | accelerometer. * 136 | * For more information: variable declaration, changelog,... see 137 | AcceleroMMA7361.h * 138 | **************************************************************************************** 139 | * This library is free software; you can redistribute it and/or 140 | * 141 | * modify it under the terms of the GNU Lesser General Public 142 | * 143 | * License as published by the Free Software Foundation; either 144 | * 145 | * version 2.1 of the License, or (at your option) any later version. 146 | * 147 | * 148 | * 149 | * This library is distributed in the hope that it will be useful, 150 | * 151 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 152 | * 153 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 154 | * 155 | * Lesser General Public License for more details. 156 | * 157 | * 158 | * 159 | * You should have received a copy of the GNU Lesser General Public 160 | * 161 | * License along with this library; if not, write to the Free Software 162 | * 163 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 164 | 02110-1301 USA * 165 | ***************************************************************************************/ 166 | /// 167 | /// acceleroMMA7361.cpp - Library for retrieving data from the MMA7361 168 | 169 | /// For more information: variable declaration, changelog,... see 170 | 171 | /// 172 | 173 | 174 | /// 175 | /// constructor 176 | /// 177 | AcceleroMMA7361::AcceleroMMA7361() 178 | { 179 | } 180 | 181 | /// 182 | /// begin function to set pins: sleepPin = 13, selfTestPin = 12, 183 | 184 | /// When you use begin() with an empty parameter list, these standard 185 | 186 | /// 187 | void AcceleroMMA7361::begin() 188 | { 189 | begin(13, 12, 11, 10, A0, A1, A2); 190 | } 191 | 192 | /// 193 | /// begin variables 194 | /// - int sleepPin: number indicating to which pin the sleep port is 195 | 196 | /// - int selfTestPin: number indicating to which pin the selftest 197 | 198 | /// - int zeroGPin: number indicating to which pin the ZeroGpin is 199 | 200 | /// - int gSelectPin: number indication to which pin the Gselect is 201 | 202 | /// - int xPin: number indicating to which pin the x-axis pin is 203 | 204 | /// - int yPin: number indicating to which pin the y-axis pin is 205 | 206 | /// - int zPin: number indicating to which pin the z-axis pin is 207 | 208 | /// - int offset: array indicating the G offset on the x,y and z-axis 209 | /// When you use begin() without variables standard values are loaded: 210 | 211 | 212 | /// 213 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 214 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 215 | { 216 | pinMode(sleepPin, OUTPUT); 217 | pinMode(selfTestPin, OUTPUT); 218 | pinMode(zeroGPin, INPUT); 219 | pinMode(gSelectPin, INPUT); 220 | pinMode(xPin, INPUT); 221 | pinMode(yPin, INPUT); 222 | pinMode(zPin, INPUT); 223 | digitalWrite(sleepPin,HIGH); 224 | digitalWrite(selfTestPin,LOW); 225 | _sleepPin = sleepPin; 226 | _selfTestPin = selfTestPin; 227 | _zeroGPin = zeroGPin; 228 | _gSelectPin = gSelectPin; 229 | _xPin = xPin; 230 | _yPin = yPin; 231 | _zPin = zPin; 232 | _sleep = false; 233 | setOffSets(0,0,0); 234 | setARefVoltage(5); 235 | setAveraging(10); 236 | setSensitivity(HIGH); 237 | } 238 | 239 | /// 240 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 241 | 242 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 243 | /// Offsets are added to the raw datafunctions 244 | /// 245 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 246 | { 247 | if (_refVoltage==3.3) 248 | { 249 | _offSets[0]= map(xOffSet,0,3300,0,1024); 250 | _offSets[1]= map(yOffSet,0,3300,0,1024); 251 | _offSets[2]= map(zOffSet,0,3300,0,1024); 252 | } 253 | else 254 | { 255 | _offSets[0]= map(xOffSet,0,5000,0,1024); 256 | _offSets[1]= map(yOffSet,0,5000,0,1024); 257 | _offSets[2]= map(zOffSet,0,5000,0,1024); 258 | } 259 | } 260 | 261 | /// 262 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 263 | 264 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 265 | 266 | /// 3.3 V VCC pin. This increases accuracy 267 | /// 268 | void AcceleroMMA7361::setARefVoltage(double refV) 269 | { 270 | _refVoltage = refV; 271 | if (refV == 3.3) 272 | { 273 | analogReference(EXTERNAL); 274 | } 275 | } 276 | 277 | /// 278 | /// setAveraging(int avg): Sets how many samples have to be averaged 279 | 280 | /// 281 | void AcceleroMMA7361::setAveraging(int avg) 282 | { 283 | _average = avg; 284 | } 285 | 286 | /// 287 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 288 | 289 | /// 290 | void AcceleroMMA7361::setSensitivity(boolean sensi) 291 | { 292 | _sensi = sensi; 293 | digitalWrite(_gSelectPin, !sensi); 294 | } 295 | 296 | /// 297 | /// sleep lets the device sleep (when device is sleeping already this 298 | 299 | /// 300 | void AcceleroMMA7361::sleep() 301 | { 302 | if (!_sleep) 303 | { 304 | digitalWrite(_sleepPin, LOW); 305 | _sleep = true; 306 | } 307 | } 308 | 309 | /// 310 | /// wake enables the device after sleep (when device is not sleeping 311 | 312 | 313 | /// 314 | void AcceleroMMA7361::wake() 315 | { 316 | if (_sleep == true) 317 | { 318 | digitalWrite(_sleepPin, HIGH); 319 | _sleep = false; 320 | delay(2); 321 | } 322 | } 323 | 324 | /// 325 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 326 | 327 | /// 328 | int AcceleroMMA7361::getXRaw() 329 | { 330 | return analogRead(_xPin)+_offSets[0]+2; 331 | } 332 | 333 | /// 334 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 335 | 336 | /// 337 | int AcceleroMMA7361::getYRaw() 338 | { 339 | return analogRead(_yPin)+_offSets[1]+2; 340 | } 341 | 342 | /// 343 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 344 | 345 | /// 346 | int AcceleroMMA7361::getZRaw() 347 | { 348 | return analogRead(_zPin)+_offSets[2]; 349 | } 350 | 351 | /// 352 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 353 | 354 | /// 355 | int AcceleroMMA7361::getXVolt() 356 | { 357 | return _mapMMA7361V(getXRaw()); 358 | } 359 | 360 | /// 361 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 362 | 363 | /// 364 | int AcceleroMMA7361::getYVolt() 365 | { 366 | return _mapMMA7361V(getYRaw()); 367 | } 368 | 369 | /// 370 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 371 | 372 | /// 373 | int AcceleroMMA7361::getZVolt() 374 | { 375 | return _mapMMA7361V(getZRaw()); 376 | } 377 | 378 | /// 379 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 380 | /// 381 | int AcceleroMMA7361::getXAccel() 382 | { 383 | int sum = 0; 384 | for (int i = 0;i<_average;i++) 385 | { 386 | sum = sum + _mapMMA7361G(getXRaw()); 387 | } 388 | return sum/_average; 389 | } 390 | 391 | /// 392 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 393 | /// 394 | int AcceleroMMA7361::getYAccel() 395 | { 396 | int sum = 0; 397 | for (int i = 0;i<_average;i++) 398 | { 399 | sum = sum + _mapMMA7361G(getYRaw()); 400 | } 401 | return sum/_average; 402 | } 403 | 404 | /// 405 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 406 | /// 407 | int AcceleroMMA7361::getZAccel() 408 | { 409 | int sum = 0; 410 | for (int i = 0;i<_average;i++) 411 | { 412 | sum = sum + _mapMMA7361G(getZRaw()); 413 | } 414 | return sum/_average; 415 | } 416 | 417 | /// 418 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 419 | 420 | /// 421 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 422 | { 423 | int sum[3]; 424 | sum[0] = 0; 425 | sum[1] = 0; 426 | sum[2] = 0; 427 | for (int i = 0;i<_average;i++) 428 | { 429 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 430 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 431 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 432 | } 433 | *_XAxis = sum[0]/_average; 434 | *_YAxis = sum[1]/_average; 435 | *_ZAxis = sum[2]/_average; 436 | } 437 | 438 | /// 439 | /// mapMMA7361V: calculates and returns the voltage value derived from 440 | 441 | /// 442 | int AcceleroMMA7361::_mapMMA7361V(int value) 443 | { 444 | if (_refVoltage==3.3) 445 | { 446 | return map(value,0,1024,0,3300); 447 | } 448 | else 449 | { 450 | return map(value,0,1024,0,5000); 451 | } 452 | } 453 | 454 | /// 455 | /// mapMMA7361G: calculates and returns the accelerometer value in 456 | 457 | 458 | /// 459 | int AcceleroMMA7361::_mapMMA7361G(int value) 460 | { 461 | if(_sensi == false) 462 | { 463 | if (_refVoltage==3.3) 464 | { 465 | return map(value,0,1024,-825,800); 466 | } 467 | else 468 | { 469 | return map(value,0,1024,-800,1600); 470 | } 471 | } 472 | else 473 | { 474 | if (_refVoltage==3.3) 475 | { 476 | return map(value,0,1024,-206,206); 477 | } 478 | else 479 | { 480 | return map(value,0,1024,-260,419); 481 | } 482 | } 483 | } 484 | 485 | /// 486 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 487 | 488 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 489 | 490 | /// 491 | void AcceleroMMA7361::calibrate() 492 | { 493 | Serial.println(getOrientation()); 494 | Serial.print("\nCalibrating MMA7361011"); 495 | double var = 5000; 496 | double sumX = 0; 497 | double sumY = 0; 498 | double sumZ = 0; 499 | for (int i = 0;i 529 | /// getOrientation returns which axis perpendicular with the earths 530 | 531 | /// negative depending on which side of the axis is pointing downwards 532 | /// 533 | int AcceleroMMA7361::getOrientation() 534 | { 535 | int gemiddelde = 10; 536 | int x = 0; 537 | int y = 0; 538 | int z = 0; 539 | int xAbs = 0; 540 | int yAbs = 0; 541 | int zAbs = 0; 542 | for(int i = 0; i0) 558 | { 559 | return 1; 560 | } 561 | return -1; 562 | } 563 | if (yAbs0) 566 | { 567 | return 2; 568 | } 569 | return -2; 570 | } 571 | if (zAbs0) 574 | { 575 | return 3; 576 | } 577 | return -3; 578 | } 579 | return 0; 580 | } 581 | 582 | /// 583 | /// getTotalVector returns the magnitude of the total acceleration 584 | 585 | /// 586 | int AcceleroMMA7361::getTotalVector() 587 | { 588 | return sqrt(square(_mapMMA7361G(getXRaw())) + 589 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 590 | } 591 | 592 | 593 | AcceleroMMA7361 accelero; 594 | int x; 595 | int y; 596 | int z; 597 | 598 | void setup() 599 | { 600 | Serial.begin(9600); 601 | accelero.begin(13, 12, 11, 10, A0, A1, A2); // (int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 602 | // WAŻNE: podłączyć 3V3 do AREF'u!!!! 603 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 604 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 605 | accelero.calibrate(); 606 | } 607 | 608 | void loop() 609 | { 610 | x = accelero.getXAccel(); 611 | y = accelero.getYAccel(); 612 | z = accelero.getZAccel(); 613 | Serial.print("\nx: "); 614 | Serial.print(x); 615 | Serial.print(" \ty: "); 616 | Serial.print(y); 617 | Serial.print(" \tz: "); 618 | Serial.print(z); 619 | Serial.print("\tBLAH"); 620 | delay(500); //make it readable 621 | } 622 | -------------------------------------------------------------------------------- /sources/accelerometer_xbee_midi_mega.ino: -------------------------------------------------------------------------------- 1 | /**************************************************************************************************************** 2 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 3 | accelerometer. * 4 | * Copyright 2011 Jef Neefs (neefs@gmail.com) and Jeroen Doggen 5 | (jeroendoggen@gmail.com) * 6 | * DATASHEET: http://www.sparkfun.com/datasheets/Components/General/MMA7361L.pdf 7 | * 8 | **************************************************************************************************************** 9 | * Version History: 10 | * 11 | * Version 0.1: -get raw values 12 | * 13 | * Version 0.2: -get voltages and G forces 14 | * 15 | * Version 0.3: -removed begin parameters offset 16 | * 17 | * -added public function setOffSets(int,int,int) 18 | * 19 | * -added a private variable _offSets[3] containing the 20 | offset on each axis * 21 | * -changed long and double return values of private and 22 | public functions to int * 23 | * Version 0.4: -added calibrate 24 | * 25 | * Version 0.5: -added setARefVoltage 26 | * 27 | * -added setAveraging 28 | * 29 | * -added a default begin function 30 | * 31 | * Version 0.6: -added getAccelXYZ to get all axis in one call 32 | * 33 | * -added getTotalVector returns the magnitude of the 34 | total vector as an integer * 35 | * -added getOrientation returns which axis 36 | perpendicular with the earths surface x=1,y=2,z=3 * 37 | * is positive or negative depending on which side of 38 | the axis is pointing downwards * 39 | * Version 0.7: -added setSensitivity 40 | * 41 | * -added sleep & wake 42 | * 43 | * Version 0.8: -converted to Arduino 1.0 library 44 | * 45 | * -changed license to LGPL 46 | * 47 | * Roadmap: 48 | * 49 | * Version 0.x: auto zero calibration 50 | http://www.freescale.com/files/sensors/doc/app_note/AN3447.pdf 51 | * 52 | * Version 0.x: We asumed the output to be linear, it is nearly 53 | linear but not exectly... . * 54 | **************************************************************************************************************** 55 | * This library is free software; you can redistribute it and/or 56 | * 57 | * modify it under the terms of the GNU Lesser General Public 58 | * 59 | * License as published by the Free Software Foundation; either 60 | * 61 | * version 2.1 of the License, or (at your option) any later version. 62 | * 63 | * 64 | * 65 | * This library is distributed in the hope that it will be useful, 66 | * 67 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 68 | * 69 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 70 | * 71 | * Lesser General Public License for more details. 72 | * 73 | * 74 | * 75 | * You should have received a copy of the GNU Lesser General Public 76 | * 77 | * License along with this library; if not, write to the Free Software 78 | * 79 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 80 | 02110-1301 USA * 81 | ***************************************************************************************************************/ 82 | #ifndef AcceleroMMA7361_h 83 | #define AcceleroMMA7361_h 84 | #include 85 | 86 | class AcceleroMMA7361 87 | { 88 | public: 89 | AcceleroMMA7361(); 90 | void begin(); 91 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 92 | gSelectPin, int xPin, int yPin, int zPin); 93 | int getXRaw(); 94 | int getYRaw(); 95 | int getZRaw(); 96 | int getXVolt(); 97 | int getYVolt(); 98 | int getZVolt(); 99 | int getXAccel(); 100 | int getYAccel(); 101 | int getZAccel(); 102 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 103 | int getTotalVector(); 104 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 105 | void calibrate(); // only to be executed when Z-axis is oriented to the ground 106 | 107 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 108 | void setARefVoltage(double _refV); 109 | void setAveraging(int avg); 110 | int getOrientation(); 111 | void setSensitivity(boolean sensi); 112 | void sleep(); 113 | void wake(); 114 | 115 | private: 116 | int _mapMMA7361V(int value); 117 | int _mapMMA7361G(int value); 118 | int _sleepPin; 119 | int _selfTestPin; 120 | int _zeroGPin; 121 | int _gSelectPin; 122 | int _xPin; 123 | int _yPin; 124 | int _zPin; 125 | int _offSets[3]; 126 | int _polarities[3]; 127 | double _refVoltage; 128 | int _average; 129 | boolean _sleep; 130 | boolean _sensi; 131 | }; 132 | #endif 133 | /**************************************************************************************** 134 | * acceleroMMA7361.h - Library for retrieving data from the MMA7361 135 | accelerometer. * 136 | * For more information: variable declaration, changelog,... see 137 | AcceleroMMA7361.h * 138 | **************************************************************************************** 139 | * This library is free software; you can redistribute it and/or 140 | * 141 | * modify it under the terms of the GNU Lesser General Public 142 | * 143 | * License as published by the Free Software Foundation; either 144 | * 145 | * version 2.1 of the License, or (at your option) any later version. 146 | * 147 | * 148 | * 149 | * This library is distributed in the hope that it will be useful, 150 | * 151 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 152 | * 153 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 154 | * 155 | * Lesser General Public License for more details. 156 | * 157 | * 158 | * 159 | * You should have received a copy of the GNU Lesser General Public 160 | * 161 | * License along with this library; if not, write to the Free Software 162 | * 163 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 164 | 02110-1301 USA * 165 | ***************************************************************************************/ 166 | /// 167 | /// acceleroMMA7361.cpp - Library for retrieving data from the MMA7361 accelerometer. 168 | 169 | /// For more information: variable declaration, changelog,... see AcceleroMMA7361.h 170 | 171 | /// 172 | 173 | 174 | /// 175 | /// constructor 176 | /// 177 | AcceleroMMA7361::AcceleroMMA7361() 178 | { 179 | } 180 | 181 | /// 182 | /// begin function to set pins: sleepPin = 13, selfTestPin = 12, 183 | /// zeroGPin = 11, gSelectPin = 10, xPin = A0, yPin = A1, zPin = A2. 184 | /// When you use begin() with an empty parameter list, these standard 185 | /// values are used 186 | /// 187 | void AcceleroMMA7361::begin() 188 | { 189 | begin(13, 12, 11, 10, A0, A1, A2); 190 | } 191 | 192 | /// 193 | /// begin variables 194 | /// - int sleepPin: number indicating to which pin the sleep port is 195 | /// attached. DIGITAL OUT 196 | /// - int selfTestPin: number indicating to which pin the selftest 197 | /// port is attached. DIGITAL OUT 198 | /// - int zeroGPin: number indicating to which pin the ZeroGpin is 199 | /// connected to. DIGITAL IN 200 | /// - int gSelectPin: number indication to which pin the Gselect is 201 | /// connected to. DIGITAL OUT 202 | /// - int xPin: number indicating to which pin the x-axis pin is 203 | /// connected to. ANALOG IN 204 | /// - int yPin: number indicating to which pin the y-axis pin is 205 | /// connected to. ANALOG IN 206 | /// - int zPin: number indicating to which pin the z-axis pin is 207 | /// connected to. ANALOG IN 208 | /// - int offset: array indicating the G offset on the x,y and z-axis 209 | /// When you use begin() without variables standard values are loaded: 210 | // A0,A1,A2 as input for X,Y,Z and digital pins 13,12,11,10 for sleep, 211 | /// selftest, zeroG and gSelect 212 | /// 213 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 214 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 215 | { 216 | pinMode(sleepPin, OUTPUT); 217 | pinMode(selfTestPin, OUTPUT); 218 | pinMode(zeroGPin, INPUT); 219 | pinMode(gSelectPin, INPUT); 220 | pinMode(xPin, INPUT); 221 | pinMode(yPin, INPUT); 222 | pinMode(zPin, INPUT); 223 | digitalWrite(sleepPin,HIGH); 224 | digitalWrite(selfTestPin,LOW); 225 | _sleepPin = sleepPin; 226 | _selfTestPin = selfTestPin; 227 | _zeroGPin = zeroGPin; 228 | _gSelectPin = gSelectPin; 229 | _xPin = xPin; 230 | _yPin = yPin; 231 | _zPin = zPin; 232 | _sleep = false; 233 | setOffSets(0,0,0); 234 | setARefVoltage(5); 235 | setAveraging(10); 236 | setSensitivity(HIGH); 237 | } 238 | 239 | /// 240 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 241 | /// offset values for the x,y,z axis. 242 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 243 | /// Offsets are added to the raw datafunctions 244 | /// 245 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 246 | { 247 | if (_refVoltage==3.3) 248 | { 249 | _offSets[0]= map(xOffSet,0,3300,0,1024); 250 | _offSets[1]= map(yOffSet,0,3300,0,1024); 251 | _offSets[2]= map(zOffSet,0,3300,0,1024); 252 | } 253 | else 254 | { 255 | _offSets[0]= map(xOffSet,0,5000,0,1024); 256 | _offSets[1]= map(yOffSet,0,5000,0,1024); 257 | _offSets[2]= map(zOffSet,0,5000,0,1024); 258 | } 259 | } 260 | 261 | /// 262 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 263 | /// (now only takes 3.3 or 5 as parameter) 264 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 265 | /// put a wire between the AREF pin and the 266 | /// 3.3 V VCC pin. This increases accuracy 267 | /// 268 | void AcceleroMMA7361::setARefVoltage(double refV) 269 | { 270 | _refVoltage = refV; 271 | if (refV == 3.3) 272 | { 273 | analogReference(EXTERNAL); 274 | } 275 | } 276 | 277 | /// 278 | /// setAveraging(int avg): Sets how many samples have to be averaged 279 | /// in getAccel default is 10. 280 | /// 281 | void AcceleroMMA7361::setAveraging(int avg) 282 | { 283 | _average = avg; 284 | } 285 | 286 | /// 287 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 288 | /// (LOW) using a boolean HIGH (1.5 G) or LOW (6 G) 289 | /// 290 | void AcceleroMMA7361::setSensitivity(boolean sensi) 291 | { 292 | _sensi = sensi; 293 | digitalWrite(_gSelectPin, !sensi); 294 | } 295 | 296 | /// 297 | /// sleep lets the device sleep (when device is sleeping already this 298 | /// does nothing) 299 | /// 300 | void AcceleroMMA7361::sleep() 301 | { 302 | if (!_sleep) 303 | { 304 | digitalWrite(_sleepPin, LOW); 305 | _sleep = true; 306 | } 307 | } 308 | 309 | /// 310 | /// wake enables the device after sleep (when device is not sleeping 311 | /// this does nothing) there is a 2 ms delay, due to enable response time 312 | /// (datasheet: typ 0.5 ms, max 2 ms) 313 | /// 314 | void AcceleroMMA7361::wake() 315 | { 316 | if (_sleep == true) 317 | { 318 | digitalWrite(_sleepPin, HIGH); 319 | _sleep = false; 320 | delay(2); 321 | } 322 | } 323 | 324 | /// 325 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 326 | /// the Arduino as an integer 327 | /// 328 | int AcceleroMMA7361::getXRaw() 329 | { 330 | return analogRead(_xPin)+_offSets[0]+2; 331 | } 332 | 333 | /// 334 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 335 | /// the Arduino as an integer 336 | /// 337 | int AcceleroMMA7361::getYRaw() 338 | { 339 | return analogRead(_yPin)+_offSets[1]+2; 340 | } 341 | 342 | /// 343 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 344 | /// the Arduino as an integer 345 | /// 346 | int AcceleroMMA7361::getZRaw() 347 | { 348 | return analogRead(_zPin)+_offSets[2]; 349 | } 350 | 351 | /// 352 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 353 | /// port of the Arduino as a integer 354 | /// 355 | int AcceleroMMA7361::getXVolt() 356 | { 357 | return _mapMMA7361V(getXRaw()); 358 | } 359 | 360 | /// 361 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 362 | /// port of the Arduino as a integer 363 | /// 364 | int AcceleroMMA7361::getYVolt() 365 | { 366 | return _mapMMA7361V(getYRaw()); 367 | } 368 | 369 | /// 370 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 371 | /// port of the Arduino as a integer 372 | /// 373 | int AcceleroMMA7361::getZVolt() 374 | { 375 | return _mapMMA7361V(getZRaw()); 376 | } 377 | 378 | /// 379 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 380 | /// 381 | int AcceleroMMA7361::getXAccel() 382 | { 383 | int sum = 0; 384 | for (int i = 0;i<_average;i++) 385 | { 386 | sum = sum + _mapMMA7361G(getXRaw()); 387 | } 388 | return sum/_average; 389 | } 390 | 391 | /// 392 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 393 | /// 394 | int AcceleroMMA7361::getYAccel() 395 | { 396 | int sum = 0; 397 | for (int i = 0;i<_average;i++) 398 | { 399 | sum = sum + _mapMMA7361G(getYRaw()); 400 | } 401 | return sum/_average; 402 | } 403 | 404 | /// 405 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 406 | /// 407 | int AcceleroMMA7361::getZAccel() 408 | { 409 | int sum = 0; 410 | for (int i = 0;i<_average;i++) 411 | { 412 | sum = sum + _mapMMA7361G(getZRaw()); 413 | } 414 | return sum/_average; 415 | } 416 | 417 | /// 418 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 419 | /// axis at once as pointers 420 | /// 421 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 422 | { 423 | int sum[3]; 424 | sum[0] = 0; 425 | sum[1] = 0; 426 | sum[2] = 0; 427 | for (int i = 0;i<_average;i++) 428 | { 429 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 430 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 431 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 432 | } 433 | *_XAxis = sum[0]/_average; 434 | *_YAxis = sum[1]/_average; 435 | *_ZAxis = sum[2]/_average; 436 | } 437 | 438 | /// 439 | /// mapMMA7361V: calculates and returns the voltage value derived from 440 | /// the raw data. Used in getXVoltage, getYVoltage, getZVoltage 441 | /// 442 | int AcceleroMMA7361::_mapMMA7361V(int value) 443 | { 444 | if (_refVoltage==3.3) 445 | { 446 | return map(value,0,1024,0,3300); 447 | } 448 | else 449 | { 450 | return map(value,0,1024,0,5000); 451 | } 452 | } 453 | 454 | /// 455 | /// mapMMA7361G: calculates and returns the accelerometer value in 456 | /// degrees derived from the raw data. Used in getXAccel, getYAccel, 457 | /// getZAccel 458 | /// 459 | int AcceleroMMA7361::_mapMMA7361G(int value) 460 | { 461 | if(_sensi == false) 462 | { 463 | if (_refVoltage==3.3) 464 | { 465 | return map(value,0,1024,-825,800); 466 | } 467 | else 468 | { 469 | return map(value,0,1024,-800,1600); 470 | } 471 | } 472 | else 473 | { 474 | if (_refVoltage==3.3) 475 | { 476 | return map(value,0,1024,-206,206); 477 | } 478 | else 479 | { 480 | return map(value,0,1024,-260,419); 481 | } 482 | } 483 | } 484 | 485 | /// 486 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 487 | /// axis will be set to 100 = 1G 488 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 489 | /// PERPENDICULAR WITH THE EARTHS SURFACE 490 | /// 491 | void AcceleroMMA7361::calibrate() 492 | { 493 | Serial.println(getOrientation()); 494 | Serial.print("\nCalibrating MMA7361011"); 495 | double var = 5000; 496 | double sumX = 0; 497 | double sumY = 0; 498 | double sumZ = 0; 499 | for (int i = 0;i 529 | /// getOrientation returns which axis perpendicular with the earths 530 | /// surface x=1,y=2,z=3 is positive or 531 | /// negative depending on which side of the axis is pointing downwards 532 | /// 533 | int AcceleroMMA7361::getOrientation() 534 | { 535 | int gemiddelde = 10; 536 | int x = 0; 537 | int y = 0; 538 | int z = 0; 539 | int xAbs = 0; 540 | int yAbs = 0; 541 | int zAbs = 0; 542 | for(int i = 0; i0) 558 | { 559 | return 1; 560 | } 561 | return -1; 562 | } 563 | if (yAbs0) 566 | { 567 | return 2; 568 | } 569 | return -2; 570 | } 571 | if (zAbs0) 574 | { 575 | return 3; 576 | } 577 | return -3; 578 | } 579 | return 0; 580 | } 581 | 582 | /// 583 | /// getTotalVector returns the magnitude of the total acceleration 584 | /// vector as an integer 585 | /// 586 | int AcceleroMMA7361::getTotalVector() 587 | { 588 | return sqrt(square(_mapMMA7361G(getXRaw())) + 589 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 590 | } 591 | 592 | 593 | AcceleroMMA7361 accelero; 594 | int x; 595 | int y; 596 | int z; 597 | 598 | 599 | void setup() 600 | { 601 | Serial1.begin(9600); 602 | Serial.begin(9600); 603 | accelero.begin(13, 12, 11, 10, A0, A1, A2); 604 | // void begin(int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 605 | // WA¯NE: do tego jeszcze pod³±czyæV3 do AREF'u!!!! 606 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 607 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 608 | accelero.calibrate(); 609 | } 610 | 611 | void loop() 612 | { 613 | x = accelero.getXAccel(); 614 | y = accelero.getYAccel(); 615 | z = accelero.getZAccel(); 616 | //Serial1.print("\nx: "); 617 | //Serial1.println(x); 618 | //Serial1.print(" \ty: "); 619 | //Serial1.println(y); 620 | //Serial1.print(" \tz: "); 621 | //Serial1.println(z); 622 | //Serial.print("\nx: "); 623 | //Serial.println(x); 624 | //Serial.print(" \ty: "); 625 | //Serial.println(y); 626 | //Serial.print(" \tz: "); 627 | //Serial.println(z); 628 | x=abs(x); 629 | noteOn(B0,16,x); 630 | delay(100); //make it readable 631 | 632 | } 633 | 634 | void noteOn(int cmd, int pitch, int velocity) { 635 | //Serial1.write(cmd); 636 | //Serial1.write(pitch); 637 | //Serial1.write(velocity); 638 | Serial.print(cmd); 639 | Serial.print(pitch); 640 | Serial.print(velocity); 641 | 642 | Serial1.write(0xB2); // MIDI control change; channel 3 643 | Serial1.write(1); // MIDI controller #1 644 | Serial1.write(x); // MIDI controller value of 127 645 | } 646 | -------------------------------------------------------------------------------- /sources/coordinator_receive_softserial_uno.ino: -------------------------------------------------------------------------------- 1 | #- piny na coordinatorze sa podlaczone DIN -> TX (pin 3) , DOUT->RX (pin 2) 2 | # 3 | #http://arduino.cc/hu/Reference/SoftwareSerial < manual softserial 4 | # 5 | #--------------------------------------------- 6 | #include 7 | 8 | SoftwareSerial xbee2(2, 3); // RX, TX 9 | 10 | void setup() 11 | { 12 | Serial.begin(9600); 13 | xbee2.begin(9600); 14 | } 15 | 16 | void loop() 17 | { 18 | if (xbee2.available()) 19 | Serial.write(xbee2.read()); 20 | } 21 | -------------------------------------------------------------------------------- /sources/coordinator_receive_softserial_uno/coordinator_receive_softserial_uno.ino: -------------------------------------------------------------------------------- 1 | #- piny na coordinatorze sa podlaczone DIN -> TX (pin 3) , DOUT->RX (pin 2) 2 | # 3 | #http://arduino.cc/hu/Reference/SoftwareSerial < manual softserial 4 | # 5 | #--------------------------------------------- 6 | #include 7 | 8 | SoftwareSerial xbee2(2, 3); // RX, TX 9 | 10 | void setup() 11 | { 12 | Serial.begin(9600); 13 | xbee2.begin(9600); 14 | } 15 | 16 | void loop() 17 | { 18 | if (xbee2.available()) 19 | Serial.write(xbee2.read()); 20 | } 21 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/encoder_controller.d: -------------------------------------------------------------------------------- 1 | encoder_controller.d encoder_controller.o: ../encoder_controller.cpp \ 2 | ../encoder_controller.h \ 3 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h \ 4 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/binary.h \ 5 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WCharacter.h \ 6 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h \ 7 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/HardwareSerial.h \ 8 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Stream.h \ 9 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h \ 10 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Printable.h \ 11 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/new.h \ 12 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/mega/pins_arduino.h 13 | 14 | ../encoder_controller.h: 15 | 16 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Arduino.h: 17 | 18 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/binary.h: 19 | 20 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WCharacter.h: 21 | 22 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/WString.h: 23 | 24 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/HardwareSerial.h: 25 | 26 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Stream.h: 27 | 28 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Print.h: 29 | 30 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/Printable.h: 31 | 32 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/new.h: 33 | 34 | /Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/mega/pins_arduino.h: 35 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/encoder_controller.eep: -------------------------------------------------------------------------------- 1 | :00000001FF 2 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/encoder_controller.elf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wzin/Arduino-Playground/efdbc3422c215d862269c7e7fba6ee9a61119f9d/sources/encoder_controller/Release/encoder_controller.elf -------------------------------------------------------------------------------- /sources/encoder_controller/Release/encoder_controller.hex: -------------------------------------------------------------------------------- 1 | :100000000C9403010C9430010C9430010C943001D9 2 | :100010000C9430010C9430010C9430010C9430019C 3 | :100020000C9430010C9430010C9430010C9430018C 4 | :100030000C9430010C9430010C9430010C9430017C 5 | :100040000C9430010C9430010C9430010C9430016C 6 | :100050000C9430010C9430010C9430010C94FC068B 7 | :100060000C9430010C9484010C94B0020C94300177 8 | :100070000C9430010C9430010C9430010C9430013C 9 | :100080000C9430010C9430010C9430010C9430012C 10 | :100090000C94BD010C94EF020C9430010C943001CF 11 | :1000A0000C9430010C9430010C9430010C9430010C 12 | :1000B0000C9430010C9430010C9430010C943001FC 13 | :1000C0000C9430010C9430010C9430010C94F60126 14 | :1000D0000C942E030C9430010C942F020C946D039D 15 | :1000E0000C94300100002100240027002A002D007C 16 | :1000F0003000330001010000040107010A01000083 17 | :100100002200250028002B002E00310034000201BF 18 | :100110000000050108010B0100002000230026005B 19 | :1001200029002C002F00320000010000030106010D 20 | :10013000090105050505070508080808020202026D 21 | :100140000A0A080804040404010101010101010173 22 | :100150000303030303030303040707070C0C0C0C3E 23 | :100160000C0C0C0C02020202060606060606060627 24 | :100170000B0B0B0B0B0B0B0B0102102020080810B4 25 | :100180002040102040800201020108040201010207 26 | :1001900004081020408080402010080402018004E0 27 | :1001A000020180402010080402010804020101023B 28 | :1001B0000408102040800102040810204080000044 29 | :1001C000090A02080B0C0D070603040100000000D9 30 | :1001D000000000000000000000000000000000001F 31 | :1001E0000000000000000000000011100F000000DF 32 | :1001F00000000000000000000000000000000000FF 33 | :1002000000000000D70411241FBECFEFD1E2DEBFF3 34 | :10021000CDBF00E00CBF12E0A0E0B2E0E4EAF1E103 35 | :1002200000E00BBF02C007900D92A631B107D9F7CD 36 | :100230001BBE14E0A6E1B2E001C01D92A13CB107D3 37 | :10024000E1F712E0C6E0D2E004C02297FE010E946E 38 | :10025000CC08C430D107C9F70E94ED060C94D00831 39 | :100260000C940000809102020E9459089C01909316 40 | :100270001B0280931A028091180290911902892B17 41 | :1002800031F52130310519F5809104020E94590899 42 | :100290002091160230911702892B19F42150304019 43 | :1002A00002C02F5F3F4F30931702209316026091D8 44 | :1002B0001602709117028CE394E04AE050E00E942D 45 | :1002C000D7068CE394E060E072E00E94EA06809139 46 | :1002D0001A0290911B0290931902809318020895BC 47 | :1002E0008091020260E00E94A6078091040260E013 48 | :1002F0000E94A6078CE394E040E855E260E070E0DD 49 | :100300000E94AC03089508951F920F920FB60F92AA 50 | :100310000BB60F9211242F933F934F938F939F937C 51 | :10032000EF93FF934091C60020915C0230915D02F3 52 | :100330002F5F3F4F2F73307080915E0290915F026C 53 | :100340002817390759F0E0915C02F0915D02E45EF4 54 | :10035000FD4F408330935D0220935C02FF91EF914B 55 | :100360009F918F914F913F912F910F900BBE0F90C6 56 | :100370000FBE0F901F90189508951F920F920FB601 57 | :100380000F920BB60F9211242F933F934F938F939D 58 | :100390009F93EF93FF934091CE002091E402309120 59 | :1003A000E5022F5F3F4F2F7330708091E6029091EE 60 | :1003B000E7022817390759F0E091E402F091E502CD 61 | :1003C000EC55FD4F40833093E5022093E402FF910A 62 | :1003D000EF919F918F914F913F912F910F900BBE75 63 | :1003E0000F900FBE0F901F90189508951F920F92B7 64 | :1003F0000FB60F920BB60F9211242F933F934F938A 65 | :100400008F939F93EF93FF934091D60020916C03BD 66 | :1004100030916D032F5F3F4F2F73307080916E03CB 67 | :1004200090916F032817390759F0E0916C03F09110 68 | :100430006D03E45DFC4F408330936D0320936C03A8 69 | :10044000FF91EF919F918F914F913F912F910F903D 70 | :100450000BBE0F900FBE0F901F90189508951F921E 71 | :100460000F920FB60F920BB60F9211242F933F935A 72 | :100470004F938F939F93EF93FF9340913601209179 73 | :10048000F4033091F5032F5F3F4F2F73307080914D 74 | :10049000F6039091F7032817390759F0E091F40318 75 | :1004A000F091F503EC54FC4F40833093F503209317 76 | :1004B000F403FF91EF919F918F914F913F912F9175 77 | :1004C0000F900BBE0F900FBE0F901F901895E091EC 78 | :1004D0004804F0914904E05CFF4F819191912081A3 79 | :1004E0003181821B930B8F739070892B11F00E94C6 80 | :1004F0008301E0916704F0916804E05CFF4F819113 81 | :10050000919120813181821B930B8F739070892B85 82 | :1005100011F00E94BC01E0918604F0918704E05C38 83 | :10052000FF4F8191919120813181821B930B8F73B9 84 | :100530009070892B11F00E94F501E091A504F091D3 85 | :10054000A604E05CFF4F8191919120813181821B53 86 | :10055000930B8F739070892B11F00E942E020895D7 87 | :100560001F920F920FB60F920BB60F9211242F937A 88 | :100570003F938F939F93EF93FF932091A00230912D 89 | :10058000A1028091A2029091A3022817390731F4A9 90 | :100590008091C1008F7D8093C10014C0E091A202C0 91 | :1005A000F091A302E05AFD4F20818091A202909128 92 | :1005B000A30201968F7390709093A3028093A2027E 93 | :1005C0002093C600FF91EF919F918F913F912F91C2 94 | :1005D0000F900BBE0F900FBE0F901F9018951F929B 95 | :1005E0000F920FB60F920BB60F9211242F933F93D9 96 | :1005F0008F939F93EF93FF932091280330912903CA 97 | :1006000080912A0390912B032817390731F48091A8 98 | :10061000C9008F7D8093C90014C0E0912A03F09136 99 | :100620002B03E851FD4F208180912A0390912B03E9 100 | :1006300001968F73907090932B0380932A032093DD 101 | :10064000CE00FF91EF919F918F913F912F910F904D 102 | :100650000BBE0F900FBE0F901F9018951F920F9218 103 | :100660000FB60F920BB60F9211242F933F938F93D7 104 | :100670009F93EF93FF932091B0033091B10380914A 105 | :10068000B2039091B3032817390731F48091D10058 106 | :100690008F7D8093D10014C0E091B203F091B30339 107 | :1006A000E059FC4F20818091B2039091B3030196F1 108 | :1006B0008F7390709093B3038093B2032093D6000E 109 | :1006C000FF91EF919F918F913F912F910F900BBED2 110 | :1006D0000F900FBE0F901F9018951F920F920FB69C 111 | :1006E0000F920BB60F9211242F933F938F939F93EA 112 | :1006F000EF93FF93209138043091390480913A04AC 113 | :1007000090913B042817390731F4809131018F7D96 114 | :100710008093310114C0E0913A04F0913B04E85019 115 | :10072000FC4F208180913A0490913B0401968F7395 116 | :10073000907090933B0480933A0420933601FF918C 117 | :10074000EF919F918F913F912F910F900BBE0F9042 118 | :100750000FBE0F901F901895AF92BF92DF92EF924D 119 | :10076000FF920F931F93CF93DF93EC017A018B01DC 120 | :10077000DD24403081EE580780E0680780E078078C 121 | :1007800011F0DD24D39491E0A92EB12CEC89FD89E0 122 | :10079000DD2069F0C5010E8C02C0880F991F0A94F4 123 | :1007A000E2F7808360E079E08DE390E005C010829D 124 | :1007B00060E874E88EE190E0A80197010E94A80823 125 | :1007C00021503040404050405695479537952795E9 126 | :1007D00080E12030380720F0DD2011F0DD24D6CF75 127 | :1007E000E889F9893083EA89FB892083EE89FF89CA 128 | :1007F000408121E030E0C9010A8C02C0880F991FB6 129 | :100800000A94E2F7482B4083EE89FF894081C901B1 130 | :100810000B8C02C0880F991F0A94E2F7482B408383 131 | :10082000EE89FF894081C9010C8C02C0880F991F95 132 | :100830000A94E2F7482B4083EE89FF8980810D8C72 133 | :1008400002C0220F331F0A94E2F720952823208349 134 | :10085000DF91CF911F910F91FF90EF90DF90BF90AC 135 | :10086000AF900895DC011C96ED91FC911D97E05C22 136 | :10087000FF4F2191319180819181281B390B2F737A 137 | :100880003070C9010895DC011C96ED91FC911D9713 138 | :10089000E05CFF4F20813181E054F040DF01AE5B2E 139 | :1008A000BF4F8D919C9111972817390719F42FEF9D 140 | :1008B0003FEF07C08D919C91E80FF91F8081282F91 141 | :1008C00030E0C9010895DC011C96ED91FC911D9763 142 | :1008D000E05CFF4F20813181E054F040DF01AE5BEE 143 | :1008E000BF4F8D919C9111972817390719F42FEF5D 144 | :1008F0003FEF10C08D919C911197E80FF91F208157 145 | :100900008D919C91119701968F73907011969C9385 146 | :100910008E9330E0C9010895FC0186859785DC013E 147 | :10092000A05CBF4FFC01EE5BFF4F2D913C911197F6 148 | :100930008081918128173907C1F70895CF93DF93FC 149 | :10094000EC01EE85FF85E05CFF4F20813181E054B2 150 | :10095000F0402F5F3F4F2F733070DF01AE5BBF4F12 151 | :100960008D919C91119728173907D1F3E05CFF4FC7 152 | :1009700080819181E054F040E80FF91F6083EE859B 153 | :10098000FF85E05CFF4F31832083EE89FF89208162 154 | :1009900081E090E00D8C02C0880F991F0A94E2F765 155 | :1009A000282B208381E090E0DF91CF9108951F9361 156 | :1009B00010923F0410923E0428EE33E040E050E0F5 157 | :1009C00020934004309341044093420450934304E5 158 | :1009D0006AE072E070933D0460933C048CE192E025 159 | :1009E000909349048093480480E692E090934B04EE 160 | :1009F00080934A0485EC90E090934D0480934C04DE 161 | :100A000084EC90E090934F0480934E0480EC90E04F 162 | :100A1000909351048093500481EC90E090935304A0 163 | :100A20008093520486EC90E0909355048093540494 164 | :100A300014E010935604B3E0B0935704A7E0A093DA 165 | :100A40005804F5E0F0935904E1E0E0935A04109261 166 | :100A50005E0410925D0420935F0430936004409321 167 | :100A600061045093620470935C0460935B0484EAB5 168 | :100A700092E0909368048093670488EE92E09093EC 169 | :100A80006A04809369048DEC90E090936C048093E9 170 | :100A90006B048CEC90E090936E0480936D0488EC72 171 | :100AA00090E09093700480936F0489EC90E09093B1 172 | :100AB0007204809371048EEC90E0909374048093A0 173 | :100AC000730410937504B0937604A0937704F093A5 174 | :100AD0007804E093790410927D0410927C042093B2 175 | :100AE0007E0430937F04409380045093810470937C 176 | :100AF0007B0460937A048CE293E090938704809364 177 | :100B0000860480E793E0909389048093880485EDC0 178 | :100B100090E090938B0480938A0484ED90E090930E 179 | :100B20008D0480938C0480ED90E090938F048093EB 180 | :100B30008E0481ED90E0909391048093900486ED73 181 | :100B400090E0909393048093920410939404B09354 182 | :100B50009504A0939604F0939704E0939804109260 183 | :100B60009C0410929B0420939D0430939E04409318 184 | :100B70009F045093A00470939A046093990484EBAB 185 | :100B800093E09093A6048093A50488EF93E090935C 186 | :100B9000A8048093A70485E391E09093AA0480932E 187 | :100BA000A90484E391E09093AC048093AB0480E3C8 188 | :100BB00091E09093AE048093AD0481E391E0909333 189 | :100BC000B0048093AF0486E391E09093B2048093E5 190 | :100BD000B1041093B304B093B404A093B504F0939C 191 | :100BE000B604E093B7041F910895CF92DF92EF927D 192 | :100BF000FF920F931F93CF93DF937C016B018A01C8 193 | :100C0000C0E0D0E00FC0D6016D916D01D701ED912C 194 | :100C1000FC910190F081E02DC7011995C80FD91FF3 195 | :100C2000015010400115110571F7CE01DF91CF91F0 196 | :100C30001F910F91FF90EF90DF90CF900895CF9389 197 | :100C4000DF93DB010D900020E9F71197A61BB70B8E 198 | :100C5000EC01E881F9810280F381E02DAD01199565 199 | :100C6000DF91CF9108954F925F927F928F929F92E2 200 | :100C7000AF92BF92CF92DF92EF92FF920F931F93AA 201 | :100C8000DF93CF93CDB7DEB7A1970FB6F894DEBF51 202 | :100C90000FBECDBF2C01742ECB01223008F42AE008 203 | :100CA00019A231E2C32ED12CCC0EDD1E822E992446 204 | :100CB000AA24BB24672D752FA50194010E94A808C2 205 | :100CC00079018A01C801B701A50194010E94890830 206 | :100CD000472D461B0894C108D1084A3014F4405DE2 207 | :100CE00001C0495CF6014083E114F10401051105DE 208 | :100CF00021F07E2C5F2DC801DDCFC201B6010E941C 209 | :100D00001F06A1960FB6F894DEBF0FBECDBFCF91E0 210 | :100D1000DF911F910F91FF90EF90DF90CF90BF90E8 211 | :100D2000AF909F908F907F905F904F900895CF925B 212 | :100D3000DF92EF92FF920F931F93CF93DF93EC011B 213 | :100D40006A017B012115310541F4E881F9810190A7 214 | :100D5000F081E02D642F19951FC02A303105D1F4A0 215 | :100D600077FF17C0E881F9810190F081E02D6DE2F5 216 | :100D700019958C0144275527BA014C195D096E0954 217 | :100D80007F09CE012AE00E9433069801280F391FFF 218 | :100D900004C02AE00E9433069C01C901DF91CF9173 219 | :100DA0001F910F91FF90EF90DF90CF900895EF92F9 220 | :100DB000FF920F931F937B019A010027F7FC009588 221 | :100DC000102FB801A7010E9497061F910F91FF9065 222 | :100DD000EF9008950E941F060895CF93DF930E941D 223 | :100DE00044070E947001C7E6D2E00E9432012097BA 224 | :100DF000E1F30E946702F9CF1F920F920FB60F9294 225 | :100E000011242F933F938F939F93AF93BF93809120 226 | :100E1000BC049091BD04A091BE04B091BF04309178 227 | :100E2000C0040196A11DB11D232F2D5F2D3720F089 228 | :100E30002D570196A11DB11D2093C0048093BC04C1 229 | :100E40009093BD04A093BE04B093BF048091B804F6 230 | :100E50009091B904A091BA04B091BB040196A11D70 231 | :100E6000B11D8093B8049093B904A093BA04B093D1 232 | :100E7000BB04BF91AF919F918F913F912F910F90A4 233 | :100E80000FBE0F901F901895789484B5826084BD32 234 | :100E900084B5816084BD85B5826085BD85B581607E 235 | :100EA00085BDEEE6F0E0808181608083E1E8F0E0DE 236 | :100EB0001082808182608083808181608083E0E80D 237 | :100EC000F0E0808181608083E1EBF0E080818460EC 238 | :100ED0008083E0EBF0E0808181608083E1E9F0E0F5 239 | :100EE000808182608083808181608083E0E9F0E09E 240 | :100EF000808181608083E1EAF0E08081826080838C 241 | :100F0000808181608083E0EAF0E08081816080837D 242 | :100F1000E1E2F1E080818260808380818160808372 243 | :100F2000E0E2F1E0808181608083EAE7F0E08081A7 244 | :100F300084608083808182608083808181608083FF 245 | :100F40008081806880831092C1000895CF93DF93E1 246 | :100F5000482F50E0CA0188589E4FFC0134914E5CE6 247 | :100F60005E4FFA018491882369F190E0880F991F00 248 | :100F7000FC01EC51FF4FA591B491FC01E250FF4FF1 249 | :100F8000C591D491662351F42FB7F8948C91932F87 250 | :100F9000909589238C93888189230BC0623061F4FA 251 | :100FA0002FB7F8948C91932F909589238C938881F7 252 | :100FB000832B88832FBF06C09FB7F8948C91832B17 253 | :100FC0008C939FBFDF91CF910895893009F449C078 254 | :100FD0008A30A0F4843051F1853040F4823079F1C8 255 | :100FE000833000F5813009F063C026C0873079F185 256 | :100FF000883098F5863009F05BC025C08D3009F443 257 | :1010000042C08E3028F48B30B1F18C30C0F52DC049 258 | :10101000803109F442C0813109F445C08F3009F0B4 259 | :1010200047C037C0809180008F7703C080918000D7 260 | :101030008F7D80938000089584B58F7702C084B53A 261 | :101040008F7D84BD08958091B0008F7703C080911B 262 | :10105000B0008F7D8093B0000895809190008F77CD 263 | :1010600007C0809190008F7D03C080919000877FA2 264 | :101070008093900008958091A0008F7707C08091A1 265 | :10108000A0008F7D03C08091A000877F8093A00087 266 | :101090000895809120018F7703C0809120018F7D7A 267 | :1010A00080932001089580912001877F8093200103 268 | :1010B00008950F931F93482F50E0CA0182549E4F0A 269 | :1010C000FC012491CA0188589E4FFC0104914E5C9A 270 | :1010D0005E4FFA011491112319F420E030E015C09D 271 | :1010E000222319F0822F0E94E507812F90E0880FBC 272 | :1010F000991F885E9E4FFC01A591B4918C9120E0D0 273 | :1011000030E0802311F021E030E0C9011F910F9100 274 | :101110000895629FD001739FF001829FE00DF11D41 275 | :10112000649FE00DF11D929FF00D839FF00D749F61 276 | :10113000F00D659FF00D9927729FB00DE11DF91F0D 277 | :10114000639FB00DE11DF91FBD01CF01112408956A 278 | :10115000A1E21A2EAA1BBB1BFD010DC0AA1FBB1FBB 279 | :10116000EE1FFF1FA217B307E407F50720F0A21B2D 280 | :10117000B30BE40BF50B661F771F881F991F1A949A 281 | :1011800069F760957095809590959B01AC01BD01C4 282 | :10119000CF010895EE0FFF1F0590F491E02D1994F3 283 | :0411A000F894FFCFF1 284 | :1011A4002F001F002100000000009E04F5053204FA 285 | :0611B400630443048C04F7 286 | :00000001FF 287 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | -include ../makefile.init 6 | 7 | RM := rm -rf 8 | 9 | # All of the sources participating in the build are defined here 10 | -include sources.mk 11 | -include subdir.mk 12 | -include objects.mk 13 | 14 | ifneq ($(MAKECMDGOALS),clean) 15 | ifneq ($(strip $(C++_DEPS)),) 16 | -include $(C++_DEPS) 17 | endif 18 | ifneq ($(strip $(C_DEPS)),) 19 | -include $(C_DEPS) 20 | endif 21 | ifneq ($(strip $(ASM_DEPS)),) 22 | -include $(ASM_DEPS) 23 | endif 24 | ifneq ($(strip $(CC_DEPS)),) 25 | -include $(CC_DEPS) 26 | endif 27 | ifneq ($(strip $(CPP_DEPS)),) 28 | -include $(CPP_DEPS) 29 | endif 30 | ifneq ($(strip $(S_DEPS)),) 31 | -include $(S_DEPS) 32 | endif 33 | ifneq ($(strip $(CXX_DEPS)),) 34 | -include $(CXX_DEPS) 35 | endif 36 | ifneq ($(strip $(C_UPPER_DEPS)),) 37 | -include $(C_UPPER_DEPS) 38 | endif 39 | ifneq ($(strip $(S_UPPER_DEPS)),) 40 | -include $(S_UPPER_DEPS) 41 | endif 42 | endif 43 | 44 | -include ../makefile.defs 45 | 46 | # Add inputs and outputs from these tool invocations to the build variables 47 | LSS += \ 48 | encoder_controller.lss \ 49 | 50 | FLASH_IMAGE += \ 51 | encoder_controller.hex \ 52 | 53 | EEPROM_IMAGE += \ 54 | encoder_controller.eep \ 55 | 56 | SIZEDUMMY += \ 57 | sizedummy \ 58 | 59 | 60 | # All Target 61 | all: encoder_controller.elf secondary-outputs 62 | 63 | # Tool invocations 64 | encoder_controller.elf: $(OBJS) $(USER_OBJS) 65 | @echo 'Building target: $@' 66 | @echo 'Invoking: AVR C++ Linker' 67 | avr-gcc -Os -Wl,--gc-sections -L"/Users/wojtek/Dropbox/Development/eclipse_workspace/Arduino_Mega_2560_or_Mega_ADK/Release" -mmcu=atmega2560 -o"encoder_controller.elf" $(OBJS) $(USER_OBJS) $(LIBS) -lm 68 | @echo 'Finished building target: $@' 69 | @echo ' ' 70 | 71 | encoder_controller.lss: encoder_controller.elf 72 | @echo 'Invoking: AVR Create Extended Listing' 73 | -avr-objdump -h -S encoder_controller.elf >"encoder_controller.lss" 74 | @echo 'Finished building: $@' 75 | @echo ' ' 76 | 77 | encoder_controller.hex: encoder_controller.elf 78 | @echo 'Create Flash image (ihex format)' 79 | -avr-objcopy -R .eeprom -O ihex encoder_controller.elf "encoder_controller.hex" 80 | @echo 'Finished building: $@' 81 | @echo ' ' 82 | 83 | encoder_controller.eep: encoder_controller.elf 84 | @echo 'Create eeprom image (ihex format)' 85 | -avr-objcopy -j .eeprom --no-change-warnings --change-section-lma .eeprom=0 -O ihex encoder_controller.elf "encoder_controller.eep" 86 | @echo 'Finished building: $@' 87 | @echo ' ' 88 | 89 | sizedummy: encoder_controller.elf 90 | @echo 'Invoking: Print Size' 91 | -avr-size --format=avr --mcu=atmega2560 encoder_controller.elf 92 | @echo 'Finished building: $@' 93 | @echo ' ' 94 | 95 | # Other Targets 96 | clean: 97 | -$(RM) $(OBJS)$(C_DEPS)$(EEPROM_IMAGE)$(ELFS)$(FLASH_IMAGE)$(LSS)$(S_DEPS)$(CXX_DEPS)$(S_UPPER_DEPS)$(C++_DEPS)$(ASM_DEPS)$(CC_DEPS)$(CPP_DEPS)$(C_UPPER_DEPS)$(SIZEDUMMY) encoder_controller.elf 98 | -@echo ' ' 99 | 100 | secondary-outputs: $(LSS) $(FLASH_IMAGE) $(EEPROM_IMAGE) $(SIZEDUMMY) 101 | 102 | .PHONY: all clean dependents 103 | .SECONDARY: 104 | 105 | -include ../makefile.targets 106 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/objects.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | USER_OBJS := 6 | 7 | LIBS := -l"Arduino_Mega_2560_or_Mega_ADK" 8 | 9 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/sources.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | O_SRCS := 6 | CPP_SRCS := 7 | C_UPPER_SRCS := 8 | C_SRCS := 9 | S_UPPER_SRCS := 10 | S_SRCS := 11 | OBJ_SRCS := 12 | ASM_SRCS := 13 | CXX_SRCS := 14 | C++_SRCS := 15 | CC_SRCS := 16 | OBJS := 17 | C_DEPS := 18 | EEPROM_IMAGE := 19 | ELFS := 20 | FLASH_IMAGE := 21 | LSS := 22 | S_DEPS := 23 | CXX_DEPS := 24 | S_UPPER_DEPS := 25 | C++_DEPS := 26 | ASM_DEPS := 27 | CC_DEPS := 28 | CPP_DEPS := 29 | C_UPPER_DEPS := 30 | SIZEDUMMY := 31 | 32 | # Every subdirectory with source files must be described here 33 | SUBDIRS := \ 34 | . \ 35 | 36 | -------------------------------------------------------------------------------- /sources/encoder_controller/Release/subdir.mk: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Automatically-generated file. Do not edit! 3 | ################################################################################ 4 | 5 | # Add inputs and outputs from these tool invocations to the build variables 6 | CPP_SRCS += \ 7 | ../encoder_controller.cpp 8 | 9 | OBJS += \ 10 | ./encoder_controller.o 11 | 12 | CPP_DEPS += \ 13 | ./encoder_controller.d 14 | 15 | 16 | # Each subdirectory must supply rules for building sources it contributes 17 | %.o: ../%.cpp 18 | @echo 'Building file: $<' 19 | @echo 'Invoking: AVR C++ Compiler' 20 | avr-g++ -I"/Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino" -I"/Users/wojtek/Downloads/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/mega" -I"/Users/wojtek/Google Drive/Atominho/Arduino-Playground/sources/encoder_controller" -D__IN_ECLIPSE__=1 -DUSB_VID= -DUSB_PID= -DARDUINO=101 -Wall -Os -ffunction-sections -fdata-sections -fno-exceptions -g -mmcu=atmega2560 -DF_CPU=16000000UL -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -c -o "$@" -x c++ "$<" 21 | @echo 'Finished building: $<' 22 | @echo ' ' 23 | 24 | 25 | -------------------------------------------------------------------------------- /sources/encoder_controller/encoder_controller.cpp: -------------------------------------------------------------------------------- 1 | // Do not remove the include below 2 | #include "encoder_controller.h" 3 | 4 | int val; 5 | int encoder0PinA = 31; 6 | int encoder0PinB = 33; 7 | int encoder0Pos = 0; 8 | int encoder0PinALast = LOW; 9 | int n = LOW; 10 | 11 | void setup() { 12 | pinMode (encoder0PinA,INPUT); 13 | pinMode (encoder0PinB,INPUT); 14 | Serial.begin (9600); 15 | } 16 | 17 | void loop() { 18 | n = digitalRead(encoder0PinA); 19 | if ((encoder0PinALast == LOW) && (n == HIGH)) { 20 | if (digitalRead(encoder0PinB) == LOW) { 21 | encoder0Pos--; 22 | } else { 23 | encoder0Pos++; 24 | } 25 | Serial.print (encoder0Pos); 26 | Serial.print ("/"); 27 | } 28 | encoder0PinALast = n; 29 | } 30 | -------------------------------------------------------------------------------- /sources/encoder_controller/encoder_controller.h: -------------------------------------------------------------------------------- 1 | // Only modify this file to include 2 | // - function definitions (prototypes) 3 | // - include files 4 | // - extern variable definitions 5 | // In the appropriate section 6 | 7 | #ifndef encoder_controller_H_ 8 | #define encoder_controller_H_ 9 | #include "Arduino.h" 10 | //add your includes for the project encoder_controller here 11 | 12 | 13 | //end of add your includes here 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | void loop(); 18 | void setup(); 19 | #ifdef __cplusplus 20 | } // extern "C" 21 | #endif 22 | 23 | //add your function definitions for the project encoder_controller here 24 | 25 | 26 | 27 | 28 | //Do not add code below this line 29 | #endif /* encoder_controller_H_ */ 30 | -------------------------------------------------------------------------------- /sources/knock_sensor_uno/knock_sensor_uno.ino: -------------------------------------------------------------------------------- 1 | 2 | int ledPin = 13; 3 | int knockSensor = 0; 4 | byte val = 0; 5 | int statePin = LOW; 6 | int THRESHOLD = 100; 7 | 8 | void setup() { 9 | pinMode(ledPin, OUTPUT); 10 | beginSerial(9600); 11 | } 12 | 13 | void loop() { 14 | val = analogRead(knockSensor); 15 | if (val >= THRESHOLD) { 16 | statePin = !statePin; 17 | digitalWrite(ledPin, statePin); 18 | printString("Knock!"); 19 | printByte(10); 20 | printByte(13); 21 | } 22 | delay(100); // we have to make a delay to avoid overloading the serial port 23 | } 24 | 25 | 26 | int knockSensor = 0 27 | void setup() { 28 | 29 | -------------------------------------------------------------------------------- /sources/multiplexing_analog_potentiometers_mega/multiplexing_analog_potentiometers_mega.ino: -------------------------------------------------------------------------------- 1 | //Mux control pins 2 | int s0 = 8; 3 | int s1 = 9; 4 | int s2 = 10; 5 | int s3 = 11; 6 | //State array 7 | int portState[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 8 | int currentState ; 9 | //Mux in "SIG" pin 10 | int SIG_pin = 0; 11 | 12 | void setup(){ 13 | pinMode(s0, OUTPUT); 14 | pinMode(s1, OUTPUT); 15 | pinMode(s2, OUTPUT); 16 | pinMode(s3, OUTPUT); 17 | 18 | digitalWrite(s0, LOW); 19 | digitalWrite(s1, LOW); 20 | digitalWrite(s2, LOW); 21 | digitalWrite(s3, LOW); 22 | 23 | Serial.begin(115200); 24 | } 25 | 26 | void loop(){ 27 | 28 | //Loop through and read all 16 values 29 | //Reports back Value at channel 6 is: 346 30 | for(int i = 0; i < 16; i ++){ 31 | currentState = readMux(i); 32 | if (abs(portState[i] - currentState) > 2){ 33 | Serial.print("Value at channel "); 34 | Serial.print(i); 35 | Serial.print("is : "); 36 | Serial.println(currentState); 37 | saveState(i,currentState); 38 | } 39 | delay(1); 40 | } 41 | 42 | } 43 | 44 | void saveState(int port,int state) { 45 | portState[port] = state; 46 | } 47 | 48 | int readMux(int channel){ 49 | int controlPin[] = {s0, s1, s2, s3}; 50 | 51 | int muxChannel[16][4]={ 52 | {0,0,0,0}, //channel 0 53 | {1,0,0,0}, //channel 1 54 | {0,1,0,0}, //channel 2 55 | {1,1,0,0}, //channel 3 56 | {0,0,1,0}, //channel 4 57 | {1,0,1,0}, //channel 5 58 | {0,1,1,0}, //channel 6 59 | {1,1,1,0}, //channel 7 60 | {0,0,0,1}, //channel 8 61 | {1,0,0,1}, //channel 9 62 | {0,1,0,1}, //channel 10 63 | {1,1,0,1}, //channel 11 64 | {0,0,1,1}, //channel 12 65 | {1,0,1,1}, //channel 13 66 | {0,1,1,1}, //channel 14 67 | {1,1,1,1} //channel 15 68 | }; 69 | 70 | //loop through the 4 sig 71 | for(int i = 0; i < 4; i ++){ 72 | digitalWrite(controlPin[i], muxChannel[channel][i]); 73 | } 74 | 75 | //read the value at the SIG pin 76 | int val = analogRead(SIG_pin); 77 | 78 | //return the value 79 | return val; 80 | } 81 | -------------------------------------------------------------------------------- /sources/overdrive_send_midi_via_xbee_uno.ino: -------------------------------------------------------------------------------- 1 | #include 2 | SoftwareSerial xbee2(4, 3); // RX, TX 3 | int inPin = 2; // the number of the input pin 4 | int outPin = 13; // the number of the output pin 5 | int state = HIGH; // the current state of the output pin 6 | int previous_state ; 7 | int reading; // the current reading from the input pin 8 | int previous = LOW; // the previous reading from the input pin 9 | // the follow variables are long's because the time, measured in miliseconds, 10 | // will quickly become a bigger number than can be stored in an int. 11 | long time = 0; // the last time the output pin was toggled 12 | long debounce = 200; // the debounce time, increase if the output flickers 13 | void setup() 14 | { 15 | pinMode(inPin, INPUT); 16 | pinMode(outPin, OUTPUT); 17 | xbee2.begin(9600); 18 | Serial.begin(9600); 19 | } 20 | void loop() 21 | { 22 | reading = digitalRead(inPin); 23 | // if the input just went from LOW and HIGH and we've waited long enough 24 | // to ignore any noise on the circuit, toggle the output pin and remember 25 | // the time 26 | if (reading == HIGH && previous == LOW && millis() - time > debounce) { 27 | if (state == HIGH) 28 | state = LOW; 29 | else 30 | state = HIGH; 31 | time = millis(); 32 | } 33 | 34 | digitalWrite(outPin, state); 35 | if (reading != previous) { 36 | if (state == HIGH) 37 | { 38 | overdrive_on(); 39 | } 40 | else { 41 | overdrive_off(); 42 | } 43 | } 44 | previous = reading; 45 | delay(100); 46 | } 47 | void overdrive_on(){ 48 | xbee2.write(0xB0); // MIDI control change; channel 3 49 | xbee2.write(0x29); // MIDI controller #1 50 | xbee2.write((byte)0x00); // MIDI controller value of 127 51 | Serial.println("on"); 52 | } 53 | 54 | void overdrive_off(){ 55 | xbee2.write(0xB0); // MIDI control change; channel 3 56 | xbee2.write(0x29); // MIDI controller #1 57 | xbee2.write(0x7F); // MIDI controller value of 127 58 | Serial.println("off"); 59 | } 60 | -------------------------------------------------------------------------------- /sources/rotary_encoder_test_uno.ino: -------------------------------------------------------------------------------- 1 | # encoder(PIN A) -> A0 2 | # encoder(PIN B) -> A1 3 | # *PIN A and B are located on the both sides of encoder 4 | # encoder(PIN MIDDLE) -> ground 5 | #/* Rotary encoder read example */ 6 | #define ENC_A 14 7 | #define ENC_B 15 8 | #define ENC_PORT PINC 9 | int magicnumber = 2; 10 | 11 | void setup() 12 | { 13 | /* Setup encoder pins as inputs */ 14 | pinMode(ENC_A, INPUT); 15 | digitalWrite(ENC_A, HIGH); 16 | pinMode(ENC_B, INPUT); 17 | digitalWrite(ENC_B, HIGH); 18 | Serial.begin (115200); 19 | Serial.println("Start"); 20 | } 21 | 22 | void loop() 23 | { 24 | static uint8_t counter = 0; //this variable will be changed by encoder input 25 | int8_t tmpdata; 26 | /**/ 27 | tmpdata = read_encoder(); 28 | if( tmpdata ) { 29 | switch (counter) { 30 | case 0: 31 | if (tmpdata == +1 ) { 32 | Serial.print("Counter value: "); 33 | Serial.println(counter, DEC); 34 | counter += tmpdata*magicnumber; 35 | break; 36 | } 37 | if (tmpdata == -1) { 38 | Serial.print("Counter value: "); 39 | Serial.println(counter, DEC); 40 | break; 41 | } 42 | case 255: 43 | if (tmpdata == -1 ) { 44 | Serial.print("Counter value: "); 45 | Serial.println(counter, DEC); 46 | counter += tmpdata*magicnumber; 47 | break; 48 | } 49 | if (tmpdata == +1) { 50 | Serial.print("Counter value: "); 51 | Serial.println(counter, DEC); 52 | break; 53 | } 54 | case 254: 55 | if (tmpdata == -1 ) { 56 | Serial.print("Counter value: "); 57 | Serial.println(counter, DEC); 58 | counter += tmpdata*magicnumber; 59 | break; 60 | } 61 | if (tmpdata == +1) { 62 | Serial.print("Counter value: "); 63 | Serial.println(counter, DEC); 64 | break; 65 | } 66 | default: 67 | Serial.print("Counter value: "); 68 | Serial.println(counter, DEC); 69 | counter += tmpdata*magicnumber; 70 | } 71 | } 72 | } 73 | 74 | /* returns change in encoder state (-1,0,1) */ 75 | int8_t read_encoder() 76 | { 77 | static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0}; 78 | static uint8_t old_AB = 0; 79 | /**/ 80 | old_AB <<= 2; //remember previous state 81 | old_AB |= ( ENC_PORT & 0x03 ); //add current state 82 | return ( enc_states[( old_AB & 0x0f )]); 83 | } 84 | -------------------------------------------------------------------------------- /sources/rotary_encoder_test_uno/rotary_encoder_test_uno.ino: -------------------------------------------------------------------------------- 1 | int encoderPins[] = {22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52}; // definicja tablicy z numerami pinow 2 | int numberOfPins = sizeof(encoderPins)/sizeof(int); // ilosc elementow tablicy encoderPins czyli ilosc pinow 3 | int numberOfEncoders = numberOfPins/2; 4 | int encoderState[] = {0}; // stan wartosci inkrementalnej encodera midi 0-255 5 | int pinHistory[] = {0}; // binarny stan na danym pinie 6 | int counter = 0; //this variable will be changed by encoder input 7 | int readDataA; //tymczasowa wartosc na aktualne zczytanie pinu A 8 | int readDataB; //tymczasowa wartosc na aktualne zczytanie pinu B 9 | int pinA; //Zmienna uzywana lokalnie do pinu A 10 | int pinB; //Zmienna uzywana lokalnie do pinu B 11 | int i = 0; // blah i 12 | int a = 0; // blah a 13 | 14 | 15 | // SETUP FUNCTIONS 16 | void setupEncoderMatrix() { 17 | Serial.println("Cleaning logical encoder counters"); 18 | for(i=0 ; i <= numberOfEncoders ; i++) { 19 | Serial.print(i); 20 | Serial.print(","); 21 | encoderState[i] = 0; 22 | } 23 | Serial.println("Done."); 24 | } 25 | void setupDigitalInputs(){ 26 | Serial.println("Setting physical inputs and pullup resistors"); 27 | for (i=0; i < numberOfPins ; i++ ) { 28 | Serial.print(encoderPins[i]); 29 | Serial.print(","); 30 | pinMode(encoderPins[i], INPUT); 31 | digitalWrite(encoderPins[i], HIGH); 32 | } 33 | Serial.println("Done."); 34 | } 35 | void setupPinHistory() { 36 | Serial.println("Cleaning logical pin states history"); 37 | for(i=0 ; i < numberOfPins ; i++) { 38 | Serial.print(i); 39 | Serial.print(","); 40 | pinHistory[i] = 0; 41 | } 42 | Serial.println("Done."); 43 | } 44 | 45 | // END OF SETUP FUNCTIONS 46 | 47 | void setup() 48 | { 49 | Serial.begin (115200); 50 | Serial.println("BLAH -----------------> Start"); 51 | Serial.print("Number of pins that we use:"); Serial.println(numberOfPins); 52 | Serial.print("We have this number of encoders:");Serial.println(numberOfPins/2); 53 | setupDigitalInputs(); 54 | setupEncoderMatrix(); 55 | setupPinHistory(); 56 | } 57 | 58 | void loop() 59 | { 60 | for (i=0 ; i <= numberOfPins ; i++ ) { 61 | pinA = i; 62 | pinB = i+1; 63 | i++; 64 | readDataA = digitalRead(encoderPins[pinA]); 65 | readDataB = digitalRead(encoderPins[pinB]); 66 | if ( readDataA != pinHistory[pinA] | readDataB != pinHistory[pinB] ) { 67 | Serial.print("PIN number: "); Serial.print(encoderPins[pinA]); Serial.print("/current state : "); Serial.print(readDataA); Serial.print("/previous:"); Serial.println(pinHistory[pinA]); 68 | Serial.print("PIN number: "); Serial.print(encoderPins[pinB]); Serial.print("/current state : "); Serial.print(readDataB); Serial.print("/previous:"); Serial.println(pinHistory[pinB]); 69 | pinHistory[pinA] = readDataA; 70 | pinHistory[pinB] = readDataB; 71 | } 72 | } 73 | delay(1000); 74 | Serial.println("---------------"); 75 | 76 | } 77 | -------------------------------------------------------------------------------- /sources/rotary_encoders_with_lib_mega/rotary_encoders_with_lib_mega.ino: -------------------------------------------------------------------------------- 1 | int EncoderConnectedPins[16] = {22,23,24,25,26,27,28,29,30,31,32,34,35,36,37,38}; 2 | int PinState[16] = {}; 3 | int EncoderState[8] = {}; // We should read it from ableton on init 4 | 5 | int NumberOfPins = sizeof(EncoderConnectedPins)/sizeof(int); 6 | int NumberOfPhysicalEncoders = (NumberOfPins)/2; 7 | 8 | 9 | //setup functions 10 | 11 | void SetupDigitalInputs(){ 12 | int state; 13 | int i; 14 | Serial.println("Setting physical inputs and pullup resistors"); 15 | for (i=0; i < NumberOfPins ; i++ ) { // setting HIGH state on pins 16 | Serial.print(EncoderConnectedPins[i]); 17 | Serial.print(","); 18 | pinMode(EncoderConnectedPins[i], INPUT); 19 | digitalWrite(EncoderConnectedPins[i], HIGH); 20 | } 21 | Serial.println(); 22 | 23 | Serial.print(" Inputs set to:"); 24 | for (i=0; i < NumberOfPins ; i++ ) { // Checking whether we have really set HIGH states on inputs 25 | 26 | state = digitalRead(EncoderConnectedPins[i]); 27 | Serial.print("PIN");Serial.print(i);Serial.print("->");Serial.print(state);Serial.print(";"); 28 | 29 | } 30 | Serial.println(); 31 | 32 | Serial.print("PinStates set to:"); 33 | for (i=0 ; i < NumberOfPins ; i++ ) { 34 | PinState[i] = 0; 35 | Serial.print("PIN");Serial.print(i);Serial.print("->");Serial.print(PinState[i]);Serial.print(";"); 36 | PinState[i+1] = 0; 37 | Serial.print("PIN");Serial.print(i+1);Serial.print("->");Serial.print(PinState[i+1]);Serial.print(";"); 38 | i++; 39 | } 40 | Serial.println(); 41 | Serial.println("------Done-------"); 42 | } 43 | 44 | //end of setup functions 45 | 46 | void setup() { 47 | 48 | Serial.begin(115200); 49 | SetupDigitalInputs(); 50 | 51 | } 52 | 53 | int i; 54 | int stateA; 55 | int stateB; 56 | 57 | 58 | void loop() { 59 | for (i=0; i < NumberOfPins ; i++ ) { 60 | // i holds the index of current encoder 61 | stateA = digitalRead(EncoderConnectedPins[i]); 62 | stateB = digitalRead(EncoderConnectedPins[i+1]); 63 | if ((stateA != PinState[i]) | (stateB != PinState[i+1])) { // Main logic 64 | Serial.print("PINA(");Serial.print(EncoderConnectedPins[i]);Serial.print(")");Serial.print("->");Serial.print(stateA);Serial.print("->previous(");Serial.print(PinState[i]);Serial.print(")"); 65 | Serial.println(); 66 | Serial.print("PINB(");Serial.print(EncoderConnectedPins[i+1]);Serial.print(")");Serial.print("->");Serial.print(stateB);Serial.print("->previous(");Serial.print(PinState[i+1]);Serial.print(")"); 67 | Serial.println(); 68 | } // end of main logic 69 | 70 | PinState[i] = stateA; // saving state of encoder A 71 | PinState[i+1] = stateB; // saving state of encoder B 72 | i++; 73 | } 74 | // Saving state 75 | delay(6); 76 | } 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /sources/rotary_pinchangeint_uno/rotary_pinchangeint_uno.ino: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | //available pins : 10->15,50->52 4 | 5 | #define firstPin 30 6 | #define lastPin 32 7 | 8 | int i ; 9 | int count; 10 | 11 | uint8_t latest_interrupted_pin; 12 | uint8_t interrupt_count[lastPin]={0}; // possible arduino pins 13 | 14 | void quicfunc() { 15 | latest_interrupted_pin=PCintPort::arduinoPin; 16 | interrupt_count[latest_interrupted_pin]++; 17 | } 18 | 19 | void interruptDigitalPortSetup(int portNumber){ 20 | pinMode(portNumber, INPUT); digitalWrite(portNumber, HIGH); 21 | PCintPort::attachInterrupt(portNumber, &quicfunc, CHANGE); 22 | Serial.print("Enabling interrupt on port number: ");Serial.println(i); 23 | } 24 | 25 | void setup() { 26 | /*pinMode(22, INPUT); digitalWrite(22, HIGH); 27 | PCintPort::attachInterrupt(22, &quicfunc, CHANGE); 28 | pinMode(23, INPUT); digitalWrite(23, HIGH); 29 | PCintPort::attachInterrupt(23, &quicfunc, CHANGE);*/ 30 | Serial.begin(115200); 31 | Serial.println("---------------------------------------"); 32 | Serial.print("firstpin:");Serial.println(firstPin); 33 | Serial.print("lastpin:");Serial.println(lastPin); 34 | 35 | for (i=firstPin; i < lastPin; i++ ){ 36 | interruptDigitalPortSetup(i); 37 | } 38 | } 39 | 40 | 41 | 42 | 43 | void loop() { 44 | 45 | for (i=firstPin; i < lastPin; i++) { 46 | if (interrupt_count[i] != 0) { 47 | count=interrupt_count[i]; 48 | interrupt_count[i]=0; 49 | Serial.print("Count for pin "); 50 | if (i < 14) { 51 | Serial.print("D"); 52 | Serial.print(i, DEC); 53 | } else { 54 | Serial.print("AI"); 55 | Serial.print(i-14, DEC); 56 | } 57 | Serial.print(" is "); 58 | Serial.println(count, DEC); 59 | } 60 | } 61 | } 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /sources/router_send_test_signal_mega.ino: -------------------------------------------------------------------------------- 1 | void setup() 2 | { 3 | Serial.begin(9600); 4 | Serial1.begin(9600); 5 | } 6 | 7 | void loop() 8 | { 9 | Serial.println("Hello USB"); 10 | Serial1.println("What"); 11 | delay(1000); 12 | } 13 | -------------------------------------------------------------------------------- /sources/router_send_test_signal_uno.ino: -------------------------------------------------------------------------------- 1 | void setup() 2 | { 3 | Serial.begin(9600); 4 | } 5 | 6 | void loop() 7 | { 8 | Serial.println("Hello XBee Network!"); 9 | delay(1000); 10 | } 11 | -------------------------------------------------------------------------------- /sources/serial1_read_mega/serial1_read_mega.ino: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /sources_examples/accelerometer_adaptation_uno/accelerometer_adaptation_uno.ino: -------------------------------------------------------------------------------- 1 | #ifndef AcceleroMMA7361_h 2 | #define AcceleroMMA7361_h 3 | #include 4 | 5 | class AcceleroMMA7361 6 | { 7 | public: 8 | AcceleroMMA7361(); 9 | void begin(); 10 | void begin(int sleepPin, int selfTestPin, int zeroGPin, int 11 | gSelectPin, int xPin, int yPin, int zPin); 12 | int getXRaw(); 13 | int getYRaw(); 14 | int getZRaw(); 15 | int getXVolt(); 16 | int getYVolt(); 17 | int getZVolt(); 18 | int getXAccel(); 19 | int getYAccel(); 20 | int getZAccel(); 21 | void getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis); 22 | int getTotalVector(); 23 | void setOffSets(int xOffSet, int yOffSet, int zOffSet); 24 | void calibrate(); // only to be executed when Z-axis is oriented to 25 | 26 | // it calculates the offset values by assuming Z = +1 G ; X and Y = 0 G 27 | void setARefVoltage(double _refV); 28 | void setAveraging(int avg); 29 | int getOrientation(); 30 | void setSensitivity(boolean sensi); 31 | void sleep(); 32 | void wake(); 33 | 34 | private: 35 | int _mapMMA7361V(int value); 36 | int _mapMMA7361G(int value); 37 | int _sleepPin; 38 | int _selfTestPin; 39 | int _zeroGPin; 40 | int _gSelectPin; 41 | int _xPin; 42 | int _yPin; 43 | int _zPin; 44 | int _offSets[3]; 45 | int _polarities[3]; 46 | double _refVoltage; 47 | int _average; 48 | boolean _sleep; 49 | boolean _sensi; 50 | }; 51 | #endif 52 | 53 | AcceleroMMA7361::AcceleroMMA7361() 54 | { 55 | } 56 | 57 | void AcceleroMMA7361::begin() 58 | { 59 | begin(13, 12, 11, 10, A0, A1, A2); 60 | } 61 | 62 | void AcceleroMMA7361::begin(int sleepPin, int selfTestPin, int 63 | zeroGPin, int gSelectPin, int xPin, int yPin, int zPin) 64 | { 65 | pinMode(sleepPin, OUTPUT); 66 | pinMode(selfTestPin, OUTPUT); 67 | pinMode(zeroGPin, INPUT); 68 | pinMode(gSelectPin, INPUT); 69 | pinMode(xPin, INPUT); 70 | pinMode(yPin, INPUT); 71 | pinMode(zPin, INPUT); 72 | digitalWrite(sleepPin,HIGH); 73 | digitalWrite(selfTestPin,LOW); 74 | _sleepPin = sleepPin; 75 | _selfTestPin = selfTestPin; 76 | _zeroGPin = zeroGPin; 77 | _gSelectPin = gSelectPin; 78 | _xPin = xPin; 79 | _yPin = yPin; 80 | _zPin = zPin; 81 | _sleep = false; 82 | setOffSets(0,0,0); 83 | setARefVoltage(5); 84 | setAveraging(10); 85 | setSensitivity(HIGH); 86 | } 87 | 88 | /// 89 | /// setOffSets( int offSetX, int offSetY, int offSetZ): Sets the 90 | 91 | /// The parameters are the offsets expressed in G-force (100 = 1 G) 92 | /// Offsets are added to the raw datafunctions 93 | /// 94 | void AcceleroMMA7361::setOffSets(int xOffSet, int yOffSet, int zOffSet) 95 | { 96 | if (_refVoltage==3.3) 97 | { 98 | _offSets[0]= map(xOffSet,0,3300,0,1024); 99 | _offSets[1]= map(yOffSet,0,3300,0,1024); 100 | _offSets[2]= map(zOffSet,0,3300,0,1024); 101 | } 102 | else 103 | { 104 | _offSets[0]= map(xOffSet,0,5000,0,1024); 105 | _offSets[1]= map(yOffSet,0,5000,0,1024); 106 | _offSets[2]= map(zOffSet,0,5000,0,1024); 107 | } 108 | } 109 | 110 | /// 111 | /// setARefVoltage(double _refV): Sets the AREF voltage to external, 112 | 113 | /// default is 5 when no AREF is used. When you want to use 3.3 AREF, 114 | 115 | /// 3.3 V VCC pin. This increases accuracy 116 | /// 117 | void AcceleroMMA7361::setARefVoltage(double refV) 118 | { 119 | _refVoltage = refV; 120 | if (refV == 3.3) 121 | { 122 | analogReference(EXTERNAL); 123 | } 124 | } 125 | 126 | /// 127 | /// setAveraging(int avg): Sets how many samples have to be averaged 128 | 129 | /// 130 | void AcceleroMMA7361::setAveraging(int avg) 131 | { 132 | _average = avg; 133 | } 134 | 135 | /// 136 | /// setSensitivity sets the sensitivity to +/-1.5 G (HIGH) or +/-6 G 137 | 138 | /// 139 | void AcceleroMMA7361::setSensitivity(boolean sensi) 140 | { 141 | _sensi = sensi; 142 | digitalWrite(_gSelectPin, !sensi); 143 | } 144 | 145 | /// 146 | /// sleep lets the device sleep (when device is sleeping already this 147 | 148 | /// 149 | void AcceleroMMA7361::sleep() 150 | { 151 | if (!_sleep) 152 | { 153 | digitalWrite(_sleepPin, LOW); 154 | _sleep = true; 155 | } 156 | } 157 | 158 | /// 159 | /// wake enables the device after sleep (when device is not sleeping 160 | 161 | 162 | /// 163 | void AcceleroMMA7361::wake() 164 | { 165 | if (_sleep == true) 166 | { 167 | digitalWrite(_sleepPin, HIGH); 168 | _sleep = false; 169 | delay(2); 170 | } 171 | } 172 | 173 | /// 174 | /// getXRaw(): Returns the raw data from the X-axis analog I/O port of 175 | 176 | /// 177 | int AcceleroMMA7361::getXRaw() 178 | { 179 | return analogRead(_xPin)+_offSets[0]+2; 180 | } 181 | 182 | /// 183 | /// getYRaw(): Returns the raw data from the Y-axis analog I/O port of 184 | 185 | /// 186 | int AcceleroMMA7361::getYRaw() 187 | { 188 | return analogRead(_yPin)+_offSets[1]+2; 189 | } 190 | 191 | /// 192 | /// getZRaw(): Returns the raw data from the Z-axis analog I/O port of 193 | 194 | /// 195 | int AcceleroMMA7361::getZRaw() 196 | { 197 | return analogRead(_zPin)+_offSets[2]; 198 | } 199 | 200 | /// 201 | /// getXVolt(): Returns the voltage in mV from the X-axis analog I/O 202 | 203 | /// 204 | int AcceleroMMA7361::getXVolt() 205 | { 206 | return _mapMMA7361V(getXRaw()); 207 | } 208 | 209 | /// 210 | /// getYVolt(): Returns the voltage in mV from the Y-axis analog I/O 211 | 212 | /// 213 | int AcceleroMMA7361::getYVolt() 214 | { 215 | return _mapMMA7361V(getYRaw()); 216 | } 217 | 218 | /// 219 | /// getZVolt(): Returns the voltage in mV from the Z-axis analog I/O 220 | 221 | /// 222 | int AcceleroMMA7361::getZVolt() 223 | { 224 | return _mapMMA7361V(getZRaw()); 225 | } 226 | 227 | /// 228 | /// getXAccel(): Returns the acceleration of the X-axis as a int (1 G = 100.00) 229 | /// 230 | int AcceleroMMA7361::getXAccel() 231 | { 232 | int sum = 0; 233 | for (int i = 0;i<_average;i++) 234 | { 235 | sum = sum + _mapMMA7361G(getXRaw()); 236 | } 237 | return sum/_average; 238 | } 239 | 240 | /// 241 | /// getYAccel(): Returns the acceleration of the Y-axis as a int (1 G = 100.00) 242 | /// 243 | int AcceleroMMA7361::getYAccel() 244 | { 245 | int sum = 0; 246 | for (int i = 0;i<_average;i++) 247 | { 248 | sum = sum + _mapMMA7361G(getYRaw()); 249 | } 250 | return sum/_average; 251 | } 252 | 253 | /// 254 | /// getZAccel(): Returns the acceleration of the Z-axis as a int (1 G = 100.00) 255 | /// 256 | int AcceleroMMA7361::getZAccel() 257 | { 258 | int sum = 0; 259 | for (int i = 0;i<_average;i++) 260 | { 261 | sum = sum + _mapMMA7361G(getZRaw()); 262 | } 263 | return sum/_average; 264 | } 265 | 266 | /// 267 | /// getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) returns all 268 | 269 | /// 270 | void AcceleroMMA7361::getAccelXYZ(int *_XAxis, int *_YAxis, int *_ZAxis) 271 | { 272 | int sum[3]; 273 | sum[0] = 0; 274 | sum[1] = 0; 275 | sum[2] = 0; 276 | for (int i = 0;i<_average;i++) 277 | { 278 | sum[0] = sum[0] + _mapMMA7361G(getXRaw()); 279 | sum[1] = sum[1] + _mapMMA7361G(getYRaw()); 280 | sum[2] = sum[2] + _mapMMA7361G(getZRaw()); 281 | } 282 | *_XAxis = sum[0]/_average; 283 | *_YAxis = sum[1]/_average; 284 | *_ZAxis = sum[2]/_average; 285 | } 286 | 287 | /// 288 | /// mapMMA7361V: calculates and returns the voltage value derived from 289 | 290 | /// 291 | int AcceleroMMA7361::_mapMMA7361V(int value) 292 | { 293 | if (_refVoltage==3.3) 294 | { 295 | return map(value,0,1024,0,3300); 296 | } 297 | else 298 | { 299 | return map(value,0,1024,0,5000); 300 | } 301 | } 302 | 303 | /// 304 | /// mapMMA7361G: calculates and returns the accelerometer value in 305 | 306 | 307 | /// 308 | int AcceleroMMA7361::_mapMMA7361G(int value) 309 | { 310 | if(_sensi == false) 311 | { 312 | if (_refVoltage==3.3) 313 | { 314 | return map(value,0,1024,-825,800); 315 | } 316 | else 317 | { 318 | return map(value,0,1024,-800,1600); 319 | } 320 | } 321 | else 322 | { 323 | if (_refVoltage==3.3) 324 | { 325 | return map(value,0,1024,-206,206); 326 | } 327 | else 328 | { 329 | return map(value,0,1024,-260,419); 330 | } 331 | } 332 | } 333 | 334 | /// 335 | /// calibrate(): Sets X and Y values via setOffsets to zero. The Z 336 | 337 | /// WARNING WHEN CALIBRATED YOU HAVE TO MAKE SURE THE Z-AXIS IS 338 | 339 | /// 340 | void AcceleroMMA7361::calibrate() 341 | { 342 | Serial.println(getOrientation()); 343 | Serial.print("\nCalibrating MMA7361011"); 344 | double var = 5000; 345 | double sumX = 0; 346 | double sumY = 0; 347 | double sumZ = 0; 348 | for (int i = 0;i 378 | /// getOrientation returns which axis perpendicular with the earths 379 | 380 | /// negative depending on which side of the axis is pointing downwards 381 | /// 382 | int AcceleroMMA7361::getOrientation() 383 | { 384 | int gemiddelde = 10; 385 | int x = 0; 386 | int y = 0; 387 | int z = 0; 388 | int xAbs = 0; 389 | int yAbs = 0; 390 | int zAbs = 0; 391 | for(int i = 0; i0) 407 | { 408 | return 1; 409 | } 410 | return -1; 411 | } 412 | if (yAbs0) 415 | { 416 | return 2; 417 | } 418 | return -2; 419 | } 420 | if (zAbs0) 423 | { 424 | return 3; 425 | } 426 | return -3; 427 | } 428 | return 0; 429 | } 430 | /// 431 | /// getTotalVector returns the magnitude of the total acceleration 432 | 433 | /// 434 | int AcceleroMMA7361::getTotalVector() 435 | { 436 | return sqrt(square(_mapMMA7361G(getXRaw())) + 437 | square(_mapMMA7361G(getYRaw())) + square(_mapMMA7361G(getZRaw()))); 438 | } 439 | 440 | 441 | AcceleroMMA7361 accelero; 442 | 443 | int x; 444 | int y; 445 | int z; 446 | int i; 447 | int counter = 0; 448 | int current_coordinates[] = {0,0,0}; 449 | int scale_x[] = {-50,50}; // needs tuning 450 | int scale_y[] = {-50,50}; // needs tuning 451 | int scale_z[] = {-50,50}; // needs tuning 452 | int previous_scale_x[] = {-0,0}; //tuning needed 453 | int previous_scale_y[] = {-0,0}; //tuning needed 454 | int previous_scale_z[] = {-0,0}; //tuning needed 455 | int x_previous; 456 | int y_previous; 457 | int z_previous; 458 | 459 | void setup() 460 | { 461 | Serial.begin(9600); 462 | Serial1.begin(9600); 463 | accelero.begin(13, 12, 11, 10, A0, A1, A2); // (int sleepPin, int selfTestPin, int zeroGPin, int gSelectPin, int xPin, int yPin, int zPin); 464 | // WAŻNE: podłączyć 3V3 do AREF'u!!!! 465 | accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V 466 | accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G 467 | accelero.calibrate(); 468 | 469 | } 470 | 471 | void loop() 472 | { 473 | //Reading the accelerometer coordinates values 474 | current_coordinates[0] = accelero.getXAccel(); 475 | current_coordinates[1] = accelero.getYAccel(); 476 | current_coordinates[2] = accelero.getZAccel(); 477 | 478 | //Normalization 479 | for(i=0; i++ ; i < 3) { 480 | current_coordinates[i] = normalize(current_coordinates[i]); 481 | } 482 | 483 | //Adapting the scale for mapping 484 | 485 | scale_x[0] = min(current_coordinates[0],previous_scale_x[0]); 486 | scale_y[0] = min(current_coordinates[1],previous_scale_y[0]); 487 | scale_z[0] = min(current_coordinates[2],previous_scale_z[0]); 488 | scale_x[1] = max(current_coordinates[0],previous_scale_x[1]); 489 | scale_y[1] = max(current_coordinates[1],previous_scale_y[1]); 490 | scale_z[1] = max(current_coordinates[2],previous_scale_z[1]); 491 | 492 | //Mapowanie 493 | x = map(current_coordinates[0],scale_x[0],scale_x[1],0,127); 494 | y = map(current_coordinates[1],scale_y[0],scale_y[1],0,127); 495 | z = map(current_coordinates[2],scale_z[0],scale_z[1],0,127); 496 | 497 | //Zapisanie stanu 498 | x_previous = x; 499 | y_previous = y; 500 | z_previous = z; 501 | //min 502 | previous_scale_x[0] = scale_x[0] ; 503 | previous_scale_y[0] = scale_y[0] ; 504 | previous_scale_z[0] = scale_z[0] ; 505 | //max 506 | previous_scale_x[1] = scale_x[1] ; 507 | previous_scale_y[1] = scale_y[1] ; 508 | previous_scale_z[1] = scale_z[1] ; 509 | 510 | if(counter == 500 ) { //set period of adaptation 511 | //min 512 | previous_scale_x[0] = previous_scale_x[0] + 1 ; 513 | previous_scale_y[0] = previous_scale_y[0] + 1 ; 514 | previous_scale_z[0] = previous_scale_z[0] + 1 ; 515 | //max 516 | previous_scale_x[1] = previous_scale_x[1] - 1 ; 517 | previous_scale_y[1] = previous_scale_y[1] - 1 ; 518 | previous_scale_z[1] = previous_scale_z[1] - 1 ; 519 | //min 520 | scale_x[0] = scale_x[0] + 1 ; 521 | scale_y[0] = scale_y[0] + 1 ; 522 | scale_z[0] = scale_z[0] + 1 ; 523 | //max 524 | scale_x[1] = scale_x[1] - 1 ; 525 | scale_y[1] = scale_y[1] - 1 ; 526 | scale_z[1] = scale_z[1] - 1 ; 527 | //Serial.println("Adaptation commited"); 528 | counter = 0; 529 | } 530 | /* 531 | Serial.println(); 532 | Serial.print("X: "); 533 | Serial.print(scale_x[0]); 534 | Serial.print("->"); 535 | Serial.print(scale_x[1]); 536 | Serial.print("/previous:"); 537 | Serial.print(previous_scale_x[0]); 538 | Serial.print("->"); 539 | Serial.print(previous_scale_x[1]); 540 | Serial.println(); 541 | Serial.print("Y: "); 542 | Serial.print(scale_y[0]); 543 | Serial.print("->"); 544 | Serial.print(scale_y[1]); 545 | Serial.print("/previous:"); 546 | Serial.print(previous_scale_y[0]); 547 | Serial.print("->"); 548 | Serial.print(previous_scale_y[1]); 549 | Serial.println(); 550 | Serial.print("Z: "); 551 | Serial.print(scale_z[0]); 552 | Serial.print("->"); 553 | Serial.print(scale_z[1]); 554 | Serial.print("/previous:"); 555 | Serial.print(previous_scale_z[0]); 556 | Serial.print("->"); 557 | Serial.print(previous_scale_z[1]); 558 | Serial.println(); 559 | 560 | Serial.print("Current -> X: "); 561 | Serial.print(current_coordinates[0]); 562 | Serial.print(",Y: "); 563 | Serial.print(current_coordinates[1]); 564 | Serial.print(",Z: "); 565 | Serial.println(current_coordinates[2]); 566 | Serial.print("Our x,y,z ->"); 567 | Serial.print(x); 568 | Serial.print(','); 569 | Serial.print(y); 570 | Serial.print(','); 571 | Serial.print(z); 572 | Serial.println(); 573 | Serial.println("-------------"); 574 | */ 575 | 576 | //smoothing 577 | x = (x + x_previous)/2; 578 | y = (y + y_previous)/2; 579 | z = (z + z_previous)/2; 580 | 581 | Serial1.write(0xB0); // MIDI control change; channel 3 582 | Serial1.write(0x2A); // MIDI controller #1 583 | Serial1.write(x); // MIDI controller value of 127 584 | 585 | Serial1.write(0xB0); // MIDI control change; channel 3 586 | Serial1.write(0x2B); // MIDI controller #1 587 | Serial1.write(y); // MIDI controller value of 127 588 | 589 | Serial1.write(0xB0); // MIDI control change; channel 3 590 | Serial1.write(0x2C); // MIDI controller #1 591 | Serial1.write(z); // MIDI controller value of 127 592 | 593 | delay(10); 594 | counter++; 595 | 596 | 597 | /* 598 | Serial.print("\nx: "); 599 | Serial.print(x); 600 | Serial.print(" \ty: "); 601 | Serial.print(y); 602 | Serial.print(" \tz: "); 603 | Serial.print(z); 604 | Serial.print("\tG*10^-2"); 605 | delay(500); //make it readable 606 | */ 607 | /* 608 | Serial.write(0xB0); // MIDI control change; channel 3 609 | Serial.write(0x2A); // MIDI controller #1 610 | Serial.write(current_coordinates[0]); // MIDI controller value of 127 611 | 612 | Serial.write(0xB0); // MIDI control change; channel 3 613 | Serial.write(0x2B); // MIDI controller #1 614 | Serial.write(current_coordinates[1]); // MIDI controller value of 127 615 | 616 | Serial.write(0xB0); // MIDI control change; channel 3 617 | Serial.write(0x2C); // MIDI controller #1 618 | Serial.write(current_coordinates[2]); // MIDI controller value of 127 619 | */ 620 | 621 | } 622 | 623 | 624 | 625 | 626 | 627 | 628 | int normalize(int newreading) { 629 | if (newreading > 180){ 630 | return 180; 631 | } else { 632 | return newreading; 633 | } 634 | if (newreading < -180){ 635 | return -180 ; 636 | } else { 637 | return newreading; 638 | } 639 | } 640 | 641 | 642 | 643 | 644 | --------------------------------------------------------------------------------