Skip to content

Commit fa6a74e

Browse files
committed
commit
1 parent 2be46f4 commit fa6a74e

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
# ArduinoServoDCMotor
1+
## ArduinoServoDCMotor
22
An Arduino servo motor with DC motor and potentiometer.
3+
4+
#Servo_DC_Motor_Pot
5+
Using only proportionality for read/target value.
6+
7+
#Servo_DC_Motor_Pot_Prop
8+
Using proportionality for read/target value and pwm.
9+
10+
Video [https://www.youtube.com/watch?v=m6xne2mSTUo](https://www.youtube.com/watch?v=m6xne2mSTUo)

Servo_DC_Motor_Pot/Servo_DC_Motor_Pot.ino

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
/*
2-
Arduino Servo DC Motor
2+
Arduino Servo DC Motor v1.0
33
Ronaldo Rezende Junior
44
ronaldo.rrj at gmail
55
09/2017
66
https://github.com/naldin
77
*/
88

99
//variables
10-
short int valPot = 0;
10+
short int valRead = 0;
1111
short int motorPWM = 20; //amount of power motor (0-255) were 255 is max.
1212

1313
//constants
1414
const short int wireMT_1 = 5; //for wire motor H bridge
1515
const short int wireMT_2 = 6; //for wire motor H bridge
1616
const short int potMT = A0; //motor potentiometer
1717
const short int potIn = A3; //input potentiometer
18-
const short int diff_error = 20; //max error to potentiometer motor
19-
const short int minPot = 10; //minimal input value
20-
const short int maxPot = 1010; //max input value
21-
const short int maxValIn = 999; //max *potentiometer value
18+
const short int diff_error = 10; //max error to potentiometer motor
19+
const short int minPot = 10; //minimal position potentiometer motor (0-1023)
20+
const short int maxPot = 1010; //max position potentiometer motor (0-1023)
21+
const short int maxValIn = 1023; //max *input value. For this case 1023, the max of pot.
2222
// *here can use something like degress, where max could be 180
2323

2424
//setup
@@ -29,23 +29,22 @@ void setup() {
2929
pinMode (potIn, INPUT); //input potentiometer
3030
digitalWrite (wireMT_1, LOW);
3131
digitalWrite (wireMT_2, LOW);
32-
valPot = analogRead(potMT);
3332
}
3433

3534
//run motor function
36-
void runMotor(short int valIn) {
35+
void runMotor(short int valTarget) {
3736
//proportionality between input and output value
38-
while (valPot <= valIn && abs(valPot - valIn) > diff_error) {
39-
valPot = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
37+
while (valRead <= valTarget && abs(valRead - valTarget) > diff_error) {
38+
valRead = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
4039
//run motor
4140
analogWrite (wireMT_1, motorPWM);
4241
analogWrite (wireMT_2, 0);
4342
}
4443
analogWrite (wireMT_1, 0); //turn off motor
4544

4645
//proportionality between input and output value
47-
while (valPot >= valIn && abs(valPot - valIn) > diff_error) {
48-
valPot = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
46+
while (valRead >= valTarget && abs(valRead - valTarget) > diff_error) {
47+
valRead = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
4948
//run motor
5049
analogWrite (wireMT_2, motorPWM);
5150
analogWrite (wireMT_1, 0);

Servo_DC_Motor_Pot_Prop/Servo_DC_Motor_Pot_Prop.ino

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Arduino Servo DC Motor
2+
Arduino Servo DC Motor v1.0
33
PWM proportional
44
Ronaldo Rezende Junior
55
ronaldo.rrj at gmail
@@ -8,7 +8,7 @@
88
*/
99

1010
//variables
11-
short int valPot = 0;
11+
short int valRead = 0;
1212
short int motorPWM = 0;
1313
short int error = 0; //difference between target value and input value
1414

@@ -19,10 +19,10 @@ const short int wireMT_1 = 5; //for wire motor H bridge
1919
const short int wireMT_2 = 6; //for wire motor H bridge
2020
const short int potMT = A0; //motor potentiometer
2121
const short int potIn = A3; //input potentiometer
22-
const short int diff_error = 20; //max error to potentiometer motor
22+
const short int diff_error = 10; //max error to potentiometer motor
2323
const short int minPot = 10; //minimal input value
2424
const short int maxPot = 1010; //max input value
25-
const short int maxValIn = 999; //max *potentiometer value
25+
const short int maxValIn = 1023; //max *input value. For this case 1023, the max of pot.
2626
// *here can use something like degress, where max could be 180
2727

2828
//setup
@@ -33,16 +33,15 @@ void setup() {
3333
pinMode (potIn, INPUT); //input potentiometer
3434
digitalWrite (wireMT_1, LOW);
3535
digitalWrite (wireMT_2, LOW);
36-
valPot = analogRead(potMT);
3736
}
3837

3938
//run motor function
40-
void runMotor(short int valIn) {
39+
void runMotor(short int valTarget) {
4140
//proportionality between input and output value
42-
while (valPot <= valIn && abs(valPot - valIn) > diff_error) {
43-
valPot = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
41+
while (valRead <= valTarget && abs(valRead - valTarget) > diff_error) {
42+
valRead = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
4443
//pwm proportional
45-
error = valIn - valPot;
44+
error = valTarget - valRead;
4645
motorPWM = ((kp * error) / 100) + mp;
4746
//run motor
4847
analogWrite (wireMT_1, motorPWM);
@@ -51,10 +50,10 @@ void runMotor(short int valIn) {
5150
analogWrite (wireMT_1, 0); //turn off motor
5251

5352
//proportionality between input and output value
54-
while (valPot >= valIn && abs(valPot - valIn) > diff_error) {
55-
valPot = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
53+
while (valRead >= valTarget && abs(valRead - valTarget) > diff_error) {
54+
valRead = (((float)analogRead(potMT) - minPot) / (maxPot - minPot)) * maxValIn;
5655
//pwm proportional
57-
error = valPot - valIn;
56+
error = valRead - valTarget;
5857
motorPWM = ((kp * error) / 100) + mp;
5958
//run motor
6059
analogWrite (wireMT_2, motorPWM);

Stand Servo Motor.stl

19.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)