//+++++++++++++++から//--------------------------までの変数の初期値は可変である。
それ以外はプログラムの関連していたり、モーターの
特性にかかわるので、変えないほうが無難である。
変数の説明
const int NBSTEPS = 4096;
const int STEPTIME = 900;
-----------------
サーボモーターを動かす
/*
BYJ48 正逆回転
*/
#define IN1 4
#define IN2 5
#define IN3 6
#define IN4 7
#define IN5 8
#define IN6 9
#define IN7 10
#define IN8 11
const int NBSTEPS = 4096;
const int STEPTIME = 900;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int Step = 0;
int mati0 = 20;
boolean Clockwise = true;
//---------------------------------------------
int arrayDefault[4] = { LOW, LOW, LOW, LOW };
int stepsMatrix[8][4] = {
{ LOW, LOW, LOW, HIGH },
{ LOW, LOW, HIGH, HIGH },
{ LOW, LOW, HIGH, LOW },
{ LOW, HIGH, HIGH, LOW },
{ LOW, HIGH, LOW, LOW },
{ HIGH, HIGH, LOW, LOW },
{ HIGH, LOW, LOW, LOW },
{ HIGH, LOW, LOW, HIGH },
};
unsigned long lastTime;
unsigned long time;
void setup() { // ***** SETUP() ************
Serial.begin(9600);
Serial.println("STERT ! ");
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(IN5, OUTPUT);
pinMode(IN6, OUTPUT);
pinMode(IN7, OUTPUT);
pinMode(IN8, OUTPUT);
}
void loop() { // ***** loop() ************
unsigned long currentMicros;
int stepsLeft = NBSTEPS;
time = 0;
while (stepsLeft > 0) {
currentMicros = micros();
if (currentMicros - lastTime >= STEPTIME) {
stepper();
time += micros() - lastTime;
lastTime = micros();
stepsLeft--;
}
}
Serial.println(time);
Serial.println("STOP!");
delay(10 * mati0);
//回転方向切り替え
Clockwise = !Clockwise;
stepsLeft = NBSTEPS;
}
void writeStep(int outArray[4]) {
for (int i = 0; i < 4; i++) {
digitalWrite(IN1 + i, outArray[i]);
}
}
void stepper() {
writeStep(stepsMatrix[Step]);
} else {
writeStep(arrayDefault);
}
setDirection();
}
void setDirection() {
(Clockwise) ? (Step++) : (Step--);
if (Step > 7) {
Step = 0;
} else if (Step < 0) {
Step = 7;
}
}
*1:Step >= 0) && (Step < 8