|
| 1 | +// Right motor pin assignments |
| 2 | +int pwmR = 3; |
| 3 | +const int L1 = 5; |
| 4 | +const int L2 = 6; |
| 5 | + |
| 6 | +// Left motor pin assignments |
| 7 | +int pwmL = 11; |
| 8 | +const int R1 = 9; |
| 9 | +const int R2 = 10; |
| 10 | + |
| 11 | +//Joystick Pin assignments |
| 12 | +#define joyX A0 |
| 13 | +#define joyY A1 |
| 14 | + |
| 15 | +// initializing the variable width and length for the rectangular path |
| 16 | +int width_counter = 0; |
| 17 | +int length_counter = 0; |
| 18 | + |
| 19 | +// Variables that control the dimensions of the rectangle path |
| 20 | +int length = 2; |
| 21 | +int width = 2; |
| 22 | + |
| 23 | +// Variables that control the radius of the circle. |
| 24 | +float radius = 1.5; |
| 25 | + |
| 26 | +// Functions to control motor output |
| 27 | + |
| 28 | +// 1 means backward |
| 29 | +// 0 means stop |
| 30 | +// -1 means forward |
| 31 | +void motorR(int speed, int direction) { |
| 32 | + analogWrite(pwmR, speed); |
| 33 | + |
| 34 | + if (direction == -1) { |
| 35 | + digitalWrite(R1, HIGH); |
| 36 | + digitalWrite(R2, LOW); |
| 37 | + } else if (direction == 1) { |
| 38 | + digitalWrite(R1, LOW); |
| 39 | + digitalWrite(R2, HIGH); |
| 40 | + } else if (direction == 0) { |
| 41 | + digitalWrite(R1, LOW); |
| 42 | + digitalWrite(R2, LOW); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void motorL(int speed, int direction) { |
| 47 | + analogWrite(pwmL, speed); |
| 48 | + |
| 49 | + if (direction == -1) { |
| 50 | + digitalWrite(L1, LOW); |
| 51 | + digitalWrite(L2, HIGH); |
| 52 | + } else if (direction == 1) { |
| 53 | + digitalWrite(L1, HIGH); |
| 54 | + digitalWrite(L2, LOW); |
| 55 | + } else if (direction == 0) { |
| 56 | + digitalWrite(L1, LOW); |
| 57 | + digitalWrite(L2, LOW); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +void path_rectangle(int width, int length) { |
| 62 | + for (int i = 0; i < 5; i++) { // completes 1/2 of the rectangle six times to make three complete loops |
| 63 | + goforward(length); |
| 64 | + stop(); |
| 65 | + turnleft(); |
| 66 | + stop(); |
| 67 | + goforward(width); |
| 68 | + stop(); |
| 69 | + turnleft(); |
| 70 | + stop(); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void path_circle(float radius){ |
| 75 | + int pwmL = 150; |
| 76 | + int pwmR = pwmL*radius; |
| 77 | + motorR(pwmR, -1); |
| 78 | + motorL(pwmL, -1); |
| 79 | +} |
| 80 | + |
| 81 | +void goforward(int distance) { |
| 82 | + int pwmR = 150; |
| 83 | + int pwmL = 150; |
| 84 | + motorR(pwmR, -1); |
| 85 | + motorL(pwmL, -1); |
| 86 | + delay(distance * 1000); |
| 87 | +} |
| 88 | + |
| 89 | +void turnleft() { |
| 90 | + int pwm = 150; |
| 91 | + motorR(pwm, -1); |
| 92 | + motorL(pwm, 0); |
| 93 | + delay(1000); |
| 94 | +} |
| 95 | + |
| 96 | +void stop() { |
| 97 | + motorR(0, 0); |
| 98 | + motorL(0, 0); |
| 99 | + delay(500); |
| 100 | +} |
| 101 | + |
| 102 | +void setup() { |
| 103 | + Serial.begin(115200); |
| 104 | + |
| 105 | + pinMode(pwmL, OUTPUT); //sets the left motor pin as a PWM output |
| 106 | + pinMode(L1, OUTPUT); // sets the logic pins for motor 1 |
| 107 | + pinMode(L2, OUTPUT); |
| 108 | + |
| 109 | + pinMode(pwmR, OUTPUT); //sets the right motor pin as a PWM output |
| 110 | + pinMode(R1, OUTPUT); // sets the logic pins for motor 2 |
| 111 | + pinMode(R2, OUTPUT); |
| 112 | +} |
| 113 | + |
| 114 | +void loop() { |
| 115 | + path_rectangle(width, length); |
| 116 | + path_circle(radius); |
| 117 | + //function for parellel parking |
| 118 | +} |
0 commit comments