Skip to content

Instantly share code, notes, and snippets.

@rndD
Created February 7, 2018 10:03
Show Gist options
  • Save rndD/faf23f994bbd43016f1b3fcd9688ff24 to your computer and use it in GitHub Desktop.
Save rndD/faf23f994bbd43016f1b3fcd9688ff24 to your computer and use it in GitHub Desktop.
// Ее нужно поставить через твою среду разработки, ну или положить рядом с проектом
#include <AccelStepper.h>
#include <AceButton.h>
using namespace ace_button;
// Encoder
boolean TurnDetected;
boolean Up;
// Мои настройки
//const int PinCLK = 2;
//const int PinDT = 3;
//const int PinSW = 4;
//
//const int MaxRotation = 100;
//
//// Motor
//const int PinStep = 18;
//const int PinDir = 17;
// Настройки Дениса
const int PinCLK = 18;
const int PinDT = 17;
const int PinSW = 16;
const int MaxRotation = 100;
// Motor
const int PinStep = 3;
const int PinDir = 4;
// Тут можно поиграть.
const int MaxSpeed = 1000;
const int StartDistance = 0;
const float Acceleartion = 100.0;
int Distance = 12000; // Тут надо смотреть. Измеряется в шагах твоего двигателя. Сколько у тебя шагов на один круг?
boolean Derection = true; // Вперед
int lastRotorCLK;
AceButton button(PinSW);
AccelStepper Motor = AccelStepper(2, PinStep, PinDir);
void setup () {
pinMode(PinCLK, INPUT);
pinMode(PinDT, INPUT);
pinMode(PinSW, INPUT_PULLUP);
button.setEventHandler(handleButtonEvent);
digitalWrite(PinSW, HIGH);
attachInterrupt(digitalPinToInterrupt(PinCLK), interuption, FALLING);
pinMode(PinStep, OUTPUT);
pinMode(PinDir, OUTPUT);
Motor.setSpeed(0);
Motor.setMaxSpeed(MaxSpeed);
Motor.setAcceleration(Acceleartion);
lastRotorCLK = digitalRead(PinCLK);
Serial.begin(9600);
Serial.println("Hello!");
}
float calcSpeed(int currentRotationPosition) {
int speedModif = 1;
if (Derection == false) {
speedModif = -1;
}
return float((MaxSpeed / MaxRotation) * currentRotationPosition * speedModif);
}
void interuption() {
delay(2);
int rotorCLK = digitalRead(PinCLK);
if (rotorCLK != lastRotorCLK) {
if (digitalRead(PinDT) != rotorCLK) {
Up = false;
} else {
Up = true;
}
TurnDetected = true;
lastRotorCLK = rotorCLK;
}
}
// Button of encoder is pressed
void handleButtonEvent(AceButton* button, uint8_t eventType, uint8_t buttonState) {
switch (eventType) {
case AceButton::kEventPressed:
Derection = !Derection;
Serial.print("!:");
break;
}
}
void loop () {
static int RotaryPosition = 1;
// Encoder
button.check();
if (TurnDetected) {
if (Up) {
// Set max pos == MaxRotation
if (RotaryPosition + 1 >= MaxRotation) {
RotaryPosition = MaxRotation;
} else {
RotaryPosition++;
}
// is Down
} else {
// Set min pos == 0
if (RotaryPosition - 1 < 0) {
RotaryPosition = 0;
} else {
RotaryPosition--;
}
}
TurnDetected = false;
// Motor
if (Motor.currentPosition() >= Distance) {
Derection = false;
}
if (Motor.currentPosition() <= StartDistance) {
Derection = true;
}
float Speed = calcSpeed(RotaryPosition);
Motor.setSpeed(Speed);
Motor.runSpeed();
Serial.print("Current encoder rot pos = ");
Serial.println(RotaryPosition);
Serial.print("Current speed = ");
Serial.println(Speed);
Serial.print("Current dirrection = ");
Serial.println(Derection);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment