本帖最后由 BOWIE 于 2014-1-22 01:59 编辑
謝謝您的答覆。我先試了一顆終於運轉了,但是我希望它轉動後會在我陀螺儀的角度固定,而不是一直轉!
以下是我的程序:
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
int motorPin1 = 8; int motorPin2 = 9;
int motorPin3 = 10; int motorPin4 = 11;
int motorSpeed = 1200; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 512; // number of steps per full revolution
int lookup[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};//逆时针
int lookup1[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};//顺时针
void anticlockwise()
{ for(int i = 0; i < 8; i++)
{ setOutput(i); delayMicroseconds(motorSpeed); } }
void clockwise()
{ for(int i = 7; i >= 0; i--)
{ setOutput(i); delayMicroseconds(motorSpeed); } }
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
int valy;
int valx;
int prevValy;
int prevValx;
void setOutput(int out)//set pins to ULN2003 high in sequence from 1 to 4
{ digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3)); }
void setup()
{
pinMode(motorPin1, OUTPUT); pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT);
Wire.begin();
Serial.begin(9600);
mpu.initialize();
}
void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
valy = map(ay, -34000, 34000, 0, 179);
if(valy < 90 )
{anticlockwise();}
else if(valy > 90)
{clockwise();}
else
{ delayMicroseconds(motorSpeed*5);}
Serial.print(valy);
prevValy = valy;
} |