/* * Nervous Blinkm * Change the blinking frequency depending on range changes * 2008/03/14 * http://tinkerlog.com */ #include "Wire.h" #include "BlinkM_funcs.h" #define RANGER_PIN 3 // pin for the sharp gp2d12 range sensor #define DELTA_THRESHOLD 10 // threshold for the range sensor #define MAX_RANGE_CHANGE 50 // max range change that can be detected #define CALMED_HUE 172 // hue value for calmed state int blinkm_addr = 0x10; // the address we're going to set the BlinkM to int distance = 0; int oldDistance = 0; int nervous = 0; int delta = 0; byte i = 0; void setup() { BlinkM_begin(); BlinkM_setAddress(blinkm_addr); Serial.begin(19200); byte rc = BlinkM_checkAddress(blinkm_addr); if (rc == -1) { Serial.println("\r\nno response"); } else if (rc == 1) { Serial.println("\r\naddr mismatch"); } BlinkM_playScript(blinkm_addr, 5, 0, 0); // play the blue blinking script BlinkM_setFadeSpeed(blinkm_addr, 20); BlinkM_setTimeAdj(blinkm_addr, 20); nervous = 0; } void loop() { // do 4 samples of the distance to reduce the jitter of the range sensor distance = 0; for (i = 0; i < 4; i++) { distance += analogRead(RANGER_PIN); delay(10); } distance = distance >> 2; Serial.print("distance: "); Serial.print(distance); // compute the distance change delta = min(abs(oldDistance - distance), MAX_RANGE_CHANGE); nervous += (delta > DELTA_THRESHOLD) ? (delta - DELTA_THRESHOLD) : 0; // sum up (only if delta is noteable) if (nervous > CALMED_HUE) { // limit the nervousness nervous = CALMED_HUE; } nervous = nervous * 0.99; // reduce the nervousness with every cycle Serial.print(", nervous: "); Serial.println(nervous); // set the time to smaller --> play script faster // set the fade speed faster --> faster fading BlinkM_setTimeAdj(blinkm_addr, 25 - (nervous >> 2)); BlinkM_setFadeSpeed(blinkm_addr, 10 + (nervous >> 2)); oldDistance = distance; delay(50); }