Skip to content

Instantly share code, notes, and snippets.

@TakuroFukamizu
Last active August 18, 2020 12:02
Show Gist options
  • Save TakuroFukamizu/d8f10055f0d1fe6c966fc931ce07adc5 to your computer and use it in GitHub Desktop.
Save TakuroFukamizu/d8f10055f0d1fe6c966fc931ce07adc5 to your computer and use it in GitHub Desktop.
a4988 sample board for m5atom
#include <M5Atom.h>
#define DIR_PIN 23
#define STP_PIN 19
#define ENL_PIN 22
#define VOL_PIN 25
#define DSW_PIN 21
#define VOL_MAX (4095.0)
/**
STP_MIN~STP_MAXの間で可変になる。
## A4988 + nema17
400: 早い。ギリギリ回る
500: 速い。安定して回る限界
1000: ふつう
2000-10000: ゆっくり回るがとても振動する
*/
//#define STP_MIN (400)
//#define STP_MAX (2000)
/**
## TMC2100 + nema17
10: 脱調
25: 速い。ギリギリ回る
30: 速い。安定して回る限界
100: はやい。
500: ゆっくり
1000: すごくゆっくり
49000: すごいゆっくり
50000: 逆に速くなる
*/
#define STP_MIN (30)
#define STP_MAX (1000)
bool isRuning = false;
void setup() {
M5.begin(true, false, true);
pinMode(DIR_PIN, OUTPUT);
pinMode(STP_PIN, OUTPUT);
pinMode(ENL_PIN, OUTPUT);
pinMode(VOL_PIN, INPUT);
pinMode(DSW_PIN, INPUT_PULLUP);
delay(50);
M5.dis.clear();
}
void loop() {
if (M5.Btn.wasReleasefor(100)) { // M5ATOMのボタン長押しで回転スタート
isRuning = !isRuning;
}
if (!isRuning) {
digitalWrite(ENL_PIN, HIGH);
delay(100);
} else {
// 速度
int vol = analogRead(VOL_PIN);
int d = (STP_MAX - STP_MIN) * (vol / VOL_MAX);
d = d + STP_MIN;
// 回転方向
int dir = digitalRead(DSW_PIN);
digitalWrite(ENL_PIN, LOW);
digitalWrite(DIR_PIN, dir == LOW ? HIGH : LOW);
Serial.printf("vol: %d, dir: %d\n", vol, dir);
for(int i=0; i<100; i++) {
digitalWrite(STP_PIN, HIGH);
delayMicroseconds(d);
digitalWrite(STP_PIN, LOW);
delayMicroseconds(d);
}
}
M5.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment