Вы находитесь на странице: 1из 5

//Juan de Jesús Chávez Naranjo | Line follower with decision making | Last modified 16/11/18

/* Pins in the stepper board

04: GND (0V)


09: M1 STEP (Full when LOW)
10: M2 STEP (Full when LOW)
11: Reset (when LOW)
12: POWER (Enable when LOW)
13: Direction
14: M1 CLOCK
15: M2 CLOCK
16: M2 Direction
17: Vcc (5V)
18: GND (0V)
19: M1 PHASE (Single when LOW)
20: M2 PHASE (Single when LOW)
*/
/* Wiring (Steppers)

0V: GND (4.18)


5V: STEP (HALF)(9n), Reset (Run normally)(11) & Vcc (17)
Arduino (Digital): Power (12), Directions (13.16) & Clocks (14n)
*/
/* Wiring (Sensors)

0V: GND (Green) via a 220 ohms resistor & Signal (Yellow) via a 10000 ohms (pull-down)
resistor
5V: Vcc (Orange)
Arduino (Digital): Signal (Yellow)
*/
//Arduino pins for the Stepper Control Board (SCB)
const int pwr = 6; //To pin 12 in the SCB (Power enabling)
const int m1D = 5; //To pin 13 in the SCB (Direction of motor 1)
const int m1C = 4; //To pin 14 in the SCB (Clock of the motor 1)
const int m2C = 3; //To pin 15 in the SCB (Clock of the motor 2)
const int m2D = 2; //To pin 16 in the SCB (Direction of motor 2)
//Experimental values for ROBOT #5
const bool m1F = HIGH; //Left motor
const bool m2F = LOW; //Right motor
int dTime = 3; //Minimum delay time supported.
//Arduino pins for the sensors
const int RS = 8; //Right Sensor
const int LS = 9; //Left Sensor
//Counters
int i = 0, j = 0, k = 0;

void setup() { //When we first run our program


pinMode(pwr, OUTPUT); //We set the pin pwr as an output
pinMode(m1D, OUTPUT); //We set the pin m1D as an output
pinMode(m1C, OUTPUT); //We set the pin m1C as an output
pinMode(m2C, OUTPUT); //We set the pin m2C as an output
pinMode(m2D, OUTPUT); //We set the pin m2D as an output
pinMode(RS, INPUT); //We set the pin RS as an input
pinMode(LS, INPUT); //We set the pin LS as an input

digitalWrite(pwr, LOW); //We set the motors off


digitalWrite(m1C, LOW); //We get the first clock to low
digitalWrite(m2C, LOW); //We get the second clock to low
digitalWrite(m1D, m1F); //Set motor one to go forwards
digitalWrite(m2D, m2F); //Set motor two to go forwards
}

