#include <AccelStepper.h>
#include <Arduino.h>
#include <RotaryEncoder.h>
// Define a stepper and the pins it will use
AccelStepper stepper(1, 5, 4); // Defaults to AccelStepper::FULL2WIRE (3 pins) on
// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
// Попробуйде дослідити ці функції (а також інші з бібліотеки https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#a3bc75bd6571b98a6177838ca81ac39ab)
// stepper.setSpeed(450); stepper.runToNewPosition(Position); stepper.move(Position);
// digitalWrite(12,LOW) ; digitalWrite(12,HIGH) ; 12 pin- Enabled
// А також файл diagram.json ( атрибути "wokwi-stepper-motor"
RotaryEncoder *encoder = nullptr;
#define PIN_IN1 2
#define PIN_IN2 3
void checkPosition()
{
encoder->tick(); // just call tick() to check the state.
}
const byte PositionPot = A0;
const byte AccelerationPot = A1;
void setup()
{
Serial.begin(115200);
stepper.setMaxSpeed(10000);
stepper.setAcceleration(35);
encoder = new RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
attachInterrupt(digitalPinToInterrupt(PIN_IN1), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_IN2), checkPosition, CHANGE);
}
void loop()
{
// Read new position
int Position = encoder->getPosition();
int Acceleration = (int)(encoder->getDirection());
stepper.setAcceleration(Acceleration);
stepper.moveTo(Position);
stepper.run();
}