Go Down

Topic: Micro Metal gear motor with shaft encoder (Read 5482 times) previous topic - next topic

raschemmel

#15
Jul 29, 2014, 03:53 pm Last Edit: Jul 29, 2014, 03:54 pm by raschemmel Reason: 1
Quote
click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.  


If you followed the above instructions correctly the result would look like the following :


Code: [Select]
/* Right motor and left motor terminals and enable pin */

int motorRightPositive = 6;
int motorLeftPositive = 9;
int motorRightNegative = 5;
int motorLeftNegative = 10;
int motorRightEnable_pin =12;
int motorLeftEnable_pin=2;

/* Left encoder connect with pin 3 and 4 */

int encoder0PinA = 3;
int encoder0PinB = 4;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

/*Right encoder connect with pin 7 and 8 */

int encoder0PinA1 = 7;
 int encoder0PinB1 = 8;
 int encoder0Pos1 =0 ;
int encoder0PinALast1 = LOW;
int n1 = LOW;



void setup() {
 
  pinMode (encoder0PinA,INPUT);
  pinMode (encoder0PinB,INPUT);
  pinMode (encoder0PinA1,INPUT);
  pinMode (encoder0PinB1,INPUT);
  Serial.begin (115200);
 
  pinMode (motorRightPositive, OUTPUT);  
  pinMode (motorLeftPositive, OUTPUT);
  pinMode (motorRightNegative ,OUTPUT);
  pinMode(motorLeftNegative, OUTPUT);
  pinMode(motorLeftEnable_pin, OUTPUT);
  pinMode(motorRightEnable_pin, OUTPUT);
   

   
 
}

void loop() {
 
  n = digitalRead(encoder0PinA);
  n1 = digitalRead(encoder0PinA1);



    if(encoder0Pos!=850 )
  {
  if ((encoder0PinALast == LOW) && (n == HIGH))
  {
    if (digitalRead(encoder0PinB) == LOW)
    {
      encoder0Pos--;
    } else
    {
      encoder0Pos++;
    }
   
    Serial.println (encoder0Pos);
    Serial.print("Left");
 
  }
  motorleftrun();
}

 if(encoder0Pos==850 )
  {
      encoder();
  }

  encoder0PinALast = n;
   
  if( encoder0Pos1!=850 )
  {
if ((encoder0PinALast1 == LOW) && (n1 == HIGH)  ) {
    if (digitalRead(encoder0PinB1) == LOW) {
      encoder0Pos1--;
    } else {
      encoder0Pos1++;
    }
     
   
  Serial.println (encoder0Pos1);
    Serial.print("Right");
   
  }
  motorrightrun();
  }
  encoder0PinALast1 = n1;
 
   if( encoder0Pos1==850)
  {
       encoder1();
  }

}


void encoder1()
{
  /* I am trying to make "When right motor statisfies the above condition ,function calls
  here and stop motor for 1 second and reset the encoder value" but I couldn't get the result*/
  digitalWrite(motorRightEnable_pin, LOW);
  analogWrite (motorRightPositive, 50);
  analogWrite (motorRightNegative, 50);
   delay(1000);
  encoder0Pos1 =0 ;  
}


void encoder()
{
  /* the same above scenerio here*/
  digitalWrite(motorLeftEnable_pin, LOW);
  analogWrite (motorLeftPositive, 50);
  analogWrite (motorLeftNegative, 50);
    delay(1000);
   encoder0Pos=0;
}


void  motorleftrun()
{
  digitalWrite(motorLeftEnable_pin, HIGH);
    analogWrite (motorLeftPositive, 50);
    analogWrite (motorLeftNegative, 0);
}

void motorrightrun()
{
  digitalWrite(motorRightEnable_pin, HIGH);
  analogWrite (motorRightPositive, 0);
    analogWrite (motorRightNegative, 50);  
}  


Arduino UNOs, Pro-Minis, ATMega328, ATtiny85, LCDs, MCP4162, keypads,<br />DS18B20s,74c922,nRF24L01, RS232, SD card, RC fixed wing, quadcopter

Go Up