Last active
April 14, 2023 17:08
-
-
Save gibatronic/1ac617e3486b05f61ca6325ffa01b1fd to your computer and use it in GitHub Desktop.
Slowly spin a bipolar stepper motor, with gentle power up and down for smooth steps
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
root = true | |
[*] | |
charset = utf-8 | |
end_of_line = lf | |
indent_size = 4 | |
insert_final_newline = true | |
[{Makefile,*.sh}] | |
indent_style = tab | |
[*.ino] | |
indent_style = space |
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
#!/bin/sh | |
set -eu | |
arduino-cli \ | |
compile \ | |
--fqbn "$ARDUINO_BOARD" \ | |
"$STEPPER_PATH" |
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
.PHONY: compile \ | |
setup \ | |
upload | |
export ARDUINO_BOARD = arduino:avr:uno | |
export ARDUINO_CORE = arduino:avr | |
export ARDUINO_PORT = $(shell find /dev/cu.usbmodem* | head -n 1) | |
export STEPPER_PATH = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) | |
.SILENT: | |
compile: | |
./compile.sh | |
setup: | |
./setup.sh | |
upload: | |
./upload.sh |
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
#!/bin/sh | |
set -eu | |
if ! command -v arduino-clia 1> /dev/null 2>&1; then | |
echo 'setup: missing arduino-cli tool, get it at arduino.github.io/arduino-cli' 1>&2 | |
exit 1 | |
fi | |
arduino-cli core install "$ARDUINO_CORE" | |
arduino-cli lib install Stepper |
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 <Arduino.h> | |
#include <Stepper.h> | |
// https://store.arduino.cc/products/arduino-motor-shield-rev3 | |
#define directionA 12 | |
#define directionB 13 | |
#define pwmA 3 | |
#define pwmB 11 | |
#define brakeA 9 | |
#define brakeB 8 | |
// https://www.arduino.cc/reference/en/libraries/stepper/ | |
#define number_of_steps 200 | |
#define rotations_per_minute 200 | |
Stepper stepper = Stepper(number_of_steps, directionA, directionB); | |
void setup() { | |
pinMode(pwmA, OUTPUT); | |
pinMode(pwmB, OUTPUT); | |
pinMode(brakeA, OUTPUT); | |
pinMode(brakeB, OUTPUT); | |
analogWrite(pwmA, 0); | |
analogWrite(pwmB, 0); | |
digitalWrite(brakeA, LOW); | |
digitalWrite(brakeB, LOW); | |
stepper.setSpeed(rotations_per_minute); | |
} | |
void loop() { | |
unsigned long timestamp = millis(); | |
int duty_cycle; | |
for (duty_cycle = 0; duty_cycle <= 255; duty_cycle += 3) { | |
analogWrite(pwmA, duty_cycle); | |
analogWrite(pwmB, duty_cycle); | |
delay(2); | |
} | |
for (duty_cycle = 255; duty_cycle >= 0; duty_cycle -= 3) { | |
analogWrite(pwmA, duty_cycle); | |
analogWrite(pwmB, duty_cycle); | |
delay(1); | |
} | |
stepper.step(1); | |
delay(5000 - (millis() - timestamp)); | |
} |
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
#!/bin/sh | |
set -eu | |
main() { | |
arduino-cli \ | |
upload \ | |
--fqbn "$ARDUINO_BOARD" \ | |
--port "$ARDUINO_PORT" \ | |
"$STEPPER_PATH" | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment