Thanks for the replies.
re the CHANGE, RISING etc, I've tried all of them and all seemed to give me problems.
re the bouncy signal, that may be a possibility but I thought that the schmitt trigger in the encoder would debounce the signal?
The code is below, sorry I thought it was so basic (and untidy atm) it wasn't worth posting:
const int _pinMotorEnableLeft = 5;
const int _pinMotorEnableRight = 7;
const int _pinMotorDirectionLeft = 4;
const int _pinMotorDirectionRight = 6;
const int _pinEncoderLeft = 20;
const int _pinEncoderRight = 21;
int _leftMotorSpeed = 0;
int _rightMotorSpeed = 0;
volatile int _leftCount = 0;
volatile int _rightCount = 0;
void UpdateLeftCount() {
++_leftCount;
}
void UpdateRightCount() {
++_rightCount;
}
void setup() {
// Motors
pinMode(_pinMotorEnableLeft, OUTPUT);
pinMode(_pinMotorEnableRight, OUTPUT);
pinMode(_pinMotorDirectionLeft, OUTPUT);
pinMode(_pinMotorDirectionRight, OUTPUT);
pinMode(_pinEncoderLeft, INPUT);
pinMode(_pinEncoderRight, INPUT);
// Attach interrupts.
attachInterrupt(3, UpdateLeftCount, CHANGE);
attachInterrupt(2, UpdateRightCount, CHANGE);
// Init serial comms at 9600 baud rate.
Serial.begin(9600);
// Init pin values
digitalWrite(_pinMotorEnableLeft, LOW);
digitalWrite(_pinMotorEnableRight, LOW);
digitalWrite(_pinMotorDirectionLeft, LOW);
digitalWrite(_pinMotorDirectionRight, LOW);
Serial.println("- Setup Complete - ");
}
void loop() {
//// temp
int encoderCount = 0;
bool oldState = LOW;
////
char incomingByte;
if (Serial.available() > 0) {
// read the incoming byte from the serial port.
incomingByte = Serial.read();
switch (incomingByte) {
case 'w':
Serial.println("fwd");
Forward();
RampUp();
break;
case 'a':
Serial.println("left");
Left();
RampUp();
break;
case 'd':
Serial.println("right");
Right();
RampUp();
break;
case 's':
Serial.println("back");
Back();
RampUp();
break;
// a temp function to test my interrupt problems.
// my pin monitor that works fine, but it obv not the correct way of doing things.
case 'p':
Serial.println("scanning pin 21");
Forward();
analogWrite(_pinMotorEnableRight, 100);
while (!(Serial.available() > 0)) {
if (oldState != digitalRead(21)) {
encoderCount++;
oldState = digitalRead(21);
}
}
Serial.println(encoderCount);
break;
// halt
case 'h':
Serial.println("stop");
Serial.println("Left: ");
Serial.println(_leftCount);
Serial.println("Right: ");
Serial.println(_rightCount);
_leftCount = 0;
_rightCount = 0;
Stop();
delay(500);
break;
}
}
}
void ResetCounters(){
_leftCount = 0;
_rightCount = 0;
}
void Forward() {
digitalWrite(_pinMotorEnableLeft, LOW);
digitalWrite(_pinMotorEnableRight, LOW);
delay(300);
digitalWrite(_pinMotorDirectionLeft, HIGH);
digitalWrite(_pinMotorDirectionRight, LOW);
ZeroMotorSpeeds();
}
void Back() {
digitalWrite(_pinMotorEnableLeft, LOW);
digitalWrite(_pinMotorEnableRight, LOW);
delay(300);
digitalWrite(_pinMotorDirectionLeft, LOW);
digitalWrite(_pinMotorDirectionRight, HIGH);
ZeroMotorSpeeds();
}
void Right() {
digitalWrite(_pinMotorEnableLeft, LOW);
digitalWrite(_pinMotorEnableRight, LOW);
delay(300);
digitalWrite(_pinMotorDirectionLeft, HIGH);
digitalWrite(_pinMotorDirectionRight, HIGH);
ZeroMotorSpeeds();
}
void Left() {
digitalWrite(_pinMotorEnableLeft, LOW);
digitalWrite(_pinMotorEnableRight, LOW);
delay(300);
digitalWrite(_pinMotorDirectionLeft, LOW);
digitalWrite(_pinMotorDirectionRight, LOW);
ZeroMotorSpeeds();
}
void ZeroMotorSpeeds() {
_leftMotorSpeed = 0;
_rightMotorSpeed = 0;
}
void Stop() {
_leftMotorSpeed = 0;
_rightMotorSpeed = 0;
digitalWrite(_pinMotorEnableLeft, LOW);
digitalWrite(_pinMotorEnableRight, LOW);
}
// RAMP UP
void RampUp() {
int leftLimit = 255;
int rightLimit = 255;
while(_leftMotorSpeed < leftLimit || _rightMotorSpeed < rightLimit) {
if (_leftMotorSpeed < leftLimit) {
_leftMotorSpeed++;
analogWrite(_pinMotorEnableLeft, _leftMotorSpeed);
}
if (_rightMotorSpeed < rightLimit) {
_rightMotorSpeed++;
analogWrite(_pinMotorEnableRight, _rightMotorSpeed);
}
delay(1);
}
}
EDIT put in code tags (they weren't there on the quick reply so I ddn't know they were there, thanks =) )