Encoders (and a general update)

Hello!

The next step in my plan was finding a way to run Roomba in the background while the robot communicated with the neurons, but I’ve come upon a (really big) bump in the road: ‘Roomba’, which I wrote a couple months ago, is no longer working.

I’m not sure what it is that’s wrong… I thought it was the sensors, so I debugged those. Then I thought it might be the battery, so I soldered up new ones of those. Then I thought maybe it was delay variations caused by the new (fully powered) batteries, so I finished testing the encoders (see below). Now–according to the Serial Monitor–it looks like the Arduino is just getting stuck somewhere in the code, leaving the motors running to just ram into things??

So back to encoders. I think it might help with the ramming-into-things if I rewrite the code so that it moves forward one rotation, then checks the sensors instead of the way it’s working right now…?

Okay so here’s the code. It doesn’t use the motor class, and since I built off of some example code I found, that heading is included.

/* Read Quadrature Encoder
 Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.

Sketch by max wolf / www.meso.net
 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;

//Reverse Polarity protector is plugged into Pin 12 on the MEGA shield

int right_speed;
int left_speed;
char SIDE;

int valR;
int encoderRPinA = 48;
int encoderRPinB = 49;
int encoderRPos = 0;
int encoderRPinALast = LOW;
int nR = LOW;
//---------------------



int valL;
int encoderLPinA = 52;
int encoderLPinB = 53;
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(115200);
 Serial.println("hello");

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);
 //Serial.println("hooray!");
 }
else if(abs(encoderLPos) > 475)
{
 analogWrite(enA, 0);
 // Serial.println("boo!");
}
 encoderLPinALast = nL;

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

Leave a Reply

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