----------------------------------- Remus Boldis 17 Oct 2019 13:40 ----------------------------------- Daca doriti sa incercati Arduino Nano, va trebui sa intrati la Tools si sa setati Arduino Uno, portul com 4 (pe alte calculatoare e posibil sa apara alte porturi) si ATmega328P (Old Bootloader). Programul de comada motor folosind un encoder (se ia cu copy-paste) : /*******Interrupt-based Rotary Encoder Sketch******* by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence */ const int pinA = 2; // Our first hardware interrupt pin is digital pin 2 const int pinB = 3; // Our second hardware interrupt pin is digital pin 3 const int stepPin = 7; // pin for pulsing a step to the stepper driver const int dirPin = 6; // pin for setting stepper driver direction const bool CW = 0; // clockwise rotation direction (viewed from encoder body out toward shaft--like CNC Machines) const bool CCW = 1; // counter-clockwise rotation direction (see above, these directions appear reverse when viewed from shaft side) volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set) volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent volatile long encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255 volatile int encoderPosOld = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor) volatile bool encoderDir = CW; // stores the direction of rotation clockwise = 0 or counter-clockwise = 1 void setup() { pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases) pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases) pinMode(stepPin,OUTPUT); pinMode(dirPin,OUTPUT); attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below) attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below) Serial.begin(115200); // start the serial monitor link } void PinA() { cli(); //stop interrupts happening before we read pin values reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge encoderPos --; //decrement the encoder's position count encoderDir = CW; digitalWrite(dirPin, encoderDir); digitalWrite(stepPin, HIGH); //delayMicroseconds(5); digitalWrite(stepPin, LOW); if (encoderPos <2147483648L> 2147483647L) { encoderPos = 0L; } bFlag = 0; //reset flags for the next turn aFlag = 0; //reset flags for the next turn } else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation sei(); //restart interrupts } void loop() { if(encoderPosOld != encoderPos) { Serial.print(encoderPos); if(encoderDir == CW) Serial.println(" CW"); if(encoderDir == CCW) Serial.println(" CCW"); encoderPosOld = encoderPos; } }