-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* Program to test encoders. (Connect both encoders) | ||
* Centauri Robotics, All rights reserved | ||
* Date: December 13, 2019 | ||
*/ | ||
//////////////////////////////////////////////////////////////////////////////////////////// | ||
//Encoder pins definition | ||
|
||
// Left encoder | ||
//Channel A to Pin 2 | ||
//Channel B to Pin 9 | ||
int Left_Encoder_PinA = 2; | ||
int Left_Encoder_PinB = 9; | ||
|
||
//Encoder value counter (Part of program; Not a hardware pin) | ||
volatile long Left_Encoder_Ticks = 0; | ||
|
||
//Variable to read current state of left encoder pin | ||
//This pins along with Channel A which is set as interrupt, allows to detect the direction | ||
volatile bool LeftEncoderBSet; | ||
|
||
///////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
//Right Encoder | ||
|
||
int Right_Encoder_PinA = 3; | ||
int Right_Encoder_PinB = 10; | ||
|
||
//Encoder value counter (Part of program; Not a hardware pin) | ||
volatile long Right_Encoder_Ticks = 0; | ||
|
||
//Variable to read current state of right encoder pin | ||
//This pins along with Channel A which is set as interrupt, allows to detect the direction | ||
volatile bool RightEncoderBSet; | ||
|
||
//////////////////////////////////////////////////////////////////////////////////////////// | ||
|
||
void setup() | ||
{ | ||
//Init Serial port with 115200 buad rate | ||
Serial.begin(115200); | ||
SetupEncoders(); | ||
} | ||
|
||
|
||
void SetupEncoders() | ||
{ | ||
// Quadrature encoders | ||
// Left encoder | ||
pinMode(Left_Encoder_PinA, INPUT); // sets pin A pullup | ||
pinMode(Left_Encoder_PinB, INPUT); // sets pin B pullup | ||
attachInterrupt(digitalPinToInterrupt(Left_Encoder_PinA), do_Left_Encoder, RISING); | ||
|
||
|
||
// Right encoder | ||
pinMode(Right_Encoder_PinA, INPUT); // sets pin A pullup | ||
pinMode(Right_Encoder_PinB, INPUT); // sets pin B pullup | ||
|
||
attachInterrupt(digitalPinToInterrupt(Right_Encoder_PinA), do_Right_Encoder, RISING); | ||
} | ||
|
||
void loop() | ||
{ | ||
Update_Encoders(); | ||
} | ||
|
||
|
||
void Update_Encoders() | ||
{ | ||
Serial.print("e"); | ||
Serial.print("\t"); | ||
Serial.print(Left_Encoder_Ticks); | ||
Serial.print("\t"); | ||
Serial.print(Right_Encoder_Ticks); | ||
Serial.print("\n"); | ||
} | ||
|
||
void do_Left_Encoder() | ||
{ | ||
LeftEncoderBSet = digitalRead(Left_Encoder_PinB); // read the input pin | ||
Left_Encoder_Ticks += LeftEncoderBSet ? -1 : +1; | ||
|
||
} | ||
void do_Right_Encoder() | ||
{ | ||
RightEncoderBSet = digitalRead(Right_Encoder_PinB); // read the input pin | ||
Right_Encoder_Ticks += RightEncoderBSet ? -1 : +1; | ||
} |