Created
December 14, 2019 07:20
-
-
Save TakuroFukamizu/3ddf18d85145d5e5c99c2b3ee2aae4fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5StickC.h> | |
#define DIR_PIN 26 | |
#define STEP_PIN 0 | |
/* | |
for M5Stick-C | |
*/ | |
void setup() { | |
M5.begin(); | |
M5.Lcd.setRotation(1); | |
M5.Lcd.setTextSize(1); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.println("Stepper test"); | |
M5.Lcd.setCursor(0, 30, 2); | |
M5.Lcd.print("off"); | |
pinMode(DIR_PIN, OUTPUT); | |
pinMode(STEP_PIN, OUTPUT); | |
pinMode(M5_LED, OUTPUT); | |
digitalWrite(M5_LED, HIGH); | |
} | |
void loop(){ | |
int dig = 0; | |
if (M5.BtnA.read()) { | |
dig = 360; | |
Serial.println("360 forward"); | |
} else if (M5.BtnB.read()) { | |
dig = -360; | |
Serial.println("360 reverse"); | |
} | |
if (dig == 0) { | |
delay(100); | |
return; | |
} | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.println("Stepper test"); | |
M5.Lcd.setCursor(0, 30, 2); | |
M5.Lcd.printf("rotate : %02d\n", dig); | |
rotateDeg(dig, .1); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.println("Stepper test"); | |
M5.Lcd.setCursor(0, 30, 2); | |
M5.Lcd.print("off"); | |
delay(100); | |
} | |
void rotate(int steps, float speed){ | |
digitalWrite(M5_LED, LOW); | |
//rotate a specific number of microsteps (8 microsteps per step) – (negitive for reverse movement) | |
//speed is any number from .01 -> 1 with 1 being fastest – Slower is stronger | |
int dir = (steps > 0)? HIGH:LOW; | |
steps = abs(steps); | |
digitalWrite(DIR_PIN,dir); | |
float usDelay = (1/speed) * 70; | |
for(int i=0; i < steps; i++){ | |
digitalWrite(STEP_PIN, HIGH); | |
delayMicroseconds(usDelay); | |
digitalWrite(STEP_PIN, LOW); | |
delayMicroseconds(usDelay); | |
} | |
digitalWrite(M5_LED, HIGH); | |
} | |
void rotateDeg(float deg, float speed){ | |
digitalWrite(M5_LED, LOW); | |
//rotate a specific number of degrees (negitive for reverse movement) | |
//speed is any number from .01 -> 1 with 1 being fastest – Slower is stronger | |
int dir = (deg > 0)? HIGH:LOW; | |
digitalWrite(DIR_PIN,dir); | |
int steps = abs(deg)*(1/0.225); | |
float usDelay = (1/speed) * 70; | |
for(int i=0; i < steps; i++){ | |
digitalWrite(STEP_PIN, HIGH); | |
delayMicroseconds(usDelay); | |
digitalWrite(STEP_PIN, LOW); | |
delayMicroseconds(usDelay); | |
} | |
digitalWrite(M5_LED, HIGH); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment