int switch_status = HIGH; // スイッチの状態。最初はHIGHが入力されている
int motor_step_pin = 9; // A4988のSTEPピン
int motor_dir_pin = 8; // A4988のDIRピン
int steps_per_revolution = 10; // ステッピングモーターの1回転あたりのステップ数
// スピード調整用の変数
int potentiometer_pin = A0; // ポテンショメータのアナログ入力ピン
int potentiometer_value = 0; // ポテンショメータからの読み取り値
int min_delay = 10; // 最小のディレイ時間 (マイクロ秒)
void setup() {
pinMode(2, INPUT_PULLUP); // 2PINを受信用に設定、プルアップ抵抗付き
pinMode(motor_step_pin, OUTPUT); // A4988のSTEPピンを出力用に設定
pinMode(motor_dir_pin, OUTPUT); // A4988のDIRピンを出力用に設定
}
void loop() {
if (digitalRead(2) == LOW) { // スイッチが押されている状態
// ステッピングモーターを1回転させる
rotateMotor();
}
}
void rotateMotor() {
// ステッピングモーターを1回転させるために、適切な制御信号を送る
digitalWrite(motor_dir_pin, HIGH); // この行をLOWに変更すると逆方向に回転します
// ポテンショメータからの値を読み取り、ディレイ時間を計算
potentiometer_value = analogRead(potentiometer_pin);
int reversed_value = 1023 - potentiometer_value; // ポテンショメータの値を逆にする
int delay_time = map(reversed_value, 0, 1023, min_delay, 1000); // 逆にした値をディレイ時間にマッピング
for (int i = 0; i < steps_per_revolution; i++) {
digitalWrite(motor_step_pin, HIGH);
delayMicroseconds(delay_time);
digitalWrite(motor_step_pin, LOW);
delayMicroseconds(delay_time);
}
}