CONTROL DE POSICION DE MOTOR CON ENCODER

CONTROL DE POSICION DE MOTOR CON ENCODER

Solution:

  1. /*CONTROL DE POSICION DE MOTOR CON ENCODER */
  2. //Pines
  3. int Pbutton=2;
  4. int encoder=3;
  5. int motor=4;
  6. int LED=13;
  7. int a=0; //entrada
  8. int b=0; //salida
  9. int interruptor;
  10. int enc;
  11. int conta=0;
  12.  
  13. void setup ()
  14. {
  15.   Serial.begin(9600);
  16. //DeclaraciĆ³n de entradas y salidas
  17.   pinMode(Pbutton,INPUT);
  18.   pinMode(encoder,INPUT);
  19.   pinMode(LED,OUTPUT);
  20.   pinMode (motor,OUTPUT);
  21.   digitalWrite(LED,LOW);
  22. }
  23.  
  24. void loop ()
  25. {
  26.     boton_on=digitalRead(Pbutton); //Lee posicion Push button
  27.     if(interruptor==HIGH)
  28.       {
  29.        digitalWrite(LED,HIGH);
  30.        digitalWrite(motor,HIGH);
  31.       }
  32.     //funcion encoder
  33.     {
  34.      enc=digitalRead(encoder);
  35.    
  36.      if(enc==1)
  37.       {
  38.         a=1;
  39.       }
  40.      else{i=0;}
  41.     }
  42.    
  43.     if(a==1&&b==0)
  44.       {
  45.         conta++;
  46.         Serial.println(contador);
  47.       }
  48.  
  49.     if(enc==1)
  50.       {
  51.         b=1;
  52.       }
  53.     else{b=0;}
  54.    //fin funcion encoder
  55.     if(conta>=48)
  56.       {
  57.         digitalWrite(LED,LOW);
  58.         digitalWrite(motor,LOW);
  59.         delay (2000);
  60.         conta=0;
  61.       }
  62. }


Learn More :