Please bear with me as I am still new to arduino, and really programming all together. I am working on an obstacle avoiding rover. Though at the moment I am trying to just take it one step at a time. I have a Rover 5 4 motor, 4 encoder chassis, Arduino Mega, with a Dagu 4 channel motor control board. For the task at hand I am simply trying to read two of the encoders and turn the coresponding wheels a set amount of rotations.
i put together a simpel bit of code that would read the encoders and Print to the serial monitor which wheel and the encoder count as I manually turned the wheel. Now I am attempting to attach an interrupt comand to the encoder read, and scecify that I want the motors to run when encoder count is below x and stop when encoder count reaches y. First things first, after adding the intterupt commands my serial monitor output is behaving very strange. If the wheel rotates faster that a very slow manual turn, then the Serial Monitor goes haywire and begins displaying strange characters and looses all Serial.Print formatting I had working. Also I can get the motors to turn on when the encoder count is < x but then it will run indefinetly. It just wont stop!
Again, I am very new to programming so please bear with me here. Any and all help will be greatly appreciated.
#include <Encoder.h>
Encoder Wheel2(18, 22);
int motor2Pin = 5;
int direct2Pin = 28;
int curr2Pin = A3;
Encoder Wheel3(19, 23);
int motor3Pin = 4;
int direct3Pin = 26;
int curr3Pin = A6;
int onSpeed = 50;
int FWDdirect = 255;
int offSpeed = 0;
int REVdirect = 0;
volatile long wheelcount2 = 0;
volatile long wheelcount3 = 0;
volatile unsigned long newPos2, newPos3;
void setup() {
Serial.begin(9600);
Serial.println("Milestone 2_1 Encoder Test:");
pinMode(motor2Pin, OUTPUT);
pinMode(direct2Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(direct3Pin, OUTPUT);
attachInterrupt(5,encoderRead,CHANGE);
attachInterrupt(4,encoderRead,CHANGE);
}
void loop(){
if (wheelcount2 < 1000){
startMotors();
}
else if(wheelcount2 >=1000){
stopMotors();
}
}
void encoderRead(){
newPos2 = Wheel2.read();
newPos3 = Wheel3.read();
if (newPos2 != wheelcount2 || newPos3 != wheelcount3){
Serial.print("Wheel 2 = ");
Serial.print(newPos2);
Serial.print(", Wheel 3 = ");
Serial.print(newPos3);
Serial.println();
wheelcount2 + 1; //= newPos2;
wheelcount3 = newPos3;
detachInterrupt;
}
}
void startMotors(){
analogWrite(motor2Pin, onSpeed);
analogWrite(direct2Pin, FWDdirect);
analogWrite(motor3Pin, onSpeed);
analogWrite(direct3Pin, REVdirect);
}
void stopMotors(){
analogWrite(motor2Pin, offSpeed);
analogWrite(direct2Pin, FWDdirect);
analogWrite(motor3Pin, offSpeed);
analogWrite(direct3Pin, REVdirect);
}