forked from vinay-lanka/navbot_hardware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up comments & updated variable
Cleaned up some of the comments, adjusted formatting to add whitespace. Changed Motor class member en_a to enc_a and en_b to enc_b to avoid confusion with L293D Enable pins.
- Loading branch information
Showing
5 changed files
with
145 additions
and
49 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,88 @@ | ||
// Motor A connections | ||
int enA = 9; | ||
int in1 = 9; | ||
int in2 = 8; | ||
// Motor B connections | ||
int enB = 3; | ||
int in3 = 10; | ||
int in4 = 11; | ||
|
||
void setup() { | ||
// Set all the motor control pins to outputs | ||
pinMode(enA, OUTPUT); | ||
pinMode(enB, OUTPUT); | ||
pinMode(in1, OUTPUT); | ||
pinMode(in2, OUTPUT); | ||
pinMode(in3, OUTPUT); | ||
pinMode(in4, OUTPUT); | ||
|
||
// Turn off motors - Initial state | ||
digitalWrite(in1, LOW); | ||
digitalWrite(in2, LOW); | ||
digitalWrite(in3, LOW); | ||
digitalWrite(in4, LOW); | ||
} | ||
|
||
void loop() { | ||
directionControl(); | ||
delay(1000); | ||
speedControl(); | ||
delay(1000); | ||
} | ||
|
||
// This function lets you control spinning direction of motors | ||
void directionControl() { | ||
// Set motors to maximum speed | ||
// For PWM maximum possible values are 0 to 255 | ||
analogWrite(enA, 255); | ||
analogWrite(enB, 255); | ||
|
||
// Turn on motor A & B | ||
digitalWrite(in1, HIGH); | ||
digitalWrite(in2, LOW); | ||
digitalWrite(in3, HIGH); | ||
digitalWrite(in4, LOW); | ||
delay(2000); | ||
|
||
// Now change motor directions | ||
digitalWrite(in1, LOW); | ||
digitalWrite(in2, HIGH); | ||
digitalWrite(in3, LOW); | ||
digitalWrite(in4, HIGH); | ||
delay(2000); | ||
|
||
// Turn off motors | ||
digitalWrite(in1, LOW); | ||
digitalWrite(in2, LOW); | ||
digitalWrite(in3, LOW); | ||
digitalWrite(in4, LOW); | ||
} | ||
|
||
// This function lets you control speed of the motors | ||
void speedControl() { | ||
// Turn on motors | ||
digitalWrite(in1, LOW); | ||
digitalWrite(in2, HIGH); | ||
digitalWrite(in3, LOW); | ||
digitalWrite(in4, HIGH); | ||
|
||
// Accelerate from zero to maximum speed | ||
for (int i = 0; i < 256; i++) { | ||
analogWrite(enA, i); | ||
analogWrite(enB, i); | ||
delay(20); | ||
} | ||
|
||
// Decelerate from maximum speed to zero | ||
for (int i = 255; i >= 0; --i) { | ||
analogWrite(enA, i); | ||
analogWrite(enB, i); | ||
delay(20); | ||
} | ||
|
||
// Now turn off motors | ||
digitalWrite(in1, LOW); | ||
digitalWrite(in2, LOW); | ||
digitalWrite(in3, LOW); | ||
digitalWrite(in4, LOW); | ||
} |
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
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,31 @@ | ||
/* | ||
Motor.h - Library for working with the Cytron SPG30E-30K or other DC motor with quadrant encoder. | ||
Created by Vinay Lanka, January 27, 2021. | ||
Update - Dingo 21 November 2021 | ||
+ en_a and en_b updated to enc_a and enc_b to avoid confusion with "Enable A and Enable B on the L293D motor driver (which is not used) | ||
+ comments update with details | ||
*/ | ||
#ifndef Motor_h | ||
#define Motor_h | ||
|
||
#include "Arduino.h" | ||
|
||
class Motor { | ||
public: | ||
//Constructor for Motor - 'Plus' and 'Minus' are the Motor pins to connect the output to / enc_a and enc_b are the encoder inputs | ||
Motor(int plus, int minus, int enc_a, int enc_b); | ||
|
||
//Rotate motor shaft based on a percentage value | ||
void rotate(int value); | ||
|
||
//Motor Outputs - plus is one direction and minus is the other | ||
int plus; | ||
int minus; | ||
|
||
//Encoder Inputs | ||
int enc_a; | ||
int enc_b; | ||
}; | ||
|
||
#endif |
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