motor control, servo and stepper

This commit is contained in:
OddlyTimbot committed 2026-10-21 22:34:38 -04:00
1 parent 16a51eaa3c
commit 9e3e92b6f7
9 files changed
+1345

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
#include <Arduino.h>
#include <Servo.h>
#include <AccelStepper.h>
#define STEP_PIN 0
#define DIR_PIN 1
// Create stepper instance using DRIVER interface
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
/*
A Continuous servo example.
With 360 continuous the 90 position is a stop
180 rotates right
0 rotates left
*/
//Servo myServo;
//int pos = 0;
void setup() {
// myServo.attach(0); // Attach to GPIO 0 continuous mode
//myServo.attach(0, 500, 2500); //180 degree mode
// Set maximum speed and acceleration
stepper.setMaxSpeed(1000); // Steps per second
stepper.setAcceleration(500); // Steps per second^2
stepper.setCurrentPosition(0); // Home position
}
void loop() {
/*
myServo.write(180);
delay(5000);
myServo.write(90);
delay(1000);
myServo.write(0);
delay(5000);
myServo.write(90);
delay(1000);
*/
//hi
/*
//180 degree version
for (pos = 0; pos <= 180; pos += 1) {
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
// goes from 180 degrees to 0 degrees
for (pos = 180; pos >= 0; pos -= 1) {
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(10); // waits 15ms for the servo to reach the position
}
*/
// Move 800 steps forward (4 revolutions at 1/8 microstepping)
stepper.moveTo(800);
// Run the motor to the target position
// run() must be called frequently in the loop
while (stepper.distanceToGo() != 0) {
stepper.run();
}
delay(1000);
// Move back to home
stepper.moveTo(0);
while (stepper.distanceToGo() != 0) {
stepper.run();
}
delay(1000);
}