Hi, I am trying to use the below code to read an encoder value whilst driving a dc motor via an H-bridge. The loop of the code is about the process my robot must do, whilst reading the encoder and driving the motor.
The code is a combo of two separate codes (which work by themselves) for driving a motor and reading the encoder. When combined, I can’t get an encoder reading.
Any ideas/help as to why my encoder will work with its own code, but not when put into the main code?
ENCODER CODE:
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
unsigned int copy_encoder0Pos = 0;
unsigned long lastDisplayTime;
unsigned long displayInterval = 100; // One second serial updates
void setup() {
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start"); // a personal quirk
}
void loop(){
if (millis() - lastDisplayTime >= displayInterval)
{
lastDisplayTime += displayInterval;
noInterrupts();
copy_encoder0Pos = encoder0Pos;
interrupts();
Serial.println (copy_encoder0Pos, DEC);
}
}
void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning
* forward. If they're different, it's going backward.
*
* For more information on speeding up this process, see
* [Reference/PortManipulation], specifically the PIND register.
*/
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}
}
Main code
//ENCODER DECLARATIONS
#define encoder0PinA 2
#define encoder0PinB 3
volatile unsigned int encoder0Pos = 0;
unsigned int copy_encoder0Pos = 0;
unsigned long lastDisplayTime;
unsigned long displayInterval = 100; // One second serial updates
volatile unsigned int encoderPosStart = 0;
volatile unsigned int encoderPosWall = 0;
//MOTOR DECLARATIONS
int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
boolean buttonState;
boolean lastState;
boolean state = HIGH;
void setup() {
//ENCODER SETUP
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
//MOTOR SETUP
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
attachInterrupt(0, doEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
Serial.begin (9600);
Serial.println("start");
}
void loop() {
//MAIN DRIVE
encoderPosStart = encoder0Pos; //Read initial encoder position
//Stage 1: To Wall & Reverse
int speed = 140;
while ( digitalRead(switchPin) == LOW) //OR HIGH?
{
//delay (10); //switch bounce NO DELAY!
encoderPosWall = encoder0Pos; //Store encoder value when vehiclle hits wall
boolean reverse = digitalRead(switchPin);
setMotor(speed, reverse);
//Stage 2: Stopping on first target
if (encoderPosStart == encoder0Pos) {
int speed = 0;
delay (8000);
{
boolean reverse = HIGH;
int speed = 140;
setMotor(speed, reverse); //NOT SURE THIS SPEED SETTING WILL WORK!
}
//Stopping on second target
if (encoder0Pos = encoderPosWall / 4) {
int speed = 0;
delay (8000);
while (1);
}
}
}
//ENCODER READING LOOP
if (millis() - lastDisplayTime >= displayInterval)
{
lastDisplayTime += displayInterval;
noInterrupts();
copy_encoder0Pos = encoder0Pos;
interrupts();
Serial.println (copy_encoder0Pos, DEC);
}
}
/*//MOTOR DRIVE LOOP
{
int speed = 70;
int reading = digitalRead(switchPin);
if ( reading == LOW && lastState == HIGH ) {
delay (10); // To solve the The problem switch bounce
if (digitalRead(switchPin) == LOW) state = !state;
}
boolean reverse = state;
lastState = reading;
setMotor(speed, reverse);
}*/
//ENCODER POSITION DETECTION INTERRUPT.
void doEncoder() {
/* If pinA and pinB are both high or both low, it is spinning
forward. If they're different, it's going backward.
For more information on speeding up this process, see
[Reference/PortManipulation], specifically the PIND register.
*/
if (digitalRead(encoder0PinA) == digitalRead(encoder0PinB)) {
encoder0Pos++;
} else {
encoder0Pos--;
}
}
//MOTOR DRIVE INTERRUPT
void setMotor(int speed, boolean reverse)
{
analogWrite(enablePin, speed);
digitalWrite(in1Pin, ! reverse);
digitalWrite(in2Pin, reverse);
}