Salve, ho bisogno del vostro aiuto per correggere il seguente codice utile a controllare la rotazione dell'albero di un motore passo passo mediante la rotazione della manopola di un encoder ottico.
di seguito il codice:
#include "Stepper.h"
#define STEPS 32 // Number of steps for one revolution of Internal shaft
// 2048 steps for one revolution of External shaft
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
const int PinCLK=2; // Generating interrupts using CLK signal - CANALE A del mio encoder
const int PinDT=3; // Reading DT signal - CANALE B del mio encoder
const int PinSW=4; // Reading Push Button switch - COSA FACCIO CON QUESTO?
int RotaryPosition=0; //To store Stepper Motor Position
int PrevPosition; //Previous Rotary position Value to chech accuracy
int StepsToTake; // How much to move Stepper
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
//Interrupt routine runs if CLK goes from HIGH to LOW
void isr () {
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
void setup () {
pinMode(PinCLK, INPUT);
pinMode(PinDT, INPUT);
pinMode(PinSW, INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0, isr, FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}
void loop () {
small_stepper.setSpeed(700); //Max seems to be 700
if (!(digitalRead(PinSW))) { // check if button is pressed
if (RotaryPosition == 0) { // check if button was already pressed
} else {
small_stepper.step(-(RotaryPosition*50));
RotaryPosition=0; // Reset position to ZERO
}
}
// Runs if rotation was detected
if (TurnDetected) {
PrevPosition = RotaryPosition; // Save previous position in variable
if (rotationdirection){
RotaryPosition=RotaryPosition-1; } // decrase Position by 1
else {
RotaryPosition=RotaryPosition+1; } // increase Position by 1
TurnDetected = false; // do NOT repeat IF loop until new rotation dected
// Which direction to move Stepper motor
if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW
StepsToTake=50;
small_stepper.step(StepsToTake);
}
if ((RotaryPosition + 1) == PrevPosition) {
StepsToTake= -50;
small_stepper.step(StepsToTake);
}
}
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
Tale sketch è stato ideato per il seguente encoder:
Io vorrei invece utilizzare un encoder ottico Bourns ENA1J-B28 L00128L (scheda tecnica in allegato) - con un motore passo passo bipolare 0.9°, 5.63 V, 0.67 A (scheda tecnica allegata) utilizzando un driver A4988.
(schema elettrico generale allegato).
In sostanza i pin CLK e DT dell'encoder rotativo dovrebbero corrispondere ai pin A e B del mio encoder, SW che rappresenta il pulsante interno, non lo collego perché il mio encoder ne è sprovvisto e quindi come potrei gestire le righe di programma dedicate a questo pulsante?
Lo sketch riesco a caricarlo completamente, ma non si muove nulla purtroppo.
Quale potrebbe essere il problema?
Vi ringrazio in anticipo e vi saluto
Encoder rotativo - scheda tecnica.pdf (392 KB)
Motore passo passo - scheda tecnica.pdf (64.9 KB)
schema elettrico.pdf (61.1 KB)