void loop() {
int RSRd = digitalRead(RS); //Right Sensor Read
int LSRd = digitalRead(LS); //Left Sensor Read

if (RSRd) { //When the right sensor reads light we step forward once with the right
wheel
digitalWrite(m2C, HIGH); //Right wheel clock HIGH
delay(dTime); //Wait
digitalWrite(m2C, LOW); //Clock LOW
delay(dTime); //Wait
}
if (LSRd) { //When the left sensor reads light we step forward once with the left
wheel
digitalWrite(m1C, HIGH); //Left wheel clock HIGH
delay(dTime); //Wait
digitalWrite(m1C, LOW); //Clock LOW
delay(dTime); //Wait
}
if (!LSRd && !RSRd) { //If none wheel is reading "light" we step forward once.
i++; //Counting how many times in a row we have sensed black in both sides.
k = 0; //Resets each time we get a Two Black sensing
if (i == 20) { //If we sense two blacks in a row, we assume the robot is in an intersection
j++; //Counting how many times we have been in an intersection
delay(2000); //Delay two seconds so if it stops only in intersections, we know it is
working
switch (j) { //”j” may have values from 1 to 10 for we get 10 times to an intersection in
a full loop
case 1: //In the first intersection we force the robot to turn right
for (int t = 0; t < 73; t++) { //We step 73 times forward with only the left wheel
digitalWrite(m1C, HIGH); //Left wheel clock HIGH
delay(dTime); //Wait
digitalWrite(m1C, LOW); //Clock LOW
delay(dTime); //Wait
}
for (int t = 0; i < 20; i++) { //After that in order to be sure it won't come back, we move a
little forward.
digitalWrite(m1C, HIGH); //Clock one goes HIGH
digitalWrite(m2C, HIGH); //Clock two goes HIGH
delay(dTime); //Delay a given time
digitalWrite(m1C, LOW); //Clock goes LOW
digitalWrite(m2C, LOW); //Clock goes LOW
delay(dTime); //Delay
}
break; //We assume we are done with the intersection and continue normally
case 3: //In the third intersection we force the robot to turn right
for (int t = 0; t < 73; t++) { //We step 73 times forward with only the left wheel
digitalWrite(m1C, HIGH); //Left wheel clock HIGH
delay(dTime); //Wait
digitalWrite(m1C, LOW); //Clock LOW
delay(dTime); //Wait
}
for (int t = 0; i < 20; i++) { //After that in order to be sure it won't come back, we move a
little forward.
digitalWrite(m1C, HIGH); //Clock one goes HIGH
digitalWrite(m2C, HIGH); //Clock two goes HIGH
delay(dTime); //Delay a given time
digitalWrite(m1C, LOW); //Clock goes LOW
digitalWrite(m2C, LOW); //Clock goes LOW
delay(dTime); //Delay
}
break; //We assume we are done with the intersection and continue normally
case 8: //In the eighth intersection we force the robot to turn right
for (int t = 0; t < 73; t++) { //We step 73 times forward with only the left wheel
digitalWrite(m1C, HIGH); //Left wheel clock HIGH
delay(dTime); //Wait
digitalWrite(m1C, LOW); //Clock LOW
delay(dTime); //Wait
}
for (int t = 0; i < 20; i++) { //After that in order to be sure it won't come back, we move a
little forward.
digitalWrite(m1C, HIGH); //Clock one goes HIGH
digitalWrite(m2C, HIGH); //Clock two goes HIGH
delay(dTime); //Delay a given time
digitalWrite(m1C, LOW); //Clock goes LOW
digitalWrite(m2C, LOW); //Clock goes LOW
delay(dTime); //Delay
}
break; //We assume we are done with the intersection and continue normally
case 5:
for (int t = 0; t < 50; t++) { //We step fifty times only with the motor in the right
digitalWrite(m2C, HIGH); //Right wheel clock HIGH
delay(dTime); //Wait
digitalWrite(m2C, LOW); //Clock LOW
delay(dTime); //Wait
}
for (int t = 0; i < 20; i++) { //We move 10 steps forward to avoid getting stuck in the
intersection
digitalWrite(m1C, HIGH); //Clock one goes HIGH
digitalWrite(m2C, HIGH); //Clock two goes HIGH
delay(dTime); //Delay a given time
digitalWrite(m1C, LOW); //Clock goes LOW
digitalWrite(m2C, LOW); //Clock goes LOW
delay(dTime); //Wait
}
break; //We assume we are done with the intersection and continue normally
case 10: //The tenth time we get in an intersection we have completed a full cycle
j = 0; //We reset our counter, so the robot will in theory continue the circuit/if we had a
better battery
break; //We finish this case
/*The second, fourth, sixth, seventh, ninth, and tenth times we get to intersections we do
not force any turn
for the normal behavior of the robot is keeping moving forward and that is what we
attempt to achieve*/
}
}
digitalWrite(m1C, HIGH); //Clock one goes HIGH
digitalWrite(m2C, HIGH); //Clock two goes HIGH
delay(dTime); //Delay a given time
digitalWrite(m1C, LOW); //Clock goes LOW
digitalWrite(m2C, LOW); //Clock goes LOW
delay(dTime); //Delay a given time
} else { //If at least one sensor receives light
k++; //We count how times in a row it as happened
}
if (k == 150) { //When we have moved 150 times without two-black sensing
i = 0; //We reset the counter for two-black sensing, meaning we passed the intersection.
}
}

Вам также может понравиться