Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Created January 23, 2018 19:50
Show Gist options
  • Save azyobuzin/941b921853f1d06cfc201cfc3a8595f1 to your computer and use it in GitHub Desktop.
Save azyobuzin/941b921853f1d06cfc201cfc3a8595f1 to your computer and use it in GitHub Desktop.
const int AIN1 = 12;
const int BIN1 = 11;
const int BTN = 10;
void setupPin(int pin) {
pinMode(pin, OUTPUT);
}
void setup() {
// put your setup code here, to run once:
setupPin(AIN1);
setupPin(BIN1);
pinMode(AIN1, OUTPUT);
pinMode(BIN1, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void writeSignal(int a, int b) {
digitalWrite(AIN1, a);
digitalWrite(BIN1, b);
}
const int STEPS[] = {
HIGH, HIGH,
LOW, HIGH,
LOW, LOW,
HIGH, LOW
};
int currentStep = 2;
void writeStep() {
digitalWrite(AIN1, STEPS[currentStep * 2]);
digitalWrite(BIN1, STEPS[currentStep * 2 + 1]);
}
// 7.5°時計回り
void rotate() {
if (currentStep >= 3)
currentStep = 0;
else
currentStep++;
writeStep();
}
// 7.5°反時計回り
void rotateReverse() {
if (currentStep <= 0)
currentStep = 3;
else
currentStep--;
writeStep();
}
bool reverse = false;
void loop() {
if (digitalRead(BTN) == LOW) {
for (int i = 0; i < 12; i++) {
if (reverse) rotateReverse();
else rotate();
delay(20);
}
delay(500);
reverse = !reverse;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment