Encoder Class

Hellooo

So I had some encoder code that was essentially lots of bits and pieces, and I decided to clean it all up into classes, etc so that it’s easier to incorporate into ‘Roomba’.

Here’s also a thread I started in the Arduino forum about it (may find useful lol).

Here’s the new version:

class Encoder{
 private:
 int encoderPinA; //yellow wire
 int encoderPinB; //white wire
 int encoderPos = 0; 
 int encoderPinALast = LOW;
 int n = LOW;

public:
 Encoder (int pinA, int pinB){
 encoderPinA = pinA;
 encoderPinB = pinB;
 pinMode(encoderPinA, INPUT);
 pinMode(encoderPinB, INPUT);
 }

int limit(int lim){ //tells if the wheel has moved more than it's supposed to
 n = digitalRead(encoderPinA);
 if (((encoderPinALast == LOW) && (n == HIGH))) {
 if (digitalRead(encoderPinB) == LOW) {
 encoderPos--;
 } else {
 encoderPos++;
 }
 Serial.println(encoderPos);
 }
 encoderPinALast = n;
//------
 if(abs(encoderPos) < abs(lim))
 {
 return 0; //false; it hasn't moved more than it's supposed to
 }
 else if(abs(encoderPos) > abs(lim))
 {
 return 1; //true, it has reached/surpassed the limit
 }
 }
};

Encoder right_enc(35, 34);
Encoder left_enc(37, 36);

void setup() {
 Serial.begin(115200);
 Serial.println("Starting up...");
}

void loop() {
left_enc.limit(200);
}

And here’s the original:

 /* Read Quadrature Encoder
   Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
   Sketch by max wolf / www.meso.net .... except it's been modified by yours truly :)
   v. 0.1 - very basic functions - mw 20061220
*/

//  9/21/18
//motor A (right)
int enA = 5; //enable A, pin 7 on the Motor Module
int in1 = 42; //btw, this is mapping pins on the Motor Module (ie. the one labeled IN1) to pins on the Arduino (number 6)
int in2 = 43;

//motor B (left)
int enB = 6;
int in3 = 44; 
int in4 = 45;


int right_speed;
int left_speed;
char SIDE;

int valR;
int encoderRPinA = 35;
int encoderRPinB = 34;
int encoderRPos = 0;
int encoderRPinALast = LOW;
int nR = LOW;
//---------------------

int valL;
int encoderLPinA = 37; //yellow
int encoderLPinB = 36; //white
int encoderLPos = 0;
int encoderLPinALast = LOW;
int nL = LOW;

void setup() {
  pinMode (encoderRPinA, INPUT);
  pinMode (encoderRPinB, INPUT);
  pinMode (encoderLPinA, INPUT);
  pinMode (encoderLPinB, INPUT);
  Serial.begin(9600);

  pinMode(enA, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);

  pinMode(enB, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

//this sets it so that the robot is moving forward (ie. both wheels in same direction)
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
}

void loop() {
  nR = digitalRead(encoderRPinA);
  if (((encoderRPinALast == LOW) && (nR == HIGH))) {
    if (digitalRead(encoderRPinB) == LOW) {
      encoderRPos--;
    } else {
      encoderRPos++;
    }
 //   Serial.println(encoderRPos);
  }
//------------------------------

  nL = digitalRead(encoderLPinA);
  if (((encoderLPinALast == LOW) && (nL == HIGH))) {
    if (digitalRead(encoderLPinB) == LOW) {
      encoderLPos--;
    } else {
      encoderLPos++;
    }
  //  Serial.println(encoderLPos);
  }

  

 if(abs(encoderRPos) < abs(475))
  {
    analogWrite(enB, 90);
    //Serial.println("hooray!");
  }
else if(abs(encoderRPos) > abs(475))
{
   analogWrite(enB, 0);
  // Serial.println("boo!");
}
  encoderRPinALast = nR;
//------------------------------------------

  if(abs(encoderLPos) < 475)
  {
    analogWrite(enA, 90);
  }
else if(abs(encoderLPos) > 475)
{
   analogWrite(enA, 0);

}
  encoderLPinALast = nL;

//analogWrite(enA, 50);
//analogWrite(enB, 50);
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *