Last active
August 20, 2019 01:22
-
-
Save hgducharme/8337c458ae375c56c41241b0b995a6de to your computer and use it in GitHub Desktop.
Basic code for controlling a DC motor and one quadrature rotary encoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LPD3806 rotary encoder | |
#include <Arduino.h> | |
#include "rotaryEncoderLPD3806.h" | |
#include "motorControllerDrokL298.h" | |
void setup() | |
{ | |
Serial.begin(9400); | |
// Motor controller | |
pinMode(IN1, OUTPUT); | |
pinMode(IN2, OUTPUT); | |
pinMode(ENA, OUTPUT); | |
// Rotary encoder | |
// Does have one pin interrupt vs two change how fast communication is? | |
pinMode(rotaryEncoderPhaseA, INPUT_PULLUP); | |
pinMode(rotaryEncoderPhaseB, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(rotaryEncoderPhaseA), readPhaseA, CHANGE); | |
attachInterrupt(digitalPinToInterrupt(rotaryEncoderPhaseB), readPhaseB, CHANGE); | |
} | |
void loop() | |
{ | |
float angleDegrees; | |
float angleRadians; | |
float distance; | |
float idlerRadius = 0.20; | |
if (rotaryEncoderCount != previousCount) | |
{ | |
angleRadians = (rotaryEncoderCount / 2400.0) * (2 * PI); | |
distance = idlerRadius * angleRadians; | |
Serial.println(distance); | |
} | |
// I have tried two different ways of controlling the motor to see if it would help with efficiency: | |
// 1) Writing the code directly | |
// 2) Outsourcing it to a function, | |
// but both behave the same. | |
// If I have the below code uncommented then the rotary encoder won't print values. | |
// Method (1) | |
// digitalWrite(IN1, LOW); | |
// digitalWrite(IN2, HIGH); | |
// analogWrite(ENA, 20); | |
// Method (2) | |
// moveCart('R', 36); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define IN1 7 | |
#define IN2 8 | |
#define ENA 9 | |
void moveCart(char direction, int speed); | |
void brake(); | |
void moveCart(char direction, int speed) | |
{ | |
if (speed == 0) { | |
brake(); | |
} | |
else | |
{ | |
// Rotate motor clockwise | |
if (direction == 'L') | |
{ | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, HIGH); | |
} | |
// Rotate motor counter-clockwise | |
else if (direction == 'R') | |
{ | |
digitalWrite(IN1, HIGH); | |
digitalWrite(IN2, LOW); | |
} | |
else | |
{ | |
brake(); | |
} | |
analogWrite(ENA, speed); | |
} | |
} | |
void brake() { | |
digitalWrite(IN1, LOW); | |
digitalWrite(IN2, LOW); | |
analogWrite(ENA, 0); | |
delay(100); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define rotaryEncoderPhaseA 2 | |
#define rotaryEncoderPhaseB 3 | |
volatile float rotaryEncoderCount; | |
volatile float previousCount = 0; | |
void readPhaseA(); | |
void readPhaseB(); | |
void readPhaseA() | |
{ | |
// If they're the same, then we're moving clockwise | |
if (digitalRead(rotaryEncoderPhaseA) == digitalRead(rotaryEncoderPhaseB)) | |
{ | |
rotaryEncoderCount--; | |
} | |
// If they're not the same, then we're moving counter clockwise | |
else | |
{ | |
rotaryEncoderCount++; | |
} | |
previousCount = rotaryEncoderCount; | |
} | |
void readPhaseB() | |
{ | |
// If they're the same, then we're moving clockwise | |
if (digitalRead(rotaryEncoderPhaseA) == digitalRead(rotaryEncoderPhaseB)) | |
{ | |
rotaryEncoderCount++; | |
} | |
// If they're not the same, then we're moving counter clockwise | |
else | |
{ | |
rotaryEncoderCount--; | |
} | |
previousCount = rotaryEncoderCount; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment