Advertisement
squishyrobot

Accelerometer and BlinkM as Arduino

Sep 27th, 2011
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.26 KB | None | 0 0
  1. /*******************************************************************************
  2.  * This project uses a MEMSIC 2125 2-axis accelerometer breakout board from
  3.  * Parallax and hooks it up to a BlinkM from ThingM. The BlinkM is loaded with
  4.  * code as an Arduino. It reads the pulsewidth outputs from the accelerometer
  5.  * and correlates this to a color which is supposed to, on a basic level,
  6.  * represent the device heating up as you shake it.
  7.  *
  8.  * Wiring Configuration:
  9.  * BlinkM PWR+ to +5V.
  10.  * BlinkM PWR- to 0V.
  11.  * BlinkM SDA (D pin) to MEMSIC 2125 X-Axis PWM.
  12.  * BlinkM SCL (C pin) to MEMSIC 2125 Y-Axis PWM.
  13.  * MEMSIC + to +5V.
  14.  * MEMSIC 2125 GND (both of them) to 0V.
  15.  *
  16.  * Board: BlinkM/MinM ATtiny85 with Arduino ISP
  17.  ******************************************************************************/
  18.  
  19. // INCLUDES
  20. // You gotta include this to use PROGMEM (for lookup tables in program memory).
  21. #include <avr/pgmspace.h>
  22.  
  23. // LOOKUP TABLES
  24. // Save my lookup tables as unsigned chars (bytes).
  25. // Note the special data type prog_uchar when we use program memory.
  26. prog_uchar PROGMEM red[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,6,8,11,13,17,20,24,28,33,38,43,49,56,62,69,77,84,93,102,110,120,130,141,151,163,175,186,199,213,227,240,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
  27. prog_uchar PROGMEM green[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,3,3,4,4,5,6,6,7,8,9,10,11,12,13,14,15,16,17,19,20,22,23,25,26,28,29,31,33,35,36,39,40,43,44,47,49,51,53,56,58,61,63,66,68,71,74,77,79,83,85,89,91,95,98,102,105,109,111,116,119,123,126,130,133,138,141,146,149,154,158,163,166,172,175,181,184,190,194,199,203,209,213,219,223,229,234,240,244,251,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
  28. prog_uchar PROGMEM blue[] = {0,0,1,2,4,7,11,15,20,26,33,40,49,58,68,79,91,105,119,133,149,166,184,203,223,244,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,246,238,229,221,213,205,197,190,182,175,168,161,154,148,141,135,129,123,117,111,106,100,95,90,85,81,76,71,67,63,59,56,52,49,45,42,39,35,33,30,27,25,22,20,18,16,14,12,11,9,8,6,5,4,3,3,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,5,7,11,15,21,27,34,42,50,61,71,83,95,109,124,140,156,173,192,213,234,255};
  29.  
  30. // GLOBAL CONSTANTS
  31. const int REDPIN = 3;
  32. const int GREENPIN = 4;
  33. const int BLUEPIN = 1;
  34. const int SDAPIN = 0;
  35. const int SCLPIN = 2;
  36. const int XPIN = SDAPIN;
  37. const int YPIN = SCLPIN;
  38.  
  39. // GLOBAL VARIABLES
  40. // Our main loop index only needs to be one byte because the PWM is 256 steps.
  41. byte index = 0;
  42. // The same goes for the totalAcceleration.(Actually it's the sum of the
  43. // absolute values of the derivatives, but who's counting?)
  44. int totalAcceleration = 0;
  45. byte newAverage = 0;
  46. byte oldAverage = 0;
  47. // A counter to keep track of how long the input is high.
  48. int xHighTime = 0;
  49. // This is the actual value we use for the input "high time."
  50. int totalXHighTime = 0;
  51. // And here is the fake derivative of the total high time.
  52. int xHighTimePrime = 0;
  53. int yHighTime = 0;
  54. int totalYHighTime = 0;
  55. int yHighTimePrime = 0;
  56.  
  57. // SETUP FUNCTION
  58. void setup() {
  59.   // Set the appropriate pins for output.
  60.   pinMode(REDPIN, OUTPUT);
  61.   pinMode(GREENPIN, OUTPUT);
  62.   pinMode(BLUEPIN, OUTPUT);
  63.   // And set the appropriate pins for input.
  64.   pinMode(XPIN, INPUT);
  65.   pinMode(YPIN, INPUT);
  66. }
  67.  
  68. // MAIN PROGRAM LOOP
  69. void loop() {
  70.   // FIND THE DERIVATIVE OF ACCELERATION
  71.   // If the input is high, increment the xHighTime counter.
  72.   if (digitalRead(XPIN)) xHighTime++;
  73.   // If the input is low, but the counter is non-zero (meaning we just came off
  74.   // the high part of the cycle)...
  75.   else if (xHighTime) {
  76.     // ... calulate the "derivative" as the new minus the old value...
  77.     xHighTimePrime = xHighTime - totalXHighTime;
  78.     // ... save the new xHighTime...
  79.     totalXHighTime = xHighTime;
  80.     // ... and reset the xHighTime counter.
  81.     xHighTime = 0;
  82.   }
  83.   // Same as above, but for the other axis.
  84.   if (digitalRead(YPIN)) yHighTime++;
  85.   else if (yHighTime) {
  86.     yHighTimePrime = yHighTime - totalYHighTime;
  87.     totalYHighTime = yHighTime;
  88.     yHighTime = 0;
  89.   }
  90.   // Sum the "derivative" of acceleration into a singe variable.
  91.   // Note that it has to remain constrained within the byte data type.
  92.   totalAcceleration = (abs(xHighTimePrime) + abs(yHighTimePrime))*2;
  93.  
  94.   // DISPLAY THE COLOR
  95.   // Set the output high if the index is before the transition point set in the
  96.   // lookup table...
  97.   if (index < pgm_read_byte_near(red + totalAcceleration)) digitalWrite(REDPIN, HIGH);
  98.   // ...and set it low if the index is after the transition point.
  99.   else digitalWrite(REDPIN, LOW);
  100.   // Do the same for the other colors.
  101.   if (index < pgm_read_byte_near(green + totalAcceleration)) digitalWrite(GREENPIN, HIGH);
  102.   else digitalWrite(GREENPIN, LOW);
  103.   if (index < pgm_read_byte_near(blue + totalAcceleration)) digitalWrite(BLUEPIN, HIGH);
  104.   else digitalWrite(BLUEPIN, LOW);
  105.  
  106.   // KEEP TRACK OF THE LOOP COUNTER
  107.   // Increment the index if it's less than 255, otherwise reset it.
  108.   if (index < 255) index++;
  109.   else index = 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement