![](/media/cache/profiles/5dfde97d835853b9720acc66b472b6ef.jpg.50x50_q85.jpg)
test
Dependencies: MPU6050 NeoStrip mbed
Fork of NeoPixels by
main.cpp@1:cf1ca9ff1c9e, 2015-05-10 (annotated)
- Committer:
- shuhei2306
- Date:
- Sun May 10 09:48:36 2015 +0000
- Revision:
- 1:cf1ca9ff1c9e
- Parent:
- 0:f38492690f0e
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aswild | 0:f38492690f0e | 1 | /* |
aswild | 0:f38492690f0e | 2 | * Adafruit NeoPixel 8x8 matrix example |
aswild | 0:f38492690f0e | 3 | * |
aswild | 0:f38492690f0e | 4 | * This program displays a couple simple patterns on an 8x8 NeoPixel matrix. |
aswild | 0:f38492690f0e | 5 | * |
aswild | 0:f38492690f0e | 6 | * 3 buttons are used for DigitalIns, 2 control the brightness up and down, |
aswild | 0:f38492690f0e | 7 | * and the third switches patterns |
aswild | 0:f38492690f0e | 8 | */ |
aswild | 0:f38492690f0e | 9 | |
aswild | 0:f38492690f0e | 10 | #include "mbed.h" |
aswild | 0:f38492690f0e | 11 | #include "NeoStrip.h" |
aswild | 0:f38492690f0e | 12 | #include "gt.h" |
aswild | 0:f38492690f0e | 13 | |
shuhei2306 | 1:cf1ca9ff1c9e | 14 | #include "MPU6050.h" |
shuhei2306 | 1:cf1ca9ff1c9e | 15 | |
shuhei2306 | 1:cf1ca9ff1c9e | 16 | #define N 30 |
shuhei2306 | 1:cf1ca9ff1c9e | 17 | #define NUM_AINS 10 |
aswild | 0:f38492690f0e | 18 | |
shuhei2306 | 1:cf1ca9ff1c9e | 19 | DigitalOut myled(LED1); |
shuhei2306 | 1:cf1ca9ff1c9e | 20 | Serial pc(USBTX, USBRX); |
shuhei2306 | 1:cf1ca9ff1c9e | 21 | MPU6050 mpu; |
shuhei2306 | 1:cf1ca9ff1c9e | 22 | |
shuhei2306 | 1:cf1ca9ff1c9e | 23 | NeoStrip strip(p19,N); |
shuhei2306 | 1:cf1ca9ff1c9e | 24 | |
shuhei2306 | 1:cf1ca9ff1c9e | 25 | int16_t ax, ay, az; |
shuhei2306 | 1:cf1ca9ff1c9e | 26 | float gousei; |
shuhei2306 | 1:cf1ca9ff1c9e | 27 | int16_t gx, gy, gz; |
shuhei2306 | 1:cf1ca9ff1c9e | 28 | int16_t count = 0; |
aswild | 0:f38492690f0e | 29 | |
shuhei2306 | 1:cf1ca9ff1c9e | 30 | AnalogIn ain(p15); |
shuhei2306 | 1:cf1ca9ff1c9e | 31 | AnalogIn photoin(p16); |
shuhei2306 | 1:cf1ca9ff1c9e | 32 | float absArray[NUM_AINS]; |
shuhei2306 | 1:cf1ca9ff1c9e | 33 | float ave; |
shuhei2306 | 1:cf1ca9ff1c9e | 34 | int current; |
shuhei2306 | 1:cf1ca9ff1c9e | 35 | |
aswild | 0:f38492690f0e | 36 | |
shuhei2306 | 1:cf1ca9ff1c9e | 37 | //color |
shuhei2306 | 1:cf1ca9ff1c9e | 38 | float random4; |
shuhei2306 | 1:cf1ca9ff1c9e | 39 | float random_color; |
shuhei2306 | 1:cf1ca9ff1c9e | 40 | int red; |
shuhei2306 | 1:cf1ca9ff1c9e | 41 | int green; |
shuhei2306 | 1:cf1ca9ff1c9e | 42 | int blue; |
aswild | 0:f38492690f0e | 43 | |
shuhei2306 | 1:cf1ca9ff1c9e | 44 | //serial |
shuhei2306 | 1:cf1ca9ff1c9e | 45 | int receiveval; |
shuhei2306 | 1:cf1ca9ff1c9e | 46 | |
shuhei2306 | 1:cf1ca9ff1c9e | 47 | //ぼんやり消えていく |
shuhei2306 | 1:cf1ca9ff1c9e | 48 | Timer t; |
shuhei2306 | 1:cf1ca9ff1c9e | 49 | int minus = 5; |
shuhei2306 | 1:cf1ca9ff1c9e | 50 | int lightval = 0; |
aswild | 0:f38492690f0e | 51 | |
aswild | 0:f38492690f0e | 52 | int main() |
aswild | 0:f38492690f0e | 53 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 54 | pc.printf("MPU6050 test\n\n"); |
shuhei2306 | 1:cf1ca9ff1c9e | 55 | pc.printf("MPU6050 initialize \n"); |
aswild | 0:f38492690f0e | 56 | |
shuhei2306 | 1:cf1ca9ff1c9e | 57 | mpu.initialize(); |
shuhei2306 | 1:cf1ca9ff1c9e | 58 | pc.printf("MPU6050 testConnection \n"); |
aswild | 0:f38492690f0e | 59 | |
shuhei2306 | 1:cf1ca9ff1c9e | 60 | bool mpu6050TestResult = mpu.testConnection(); |
shuhei2306 | 1:cf1ca9ff1c9e | 61 | if(mpu6050TestResult) { |
shuhei2306 | 1:cf1ca9ff1c9e | 62 | pc.printf("MPU6050 test passed \n"); |
shuhei2306 | 1:cf1ca9ff1c9e | 63 | } else { |
shuhei2306 | 1:cf1ca9ff1c9e | 64 | pc.printf("MPU6050 test failed \n"); |
shuhei2306 | 1:cf1ca9ff1c9e | 65 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 66 | |
shuhei2306 | 1:cf1ca9ff1c9e | 67 | //t.start(); |
shuhei2306 | 1:cf1ca9ff1c9e | 68 | |
shuhei2306 | 1:cf1ca9ff1c9e | 69 | while(1) { |
shuhei2306 | 1:cf1ca9ff1c9e | 70 | wait(0.001); |
shuhei2306 | 1:cf1ca9ff1c9e | 71 | |
shuhei2306 | 1:cf1ca9ff1c9e | 72 | //mic get analog in |
shuhei2306 | 1:cf1ca9ff1c9e | 73 | float ain_abs; |
shuhei2306 | 1:cf1ca9ff1c9e | 74 | ain_abs = fabs(ain - 0.5); |
shuhei2306 | 1:cf1ca9ff1c9e | 75 | absArray[current] = ain_abs; |
shuhei2306 | 1:cf1ca9ff1c9e | 76 | current++; |
shuhei2306 | 1:cf1ca9ff1c9e | 77 | current = current%NUM_AINS; |
shuhei2306 | 1:cf1ca9ff1c9e | 78 | // pc.printf("%f\n",ain_abs); |
shuhei2306 | 1:cf1ca9ff1c9e | 79 | |
shuhei2306 | 1:cf1ca9ff1c9e | 80 | // calc average |
shuhei2306 | 1:cf1ca9ff1c9e | 81 | float sum = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 82 | for (int i=0; i<NUM_AINS; i++) sum += absArray[i]; |
shuhei2306 | 1:cf1ca9ff1c9e | 83 | ave = sum/NUM_AINS; |
shuhei2306 | 1:cf1ca9ff1c9e | 84 | |
shuhei2306 | 1:cf1ca9ff1c9e | 85 | //getmotion |
shuhei2306 | 1:cf1ca9ff1c9e | 86 | mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); |
shuhei2306 | 1:cf1ca9ff1c9e | 87 | //writing current accelerometer and gyro position |
shuhei2306 | 1:cf1ca9ff1c9e | 88 | gousei = (ax*ax + ay*ay + az*az)/1000; |
shuhei2306 | 1:cf1ca9ff1c9e | 89 | gousei = sqrt(gousei); |
aswild | 0:f38492690f0e | 90 | |
shuhei2306 | 1:cf1ca9ff1c9e | 91 | pc.printf("%f\n", gousei); |
shuhei2306 | 1:cf1ca9ff1c9e | 92 | // pc.printf("%f\n", photoin.read()); |
shuhei2306 | 1:cf1ca9ff1c9e | 93 | |
shuhei2306 | 1:cf1ca9ff1c9e | 94 | |
shuhei2306 | 1:cf1ca9ff1c9e | 95 | if(photoin.read() < 0.5) |
shuhei2306 | 1:cf1ca9ff1c9e | 96 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 97 | if(gousei > 1000) |
shuhei2306 | 1:cf1ca9ff1c9e | 98 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 99 | lightval = 255; |
shuhei2306 | 1:cf1ca9ff1c9e | 100 | for(int i = 0; i < N; i++) |
shuhei2306 | 1:cf1ca9ff1c9e | 101 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 102 | strip.setPixel(i,lightval,0,0); |
shuhei2306 | 1:cf1ca9ff1c9e | 103 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 104 | //pc.printf("test"); |
shuhei2306 | 1:cf1ca9ff1c9e | 105 | // t.reset(); |
shuhei2306 | 1:cf1ca9ff1c9e | 106 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 107 | else |
shuhei2306 | 1:cf1ca9ff1c9e | 108 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 109 | //pc.printf("%f\n", t.read()); |
shuhei2306 | 1:cf1ca9ff1c9e | 110 | lightval = lightval - minus; |
shuhei2306 | 1:cf1ca9ff1c9e | 111 | if(lightval < 0) |
shuhei2306 | 1:cf1ca9ff1c9e | 112 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 113 | lightval = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 114 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 115 | for(int i = 0; i < N; i++) |
shuhei2306 | 1:cf1ca9ff1c9e | 116 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 117 | strip.setPixel(i,lightval,0,0); |
shuhei2306 | 1:cf1ca9ff1c9e | 118 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 119 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 120 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 121 | else |
shuhei2306 | 1:cf1ca9ff1c9e | 122 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 123 | // output |
shuhei2306 | 1:cf1ca9ff1c9e | 124 | if(ave > 0.3) { |
shuhei2306 | 1:cf1ca9ff1c9e | 125 | for(int i=0; i<N; i++) strip.setPixel(i, 0, 200, 0); |
shuhei2306 | 1:cf1ca9ff1c9e | 126 | // strip.write(); |
shuhei2306 | 1:cf1ca9ff1c9e | 127 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 128 | else |
shuhei2306 | 1:cf1ca9ff1c9e | 129 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 130 | for(int i = 0; i < N; i++) |
shuhei2306 | 1:cf1ca9ff1c9e | 131 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 132 | strip.setPixel(i,0,0,0); |
shuhei2306 | 1:cf1ca9ff1c9e | 133 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 134 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 135 | //pc.scanf("%d",receiveval); |
shuhei2306 | 1:cf1ca9ff1c9e | 136 | } |
aswild | 0:f38492690f0e | 137 | |
aswild | 0:f38492690f0e | 138 | |
shuhei2306 | 1:cf1ca9ff1c9e | 139 | strip.write(); |
shuhei2306 | 1:cf1ca9ff1c9e | 140 | } |
aswild | 0:f38492690f0e | 141 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 142 | /* |
shuhei2306 | 1:cf1ca9ff1c9e | 143 | void random_() |
aswild | 0:f38492690f0e | 144 | { |
shuhei2306 | 1:cf1ca9ff1c9e | 145 | for(int i=0; i<3000; i++){ |
shuhei2306 | 1:cf1ca9ff1c9e | 146 | |
shuhei2306 | 1:cf1ca9ff1c9e | 147 | //random4 = random(0, 60); |
shuhei2306 | 1:cf1ca9ff1c9e | 148 | random_color = random(0, 6); |
shuhei2306 | 1:cf1ca9ff1c9e | 149 | |
shuhei2306 | 1:cf1ca9ff1c9e | 150 | if(random_color == 0){ |
shuhei2306 | 1:cf1ca9ff1c9e | 151 | red = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 152 | green = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 153 | blue = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 154 | }else if(random_color == 1){ |
shuhei2306 | 1:cf1ca9ff1c9e | 155 | red = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 156 | green = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 157 | blue = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 158 | }else if(random_color == 2){ |
shuhei2306 | 1:cf1ca9ff1c9e | 159 | red = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 160 | green = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 161 | blue = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 162 | }else if(random_color == 3){ |
shuhei2306 | 1:cf1ca9ff1c9e | 163 | red = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 164 | green = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 165 | blue = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 166 | }else if(random_color == 4){ |
shuhei2306 | 1:cf1ca9ff1c9e | 167 | red = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 168 | green = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 169 | blue = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 170 | }else if(random_color == 5){ |
shuhei2306 | 1:cf1ca9ff1c9e | 171 | red = 0; |
shuhei2306 | 1:cf1ca9ff1c9e | 172 | green = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 173 | blue = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 174 | }else if(random_color == 6){ |
shuhei2306 | 1:cf1ca9ff1c9e | 175 | red = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 176 | green = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 177 | blue = 50; |
shuhei2306 | 1:cf1ca9ff1c9e | 178 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 179 | |
shuhei2306 | 1:cf1ca9ff1c9e | 180 | strip.setPixel(random4, red, green, blue); |
shuhei2306 | 1:cf1ca9ff1c9e | 181 | |
shuhei2306 | 1:cf1ca9ff1c9e | 182 | strip.write(); |
shuhei2306 | 1:cf1ca9ff1c9e | 183 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 184 | |
shuhei2306 | 1:cf1ca9ff1c9e | 185 | |
aswild | 0:f38492690f0e | 186 | } |
shuhei2306 | 1:cf1ca9ff1c9e | 187 | */ |