Go Down

Topic: Help me i have project with rotary encoder and motor (Read 148 times) previous topic - next topic

Erik3895

Hello i am new in Arduino. I do project with rotary encoder and motor. I want that when i three time circling  encoder to other side the motor finish first circle after that do the next circle.
This is my code.

void unStepMotor(int pin_1, int pin_2,int pin_3,int pin_4,int speedMotor){
  digitalWrite(pin_1,HIGH);
  digitalWrite(pin_2,LOW);
  digitalWrite(pin_3,LOW);
  digitalWrite(pin_4,LOW);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,HIGH);
  digitalWrite(pin_2,HIGH);
  digitalWrite(pin_3,LOW);
  digitalWrite(pin_4,LOW);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,LOW);
  digitalWrite(pin_2,HIGH);
  digitalWrite(pin_3,LOW);
  digitalWrite(pin_4,LOW);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,LOW);
  digitalWrite(pin_2,HIGH);
  digitalWrite(pin_3,HIGH);
  digitalWrite(pin_4,LOW);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,LOW);
  digitalWrite(pin_2,LOW);
  digitalWrite(pin_3,HIGH);
  digitalWrite(pin_4,LOW);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,LOW);
  digitalWrite(pin_2,LOW);
  digitalWrite(pin_3,HIGH);
  digitalWrite(pin_4,HIGH);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,LOW);
  digitalWrite(pin_2,LOW);
  digitalWrite(pin_3,LOW);
  digitalWrite(pin_4,HIGH);
  delay(speedMotor + 1);
  //
  digitalWrite(pin_1,HIGH);
  digitalWrite(pin_2,LOW);
  digitalWrite(pin_3,LOW);
  digitalWrite(pin_4,HIGH);
  delay(speedMotor + 1); 
  //
}

  int z;
  int i;
  int x=0;
  int y=0;
  int j;
 
     int curso;
     
 #define outputA 2
 #define outputB 3 
  int counter = 0;
 int aState;
 int aLastState; 
int oldcounter=0;
void setup(){
  pinMode(8,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(11,OUTPUT);
  digitalWrite(8,LOW);
  digitalWrite(10,LOW);
  digitalWrite(9,LOW);
  digitalWrite(11,LOW);
  pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   Serial.begin (9600);
    aLastState = digitalRead(outputA);
   
 
}
//
void loop(){

 

   aState = digitalRead(outputA); // Reads the "current" state of the outputA

 
 aState = digitalRead(outputA); // Reads the "current" state of the outputA
   // If the previous and the current state of the outputA are different, that means a Pulse has occured
   if (aState != aLastState){     
     // If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
     if (digitalRead(outputB) != aState) {
       counter ++;
     
       
     } else {
       counter --;
     
       
     }
 
 
   }
    aLastState = aState;


 
  if(oldcounter != counter){
    if (z < 2)
    {
      z++;
    }
    else {
      if (oldcounter < counter)
      {
        curso++;
        x++;
       
       
      } else {
       curso--;
       y++;
 
       
      }
      z = 0;
      z++;
    }
  }

  oldcounter=counter;



if(x>0){

  Serial.print(x);
  if(i<25){
   i++;
    unStepMotor(11,9,10,8,2);}
    else{
      i=0;
      x--;}
    }

      else if(y>0){
 
  Serial.print(y);
  if(j<25){
   j++;
    unStepMotor(8,10,9,11,2);}
    else{
      j=0;
      y--;}
    }
       
       
   

 

}

PaulS

Quote
This is my code.
That code is NOT posted correctly. Read the stickies, and modify your post to post your code CORRECTLY, AFTER you use Tools + Auto Format AND remove the excess white space.

EVERY { belongs on a line BY ITSELF.
EVERY } belongs on a line BY ITSELF.

Some (more) functions would be useful. Put the code to read the encoder in a function that you call from loop(), rather than putting the code in loop().

You WILL miss encoder steps while you have your head buried in the sand (during the delay() calls).

You need to explain what you need help with.
The art of getting good answers lies in asking good questions.

Go Up