Advertisement
Guest User

Untitled

a guest
Aug 20th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <libs/Wire/Wire.h>
  3.  
  4. #define POT_ADDR 0x2e
  5. #define ADXL345_ADDR 0x53
  6. #define DATAX0 0x32
  7. void get_pot_data();
  8. void get_accel_data();
  9. void format_accel_data(int16_t *x, int16_t *y, int16_t *z);
  10. void setup_adc();
  11. int get_adc_data();
  12.  
  13. int val = 0;
  14. int adcPin = A6;
  15. byte accel_buff[6];
  16. int16_t adc_out = 0;
  17. int16_t x = 0;
  18. int16_t y = 0;
  19. int16_t z = 0;
  20.  
  21. void setup() {
  22. delay(5000);
  23. analogReference(DEFAULT);
  24. Wire.begin();
  25. Serial.begin(9600);
  26. setup_adc();
  27. }
  28.  
  29. void setup_adc() {
  30. Wire.beginTransmission(ADXL345_ADDR);
  31. Wire.write(0x2D);
  32. Wire.write(0x08);
  33. Wire.endTransmission();
  34.  
  35. }
  36.  
  37. void loop() {
  38. get_accel_data();
  39. format_accel_data(&x, &y, &z);
  40. val = get_adc_data();
  41. Serial.print(micros());
  42. Serial.print(";");
  43. Serial.print(val);
  44. Serial.print(";");
  45. Serial.print(x);
  46. Serial.print(";");
  47. Serial.print(y);
  48. Serial.print(";");
  49. Serial.print(z);
  50. Serial.print(";");
  51. Serial.println("");
  52. }
  53.  
  54.  
  55. void get_accel_data() {
  56. Wire.beginTransmission(ADXL345_ADDR);
  57. Wire.write(DATAX0);
  58. Wire.endTransmission();
  59.  
  60. Wire.requestFrom(ADXL345_ADDR, 6);
  61.  
  62. int i = 0;
  63.  
  64. while (Wire.available()) {
  65. accel_buff[i] = Wire.read();
  66. i++;
  67. }
  68.  
  69. }
  70.  
  71. void format_accel_data(int16_t *x, int16_t *y, int16_t *z) {
  72. *x = (((int)accel_buff[1]) << 8) | accel_buff[0];
  73. *y = (((int)accel_buff[3]) << 8) | accel_buff[2];
  74. *z = (((int)accel_buff[5]) << 8) | accel_buff[4];
  75.  
  76. }
  77.  
  78. int get_adc_data() {
  79. int val = analogRead(adcPin);
  80. return val;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement