Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************************
- * This project uses a MEMSIC 2125 2-axis accelerometer breakout board from
- * Parallax and hooks it up to a BlinkM from ThingM. The BlinkM is loaded with
- * code as an Arduino. It reads the pulsewidth outputs from the accelerometer
- * and correlates this to a color which is supposed to, on a basic level,
- * represent the device heating up as you shake it.
- *
- * Wiring Configuration:
- * BlinkM PWR+ to +5V.
- * BlinkM PWR- to 0V.
- * BlinkM SDA (D pin) to MEMSIC 2125 X-Axis PWM.
- * BlinkM SCL (C pin) to MEMSIC 2125 Y-Axis PWM.
- * MEMSIC + to +5V.
- * MEMSIC 2125 GND (both of them) to 0V.
- *
- * Board: BlinkM/MinM ATtiny85 with Arduino ISP
- ******************************************************************************/
- // INCLUDES
- // You gotta include this to use PROGMEM (for lookup tables in program memory).
- #include <avr/pgmspace.h>
- // LOOKUP TABLES
- // Save my lookup tables as unsigned chars (bytes).
- // Note the special data type prog_uchar when we use program memory.
- 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};
- 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};
- 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};
- // GLOBAL CONSTANTS
- const int REDPIN = 3;
- const int GREENPIN = 4;
- const int BLUEPIN = 1;
- const int SDAPIN = 0;
- const int SCLPIN = 2;
- const int XPIN = SDAPIN;
- const int YPIN = SCLPIN;
- // GLOBAL VARIABLES
- // Our main loop index only needs to be one byte because the PWM is 256 steps.
- byte index = 0;
- // The same goes for the totalAcceleration.(Actually it's the sum of the
- // absolute values of the derivatives, but who's counting?)
- int totalAcceleration = 0;
- byte newAverage = 0;
- byte oldAverage = 0;
- // A counter to keep track of how long the input is high.
- int xHighTime = 0;
- // This is the actual value we use for the input "high time."
- int totalXHighTime = 0;
- // And here is the fake derivative of the total high time.
- int xHighTimePrime = 0;
- int yHighTime = 0;
- int totalYHighTime = 0;
- int yHighTimePrime = 0;
- // SETUP FUNCTION
- void setup() {
- // Set the appropriate pins for output.
- pinMode(REDPIN, OUTPUT);
- pinMode(GREENPIN, OUTPUT);
- pinMode(BLUEPIN, OUTPUT);
- // And set the appropriate pins for input.
- pinMode(XPIN, INPUT);
- pinMode(YPIN, INPUT);
- }
- // MAIN PROGRAM LOOP
- void loop() {
- // FIND THE DERIVATIVE OF ACCELERATION
- // If the input is high, increment the xHighTime counter.
- if (digitalRead(XPIN)) xHighTime++;
- // If the input is low, but the counter is non-zero (meaning we just came off
- // the high part of the cycle)...
- else if (xHighTime) {
- // ... calulate the "derivative" as the new minus the old value...
- xHighTimePrime = xHighTime - totalXHighTime;
- // ... save the new xHighTime...
- totalXHighTime = xHighTime;
- // ... and reset the xHighTime counter.
- xHighTime = 0;
- }
- // Same as above, but for the other axis.
- if (digitalRead(YPIN)) yHighTime++;
- else if (yHighTime) {
- yHighTimePrime = yHighTime - totalYHighTime;
- totalYHighTime = yHighTime;
- yHighTime = 0;
- }
- // Sum the "derivative" of acceleration into a singe variable.
- // Note that it has to remain constrained within the byte data type.
- totalAcceleration = (abs(xHighTimePrime) + abs(yHighTimePrime))*2;
- // DISPLAY THE COLOR
- // Set the output high if the index is before the transition point set in the
- // lookup table...
- if (index < pgm_read_byte_near(red + totalAcceleration)) digitalWrite(REDPIN, HIGH);
- // ...and set it low if the index is after the transition point.
- else digitalWrite(REDPIN, LOW);
- // Do the same for the other colors.
- if (index < pgm_read_byte_near(green + totalAcceleration)) digitalWrite(GREENPIN, HIGH);
- else digitalWrite(GREENPIN, LOW);
- if (index < pgm_read_byte_near(blue + totalAcceleration)) digitalWrite(BLUEPIN, HIGH);
- else digitalWrite(BLUEPIN, LOW);
- // KEEP TRACK OF THE LOOP COUNTER
- // Increment the index if it's less than 255, otherwise reset it.
- if (index < 255) index++;
- else index = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement