test
Dependencies: MPU6050 NeoStrip mbed
Fork of NeoPixels by
Revision 1:cf1ca9ff1c9e, committed 2015-05-10
- Comitter:
- shuhei2306
- Date:
- Sun May 10 09:48:36 2015 +0000
- Parent:
- 0:f38492690f0e
- Commit message:
- first commit;
Changed in this revision
MPU6050.lib | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r f38492690f0e -r cf1ca9ff1c9e MPU6050.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MPU6050.lib Sun May 10 09:48:36 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/garfieldsg/code/MPU6050/#1e0baaf91e96
diff -r f38492690f0e -r cf1ca9ff1c9e main.cpp --- a/main.cpp Wed Mar 12 18:41:42 2014 +0000 +++ b/main.cpp Sun May 10 09:48:36 2015 +0000 @@ -11,170 +11,177 @@ #include "NeoStrip.h" #include "gt.h" -#define N 64 -#define PATTERNS 3 +#include "MPU6050.h" + +#define N 30 +#define NUM_AINS 10 -int hueToRGB(float h); -void pattern0(); -void pattern1(); -void pattern2(); +DigitalOut myled(LED1); +Serial pc(USBTX, USBRX); +MPU6050 mpu; + +NeoStrip strip(p19,N); + +int16_t ax, ay, az; +float gousei; +int16_t gx, gy, gz; +int16_t count = 0; -// array of function pointers to the various patterns -void (*patterns[])(void) = {&pattern0, &pattern1, &pattern2}; +AnalogIn ain(p15); +AnalogIn photoin(p16); +float absArray[NUM_AINS]; +float ave; +int current; + -NeoStrip strip(p18, N); -DigitalIn b1(p20); // brightness up -DigitalIn b2(p19); // brightness down -DigitalIn b3(p21); // next pattern +//color +float random4; +float random_color; +int red; +int green; +int blue; -// timer used for debugging -Timer timer; +//serial +int receiveval; + +//ぼんやり消えていく +Timer t; +int minus = 5; +int lightval = 0; int main() { - b1.mode(PullDown); - b2.mode(PullDown); - b3.mode(PullDown); - - int pattern = 0; - float bright = 0.2; // 20% is plenty for indoor use - bool b3o = b3; // old copy of button 3 to poll for changes + pc.printf("MPU6050 test\n\n"); + pc.printf("MPU6050 initialize \n"); - strip.setBrightness(bright); // set default brightness - - while (true) - { - timer.reset(); // use a timer to measure loop execution time for debugging purposes - timer.start(); // for this application, the main loop takes approximately 3ms to run - - // button 1 increases brightness - if (b1 && bright < 1) - { - bright += 0.01; - if (bright > 1) - bright = 1; - strip.setBrightness(bright); - } + mpu.initialize(); + pc.printf("MPU6050 testConnection \n"); - // button 2 decreases brightness - if (b2 && bright > 0) - { - bright -= 0.01; - if (bright < 0) - bright = 0; - strip.setBrightness(bright); - } - - // button 3 changes the pattern, only do stuff when its state has changed - if (b3 != b3o) - { - if (b3 && ++pattern == PATTERNS) - pattern = 0; - b3o = b3; - } - - // run the pattern update function which sets the strip's pixels - patterns[pattern](); - strip.write(); - - timer.stop(); - // print loop time if b3 is pressed - if (b3) - printf("Loop Time: %dus\n", timer.read_us()); - - wait_ms(10); - } -} - -// pattern0 displays a static image -void pattern0() -{ - strip.setPixels(0, N, gt_img); -} - -// display a shifting rainbow, all colors have maximum -// saturation and value, with evenly spaced hue -void pattern1() -{ - static float dh = 360.0 / N; - static float x = 0; + bool mpu6050TestResult = mpu.testConnection(); + if(mpu6050TestResult) { + pc.printf("MPU6050 test passed \n"); + } else { + pc.printf("MPU6050 test failed \n"); + } + + //t.start(); + + while(1) { + wait(0.001); + + //mic get analog in + float ain_abs; + ain_abs = fabs(ain - 0.5); + absArray[current] = ain_abs; + current++; + current = current%NUM_AINS; + // pc.printf("%f\n",ain_abs); + + // calc average + float sum = 0; + for (int i=0; i<NUM_AINS; i++) sum += absArray[i]; + ave = sum/NUM_AINS; + + //getmotion + mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); + //writing current accelerometer and gyro position + gousei = (ax*ax + ay*ay + az*az)/1000; + gousei = sqrt(gousei); - for (int i = 0; i < N; i++) - strip.setPixel(i, hueToRGB((dh * i) - x)); - - x += 1; - if (x > 360) - x = 0; -} - -// display a shifting gradient between red and blue -void pattern2() -{ - // offset for each pixel to allow the pattern to move - static float x = 0; - - float r, b, y; + pc.printf("%f\n", gousei); + // pc.printf("%f\n", photoin.read()); + + + if(photoin.read() < 0.5) + { + if(gousei > 1000) + { + lightval = 255; + for(int i = 0; i < N; i++) + { + strip.setPixel(i,lightval,0,0); + } + //pc.printf("test"); + // t.reset(); + } + else + { + //pc.printf("%f\n", t.read()); + lightval = lightval - minus; + if(lightval < 0) + { + lightval = 0; + } + for(int i = 0; i < N; i++) + { + strip.setPixel(i,lightval,0,0); + } + } + } + else + { + // output + if(ave > 0.3) { + for(int i=0; i<N; i++) strip.setPixel(i, 0, 200, 0); + // strip.write(); + } + else + { + for(int i = 0; i < N; i++) + { + strip.setPixel(i,0,0,0); + } + } + //pc.scanf("%d",receiveval); +} - for (int i = 0; i < N; i++) - { - // y is a scaled position between 0 (red) and 1.0 (blue) - y = 1.0 * i / (N - 1) + x; - if (y > 1) - y -= 1; - - // if on the left half, red is decreasing and blue is increasng - if (y < 0.5) - { - b = 2 * y; - r = 1 - b; - } - - // else red is increasing and blue is decreasing - else - { - r = 2 * (y - 0.5); - b = 1 - r; - } - // scale to integers and set the pixel - strip.setPixel(i, (uint8_t)(r * 255), 0, (uint8_t)(b * 200)); - } - - x += 0.003; - if (x > 1) - x = 0; + strip.write(); + } } - -// Converts HSV to RGB with the given hue, assuming -// maximum saturation and value -int hueToRGB(float h) +/* +void random_() { - // lots of floating point magic from the internet and scratching my head - float r, g, b; - if (h > 360) - h -= 360; - if (h < 0) - h += 360; - int i = (int)(h / 60.0); - float f = (h / 60.0) - i; - float q = 1 - f; - - switch (i % 6) - { - case 0: r = 1; g = f; b = 0; break; - case 1: r = q; g = 1; b = 0; break; - case 2: r = 0; g = 1; b = f; break; - case 3: r = 0; g = q; b = 1; break; - case 4: r = f; g = 0; b = 1; break; - case 5: r = 1; g = 0; b = q; break; - default: r = 0; g = 0; b = 0; break; - } - - // scale to integers and return the packed value - uint8_t R = (uint8_t)(r * 255); - uint8_t G = (uint8_t)(g * 255); - uint8_t B = (uint8_t)(b * 255); - - return (R << 16) | (G << 8) | B; + for(int i=0; i<3000; i++){ + + //random4 = random(0, 60); + random_color = random(0, 6); + + if(random_color == 0){ + red = 50; + green = 0; + blue = 0; + }else if(random_color == 1){ + red = 0; + green = 50; + blue = 0; + }else if(random_color == 2){ + red = 0; + green = 0; + blue = 50; + }else if(random_color == 3){ + red = 50; + green = 50; + blue = 0; + }else if(random_color == 4){ + red = 50; + green = 0; + blue = 50; + }else if(random_color == 5){ + red = 0; + green = 50; + blue = 50; + }else if(random_color == 6){ + red = 50; + green = 50; + blue = 50; + } + + strip.setPixel(random4, red, green, blue); + + strip.write(); + } + + } - +*/