

-----------------------------------

StarChild

15 Sep 2016 20:57



-----------------------------------

no, am primit de la un coleg softul, care face magia din Arduino.

ar fi asa:


#define encoder_a 2 
#define encoder_b 3
#define motor_step 5 
#define motor_direction 4 
#define button 6
int val = 0;
int old_val = 0;
int state = 0;

int MS1 = 9;
int MS2 = 10;
int MS3 = 11;

volatile long motor_position, encoder;

void setup () {
  
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(MS3, OUTPUT);
 
  pinMode(motor_step, OUTPUT);
  pinMode(motor_direction, OUTPUT);
  
  
  pinMode(encoder_a, INPUT);
  pinMode(encoder_b, INPUT);
  pinMode(button, INPUT);
  

  
  digitalWrite(encoder_a, HIGH); 
  digitalWrite(encoder_b, HIGH); 
  digitalWrite(button, HIGH);
  
 
  attachInterrupt(0, encoderPinChangeA, CHANGE);
 
  attachInterrupt(1, encoderPinChangeB, CHANGE);
  encoder = 0; 
}

void loop() {
  
  val = digitalRead(button);
    if ((val == HIGH) && (old_val == LOW)) {
      
    state = 1 - state;
  
  delay(10);  
      
    } 
    old_val = val;
   if (state ==1)
   {
        digitalWrite(MS1, LOW);
        digitalWrite(MS2, LOW);
        digitalWrite(MS3, LOW);
    }
    
    else {
       digitalWrite(MS1, HIGH);
       digitalWrite(MS2, HIGH);
       digitalWrite(MS3, HIGH);
    }
 
  
  
  if (encoder > 0) {
    digitalWrite(motor_direction, HIGH);
    digitalWrite(motor_step, HIGH);
    digitalWrite(motor_step, LOW);
    delayMicroseconds(200); 
    motor_position++;
    encoder = 0;
  }
  else if (encoder < 0) {
    digitalWrite (motor_direction, LOW); 
    digitalWrite (motor_step, HIGH);
    digitalWrite (motor_step, LOW);
    delayMicroseconds(200); 
    motor_position--;
    encoder = 0; 
  }
}

void encoderPinChangeA() {
  if (digitalRead(encoder_a)==digitalRead(encoder_b)) {
    encoder--;
  }
  else{
      encoder++;
  }
}

void encoderPinChangeB() {
  if (digitalRead(encoder_a) != digitalRead(encoder_b)) {
    encoder--;
  }
  else {
    encoder++;
  }
}

in toata povestea, se seteaza sa mearga normal in pasi intregi, iar la apasarea butonului in micropasi (1/16 pasi). Teoretic reductie 1:16...mult prea mare, dar ma mai joc din HIGH/LOW


schema de conectare a pinilor ar fi mai jos:

