基于arduino的运动计步器
6050或9250陀螺仪用来计步 I2C A4-SDA A5-SCL;OLED屏幕用来显示SPI接口 13-RES 12-CS 11-DC 10-SCK 9-SDA 。
代码:
1 #include "I2Cdev.h" 2 #include "MPU6050.h" 3 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 4 #include "Wire.h" 5 #include <MsTimer2.h> https://www.pjrc.com/teensy/td_libs_MsTimer2.html 6 #endif 7 MPU6050 accelgyro; 8 int16_t ax, ay, az; 9 int16_t gx, gy, gz; 10 int axoffs,ayoffs,azoffs; 11 float rax,ray,raz; 12 float ax0,ay0,az0; 13 float ax1,ay1,az1; 14 double t1; 15 double t0; 16 double t; 17 int sum; 18 int count=0; 19 #define OUTPUT_READABLE_ACCELGYRO 20 bool blinkState = false; 21 22 #include <SPI.h> 23 #include <Wire.h> 24 #include <Adafruit_GFX.h> 25 #include <Adafruit_SSD1306.h> 26 #include <MsTimer2.h> 27 #define OLED_MOSI 9 28 #define OLED_CLK 10 29 #define OLED_DC 11 30 #define OLED_CS 12 31 #define OLED_RESET 13 32 Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); 33 #define NUMFLAKES 10 34 #define XPOS 0 35 #define YPOS 1 36 #define DELTAY 2 37 //#define LOGO16_GLCD_HEIGHT 16 38 //#define LOGO16_GLCD_WIDTH 16 39 unsigned char str_30[16];//个 40 unsigned char str_31[16];//十 41 unsigned char str_32[16];//百 42 unsigned char str_20[] = 43 { 44 0xFF, 0xFF, 0xFF, 0xE7, 0xDB, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xDB, 0xE7, 0xFF, 0xFF, /*"0",0*/ 45 0xFF, 0xFF, 0xFF, 0xEF, 0x8F, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0x83, 0xFF, 0xFF, /*"1",1*/ 46 0xFF, 0xFF, 0xFF, 0xC3, 0xBD, 0xBD, 0xBD, 0xFB, 0xFB, 0xF7, 0xEF, 0xDF, 0xBD, 0x81, 0xFF, 0xFF, /*"2",2*/ 47 0xFF, 0xFF, 0xFF, 0xC3, 0xBD, 0xBD, 0xFB, 0xE7, 0xFB, 0xFD, 0xFD, 0xBD, 0xBB, 0xC7, 0xFF, 0xFF, /*"3",3*/ 48 0xFF, 0xFF, 0xFF, 0xFB, 0xF3, 0xEB, 0xDB, 0xDB, 0xBB, 0xBB, 0x81, 0xFB, 0xFB, 0xE1, 0xFF, 0xFF, /*"4",4*/ 49 0xFF, 0xFF, 0xFF, 0x81, 0xBF, 0xBF, 0xBF, 0xA7, 0x9B, 0xFD, 0xFD, 0xBD, 0xBB, 0xC7, 0xFF, 0xFF, /*"5",5*/ 50 0xFF, 0xFF, 0xFF, 0xE3, 0xDB, 0xBF, 0xBF, 0xA7, 0x9B, 0xBD, 0xBD, 0xBD, 0xDB, 0xE7, 0xFF, 0xFF, /*"6",6*/ 51 0xFF, 0xFF, 0xFF, 0x81, 0xBB, 0xBB, 0xF7, 0xF7, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xFF, 0xFF, /*"7",7*/ 52 0xFF, 0xFF, 0xFF, 0xC3, 0xBD, 0xBD, 0xBD, 0xDB, 0xE7, 0xDB, 0xBD, 0xBD, 0xBD, 0xC3, 0xFF, 0xFF, /*"8",8*/ 53 0xFF, 0xFF, 0xFF, 0xE7, 0xDB, 0xBD, 0xBD, 0xBD, 0xD9, 0xE5, 0xFD, 0xFD, 0xDB, 0xC7, 0xFF, 0xFF /*"9",9*/ 54 }; 55 #if (SSD1306_LCDHEIGHT != 64) 56 #error("Height incorrect, please fix Adafruit_SSD1306.h!"); 57 #endif 58 int ge,shi,bai,qian; 59 void jisuan(); 60 void setup() 61 { 62 #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE 63 Wire.begin(); 64 #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE 65 Fastwire::setup(400, true); 66 #endif 67 Serial.begin(9600); 68 display.clearDisplay(); 69 display.begin(SSD1306_SWITCHCAPVCC); 70 Serial.println("Initializing I2C devices..."); 71 accelgyro.initialize(); 72 accelgyro.setFullScaleGyroRange(MPU6050_GYRO_FS_500); 73 accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_2); 74 Serial.println("Testing device connections..."); 75 Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed"); 76 getoffs(); 77 MsTimer2::set(1000,jisuan); 78 MsTimer2::start(); 79 80 } 81 void jisuan() 82 { 83 sum=abs(abs(ax1)+abs(ay1)+abs(az1)); 84 if(sum>18) 85 { 86 count++; 87 sum=0; 88 } 89 if(count<10) {ge=count; shi=0; bai=0;} 90 if((count>=10)&&(count<100)) { ge=count%10; shi=count/10; bai=0;} 91 if((count>=100)&&(count<1000)) {ge=count%100%10; shi=count%100/10; bai=count/100;} 92 if(count>1000) {ge=0;shi=0;bai=0;} 93 } 94 void xianshi() 95 { 96 int i1,i2,i3; 97 for(i1=0;i1<16;i1++) 98 { 99 str_30[i1]=str_20[ge*16+i1]; 100 } 101 for(i2=0;i2<16;i2++) 102 { 103 str_31[i2]=str_20[shi*16+i2]; 104 } 105 for(i3=0;i3<16;i3++) 106 { 107 str_32[i3]=str_20[bai*16+i3]; 108 } 109 110 display.clearDisplay(); 111 } 112 void zhi() 113 { 114 accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 115 t0=t1; 116 t1=micros(); 117 t=(t1-t0)/1000000; 118 ax=ax+axoffs; 119 ay=ay+ayoffs; 120 az=az+azoffs; 121 rax=ax; 122 ray=ay; 123 raz=az; 124 ax1=(rax/16384)*9.80; 125 ay1=(ray/16384)*9.80; 126 az1=(raz/16384)*9.80; 127 128 } 129 void getoffs() 130 { 131 int16_t ax, ay, az; 132 int16_t gx, gy, gz; 133 long int axsum=0; 134 long int aysum=0; 135 long int azsum=0; 136 int i; 137 for(i=1;i<=2000;i++) 138 { 139 accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); 140 axsum=ax+axsum; 141 aysum=ay+aysum; 142 azsum=az+azsum-16384; 143 } 144 axoffs=-axsum/2000; 145 ayoffs=-aysum/2000; 146 azoffs=-azsum/2000; 147 } 148 void loop() 149 { 150 zhi(); 151 Serial.print(count); Serial.print(bai); Serial.print(shi); Serial.println(ge); 152 xianshi(); 153 display.clearDisplay(); 154 // display.drawBitmap(39, 25, str_33, 8, 16, 1);//千 155 display.drawBitmap(47, 25, str_32, 8, 16, 1);//百 156 display.drawBitmap(55, 25, str_31, 8, 16, 1);//十 157 display.drawBitmap(63, 25, str_30, 8, 16, 1);//个 158 // display.setTextSize(1); //设置字体大小 159 // display.setTextColor(WHITE); //设置字体黑色,字体背景白色 160 // display.setCursor(40,16); //设置字体的起始位置 161 // display.println("Steps:"); //输出字符并换行 162 display.display(); 163 164 }
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。