基于6050的计步手环
OLED 软件SPI 数字口 13-RES 12-CS 11-DC 10-SCK 9-SDA
陀螺仪9250 I2C A4-SDA A5-SCL
- #include "I2Cdev.h"
- #include "MPU6050.h"
- #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
- #include "Wire.h"
- #include <MsTimer2.h>
- #endif
- MPU6050 accelgyro;
- int16_t ax, ay, az;
- int16_t gx, gy, gz;
- int axoffs,ayoffs,azoffs;
- float rax,ray,raz;
- float ax0,ay0,az0;
- float ax1,ay1,az1;
- double t1;
- double t0;
- double t;
- int sum;
- int count=0;
- #define OUTPUT_READABLE_ACCELGYRO
- bool blinkState = false;
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <MsTimer2.h>
- #define OLED_MOSI 9
- #define OLED_CLK 10
- #define OLED_DC 11
- #define OLED_CS 12
- #define OLED_RESET 13
- Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
- #define NUMFLAKES 10
- #define XPOS 0
- #define YPOS 1
- #define DELTAY 2
- //#define LOGO16_GLCD_HEIGHT 16
- //#define LOGO16_GLCD_WIDTH 16
- unsigned char str_30[16];//个
- unsigned char str_31[16];//十
- unsigned char str_32[16];//百
- unsigned char str_20[] =
- {
- 0xFF, 0xFF, 0xFF, 0xE7, 0xDB, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xBD, 0xDB, 0xE7, 0xFF, 0xFF, /*"0",0*/
- 0xFF, 0xFF, 0xFF, 0xEF, 0x8F, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0x83, 0xFF, 0xFF, /*"1",1*/
- 0xFF, 0xFF, 0xFF, 0xC3, 0xBD, 0xBD, 0xBD, 0xFB, 0xFB, 0xF7, 0xEF, 0xDF, 0xBD, 0x81, 0xFF, 0xFF, /*"2",2*/
- 0xFF, 0xFF, 0xFF, 0xC3, 0xBD, 0xBD, 0xFB, 0xE7, 0xFB, 0xFD, 0xFD, 0xBD, 0xBB, 0xC7, 0xFF, 0xFF, /*"3",3*/
- 0xFF, 0xFF, 0xFF, 0xFB, 0xF3, 0xEB, 0xDB, 0xDB, 0xBB, 0xBB, 0x81, 0xFB, 0xFB, 0xE1, 0xFF, 0xFF, /*"4",4*/
- 0xFF, 0xFF, 0xFF, 0x81, 0xBF, 0xBF, 0xBF, 0xA7, 0x9B, 0xFD, 0xFD, 0xBD, 0xBB, 0xC7, 0xFF, 0xFF, /*"5",5*/
- 0xFF, 0xFF, 0xFF, 0xE3, 0xDB, 0xBF, 0xBF, 0xA7, 0x9B, 0xBD, 0xBD, 0xBD, 0xDB, 0xE7, 0xFF, 0xFF, /*"6",6*/
- 0xFF, 0xFF, 0xFF, 0x81, 0xBB, 0xBB, 0xF7, 0xF7, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xEF, 0xFF, 0xFF, /*"7",7*/
- 0xFF, 0xFF, 0xFF, 0xC3, 0xBD, 0xBD, 0xBD, 0xDB, 0xE7, 0xDB, 0xBD, 0xBD, 0xBD, 0xC3, 0xFF, 0xFF, /*"8",8*/
- 0xFF, 0xFF, 0xFF, 0xE7, 0xDB, 0xBD, 0xBD, 0xBD, 0xD9, 0xE5, 0xFD, 0xFD, 0xDB, 0xC7, 0xFF, 0xFF /*"9",9*/
- };
- #if (SSD1306_LCDHEIGHT != 64)
- #error("Height incorrect, please fix Adafruit_SSD1306.h!");
- #endif
- int ge,shi,bai,qian;
- void jisuan();
- void setup()
- {
- #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
- Wire.begin();
- #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
- Fastwire::setup(400, true);
- #endif
- Serial.begin(9600);
- display.clearDisplay();
- display.begin(SSD1306_SWITCHCAPVCC);
- Serial.println("Initializing I2C devices...");
- accelgyro.initialize();
- accelgyro.setFullScaleGyroRange(MPU6050_GYRO_FS_500);
- accelgyro.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
- Serial.println("Testing device connections...");
- Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
- getoffs();
- MsTimer2::set(1000,jisuan);
- MsTimer2::start();
- }
- void jisuan()
- {
- sum=abs(abs(ax1)+abs(ay1)+abs(az1));
- if(sum>18)
- {
- count++;
- sum=0;
- }
- if(count<10) {ge=count; shi=0; bai=0;}
- if((count>=10)&&(count<100)) { ge=count%10; shi=count/10; bai=0;}
- if((count>=100)&&(count<1000)) {ge=count%100%10; shi=count%100/10; bai=count/100;}
- if(count>1000) {ge=0;shi=0;bai=0;}
- }
- void xianshi()
- {
- int i1,i2,i3;
- for(i1=0;i1<16;i1++)
- {
- str_30[i1]=str_20[ge*16+i1];
- }
- for(i2=0;i2<16;i2++)
- {
- str_31[i2]=str_20[shi*16+i2];
- }
- for(i3=0;i3<16;i3++)
- {
- str_32[i3]=str_20[bai*16+i3];
- }
- display.clearDisplay();
- }
- void zhi()
- {
- accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
- t0=t1;
- t1=micros();
- t=(t1-t0)/1000000;
- ax=ax+axoffs;
- ay=ay+ayoffs;
- az=az+azoffs;
- rax=ax;
- ray=ay;
- raz=az;
- ax1=(rax/16384)*9.80;
- ay1=(ray/16384)*9.80;
- az1=(raz/16384)*9.80;
-
- }
- void getoffs()
- {
- int16_t ax, ay, az;
- int16_t gx, gy, gz;
- long int axsum=0;
- long int aysum=0;
- long int azsum=0;
- int i;
- for(i=1;i<=2000;i++)
- {
- accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
- axsum=ax+axsum;
- aysum=ay+aysum;
- azsum=az+azsum-16384;
- }
- axoffs=-axsum/2000;
- ayoffs=-aysum/2000;
- azoffs=-azsum/2000;
- }
- void loop()
- {
- zhi();
- Serial.print(count); Serial.print(bai); Serial.print(shi); Serial.println(ge);
- xianshi();
- display.clearDisplay();
- // display.drawBitmap(39, 25, str_33, 8, 16, 1);//千
- display.drawBitmap(47, 25, str_32, 8, 16, 1);//百
- display.drawBitmap(55, 25, str_31, 8, 16, 1);//十
- display.drawBitmap(63, 25, str_30, 8, 16, 1);//个
- // display.setTextSize(1); //设置字体大小
- // display.setTextColor(WHITE); //设置字体黑色,字体背景白色
- // display.setCursor(40,16); //设置字体的起始位置
- // display.println("Steps:"); //输出字符并换行
- display.display();
-
- }
复制代码
|