encoder - phantom readings

Hi,

I got my new robot parts:
Chassis: Turtle: 2WD Mobile Robot Platform for Arduino - DFRobot
Encoder: Gravity: TT Motor Encoders Kit - DFRobot
Motor Shield: http://store.iteadstudio.com/index.php?main_page=product_info&products_id=361
Board: Arduino Mega 2560 R3 (Atmega2560 - assembled) : ID 191 : $39.95 : Adafruit Industries, Unique & fun DIY electronics and kits

I am a bit frustrated.
When I run the left wheels I get a lot of phantom readings on the right encoder. Same the other way arround. If I disconnect the encoder I get less bot not zero readings.

The relevant code parts:

Definitions:

#define EnAL 10
#define In1 8
#define In2 9

#define EnBR 11
#define In3 12
#define In4 13

volatile unsigned long encRight = 0;
volatile unsigned long encLeft = 0;

Setup:

pinMode(2, INPUT_PULLUP); attachInterrupt(0, encLeftInterrupt, CHANGE);
pinMode(3, INPUT_PULLUP); attachInterrupt(1, encRightInterrupt, CHANGE);
int i; for(i=8;i<=13;i++) pinMode(i, OUTPUT); //motor pins

Interupt Functions:

void encRightInterrupt() {
	encRight = encRight+1;
}

void encLeftInterrupt() {
	encLeft = encLeft+1;
}

Drive functions:

void LWFWD(){
	digitalWrite(In1,HIGH);
	digitalWrite(In2,LOW);
}
void LWBWD(){
	digitalWrite(In1,LOW);
	digitalWrite(In2,HIGH);
}
void LWBRK(){
	digitalWrite(In1,LOW);
	digitalWrite(In2,LOW);
}

void RWFWD(){
	digitalWrite(In3,HIGH);
	digitalWrite(In4,LOW);
}
void RWBWD(){
	digitalWrite(In3,LOW);
	digitalWrite(In4,HIGH);
}
void RWBRK(){
	digitalWrite(In3,LOW);
	digitalWrite(In4,LOW);
}

One of my test functions:

void TLF(){
	drive='X';
	encRight = 0; 
	encLeft = 0;
	LWFWD();
	analogWrite(EnAL,savespeed(240));
	while (encLeft<=500){}

	Serial.println(encLeft); // counts to 500 (well 501) - but sometimes it ven stops after 256??
	Serial.println(encRight); //should be 0 but isn't
	stop("TLF");
}

Does someone know what I may do wong? I really hope so...

Thanks
Robert

motors (electric noise) might interfere with your interrupt enc right pin?

With both then, as I get the phantom readings on both encoders. It's sometimes even so that the encoder from the wheel which isn't running has more readings than the actual encoder.
I'll try to dejam them.

I was hoping it's a software problem. Would have been easier. :slight_smile: