Created
November 30, 2016 10:47
-
-
Save jinfeijie/3379ae8319009cd59cafea62d37c6653 to your computer and use it in GitHub Desktop.
三轴加速度传感器Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "core.h" | |
const int xpin = A0; // x-axis | |
const int ypin = A1; // y-axis | |
const int zpin = A2; // z-axis | |
int x_num; | |
int y_num; | |
int z_num; | |
int count; | |
const String XHEADER = "X: "; | |
const String YHEADER = "Y: "; | |
const String ZHEADER = "Z: "; | |
const String TAB = "\t"; | |
void led() { | |
digitalWrite(12, HIGH); | |
digitalWrite(13, LOW); | |
delay(50); | |
digitalWrite(12, LOW); | |
digitalWrite(13, LOW); | |
} | |
void setup() | |
{ | |
pinMode(12, OUTPUT); | |
pinMode(13, OUTPUT); | |
//设置波特率 | |
x_num = analogRead(xpin); | |
y_num = analogRead(ypin); | |
z_num = analogRead(zpin); | |
count = 0; | |
} | |
void loop() | |
{ | |
int tem_x, tem_y, tem_z; | |
tem_x = analogRead(xpin); | |
tem_y = analogRead(ypin); | |
tem_z = analogRead(zpin) / 100; | |
printf("tem_x:%d\n", tem_x); | |
printf("tem_y:%d\n", tem_y); | |
printf("tem_z:%d\n", tem_z); | |
if (x_num != tem_x || y_num != tem_y || z_num != tem_z) { | |
led(); | |
} | |
printf("X : %d \n", x_num); | |
printf("Y : %d \n", y_num); | |
printf("Z : %d \n", z_num); | |
printf("--------------------------------\n"); | |
//delay(1000); | |
count++; | |
printf("%d\n", count); | |
x_num = tem_x; | |
y_num = tem_y; | |
z_num = tem_z; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment