Hi everybody
I have an Dagu rover 5 with 2 encoders, a Dagu channel 4 and a arduino mega 1280.
I want to read via the encoders how far it has driven and in wich direction, and make it drive a certain distance.
When i run the code it only display this as encoder output 0 -1 0 1 0 -1 0 1 0 1(when te wheels are turning),
If i turn of the wheels by disconnecting the power i get a normal encoder ouput 0 1 2 3 4 5 6 7 8 9 10
This is my Code:
#include <Encoder.h>
#define PwmPinMotorA 8
#define PwmPinMotorB 11
#define DirectionPinMotorA 9
#define DirectionPinMotorB 10
Encoder myEnc(2, 3); // encoder pins connected to pin D2 and D3
int message = 0; // This will hold one byte of the serial message
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
pinMode(PwmPinMotorA, OUTPUT);
pinMode(PwmPinMotorB, OUTPUT);
pinMode(DirectionPinMotorA, OUTPUT);
pinMode(DirectionPinMotorB, OUTPUT);
}
void forward(){
digitalWrite(DirectionPinMotorA, HIGH);
analogWrite(PwmPinMotorA, 25);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 25);
}
void hold(){
digitalWrite(DirectionPinMotorA, LOW);
analogWrite(PwmPinMotorA, 0);
digitalWrite(DirectionPinMotorB, LOW);
analogWrite(PwmPinMotorB, 0);
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (Serial.available() > 0) { // Check if there is a new message
message = Serial.read(); // Put the serial input into the message
}
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
if (newPosition < 50){
forward();
}
else{
hold();
}
}