Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Basic Line follow Robot as in video here https://youtu.be/zWyry58pARk
- Hardware:
- 1 x Arduino Uno
- 2 x IR sensors
- 1 x Motor driver (L298N)
- 1 x Board or perspex for chassis
- 1 x switch to suit
- 1 x small breadboard or screw tag strip
- 1 x 2S LiPo 7.4V battery
- Misc:
- Mounting hardware
- hookup wires
- double sided tape and/or hot glue
- Because we are using the L298N motor driver we use 6 x pins
- Enable Left and Right are the PWM speed control pins = 2 pins
- Forward Left and Right = 2 pins
- Reverse Left and Right = 2 pins
- Always check your wiring and jumper settings several times to be sure
- */
- //PWM Values - Change value to suit speed typically between 150 and 200 with the full range being 0 to 255
- #define PULSE_PWM 190 //a 20mS pulse of this value to overcome inertia for the slower run value
- #define PWM_RUN 150 //this is the run value
- //Uno Pins
- #define LEFT_EN 10 //see note in setup
- #define LEFT_FWD 8
- #define LEFT_REV 6
- #define RIGHT_EN 9 //see note in setup
- #define RIGHT_FWD 12
- #define RIGHT_REV 7
- #define LEFT_IR 3
- #define RIGHT_IR 2
- bool right_sensor; //Variable holds right IR sensor state
- bool left_sensor; //Variable holds left IR sensor state
- bool lastLeftSensor = 1; //So we only do an action when a state changes
- bool lastRightSensor = 1; //So we only do an action when a state changes
- void setup(){
- pinMode(LEFT_EN, OUTPUT); //Left Motor PWM Pin
- pinMode(LEFT_FWD, OUTPUT); //Left Motor Forward Pin
- pinMode(LEFT_REV, OUTPUT); //Left Motor Reverse Pin
- pinMode(RIGHT_EN, OUTPUT); //Right Motor PWM Pin
- pinMode(RIGHT_FWD, OUTPUT); //Right Motor Forward Pin
- pinMode(RIGHT_REV, OUTPUT); //Right Motor Reverse Pin
- pinMode(LEFT_IR, INPUT); //IR Sensor Left
- pinMode(RIGHT_IR, INPUT); //IR Sensor Right
- /* Note PWM timers are in pairs as noted
- This Direct Registar Config changes PWM from 1khz to 7.8khz carrier frequency */
- //TCCR0B = TCCR0B & B11111000 | B00000010 ; //TIMER0 for PWM @ 7812.5 hz on D5 and D9
- TCCR1B = TCCR1B & B11111000 | B00000010 ; //TIMER1 for PWM @ 7812.5 hz on D9 and D10
- //TCCR2B = TCCR2B & B11111000 | B00000010 ; //TIMER2 for PWM @ 7812.5 hz on D3 and D11
- //Start in OFF state
- digitalWrite(LEFT_EN, LOW); //Left Motor PWM Pin
- digitalWrite(LEFT_FWD, LOW); //Left Motor Forward Pin
- digitalWrite(LEFT_REV, LOW); //Left Motor Forward Pin
- digitalWrite(RIGHT_EN, LOW); //Left Motor PWM Pin
- digitalWrite(RIGHT_FWD, LOW); //Left Motor Forward Pin
- digitalWrite(RIGHT_REV, LOW); //Left Motor Forward Pin
- delay(1000); //Pause before loop starts
- }
- void loop(){
- left_sensor = digitalRead(LEFT_IR); //get left IR sensor state
- right_sensor = digitalRead(RIGHT_IR); //get right IR sensor state
- //This sets up a state engine so we only drop inside if one state changes
- // The "!=" is Not Equal to and the "||" is a logical OR
- if(left_sensor != lastLeftSensor || right_sensor != lastRightSensor){
- //Straght ahead
- if (left_sensor == 0 && right_sensor == 0) { //If true both motor forward
- digitalWrite(LEFT_FWD, HIGH); //Set Left Forward HIGH
- digitalWrite(RIGHT_FWD, HIGH); //Set Right Forward HIGH
- digitalWrite(LEFT_REV, LOW); //Left Reverse LOW
- digitalWrite(RIGHT_REV, LOW); //Right Reverse LOW
- analogWrite(RIGHT_EN, PULSE_PWM); //PWM Pulse Right Motor
- analogWrite(LEFT_EN, PULSE_PWM); //PWM Pulse Left Motor
- delay(20);
- analogWrite(RIGHT_EN, PWM_RUN); //PWM Run Right Motor
- analogWrite(LEFT_EN, PWM_RUN); //PWM Run Left Motor
- }
- //Turn Right - Left Sensor OFF - Left REV HIGH & Right FWD HIGH
- if (left_sensor == 1 && right_sensor == 0) {
- digitalWrite(LEFT_FWD, LOW); //
- digitalWrite(RIGHT_FWD, HIGH);
- digitalWrite(LEFT_REV, HIGH);
- digitalWrite(RIGHT_REV, LOW);
- analogWrite(RIGHT_EN, PULSE_PWM); //PWM Pulse Right Motor
- analogWrite(LEFT_EN, PULSE_PWM); //PWM Pulse Left Motor
- delay(20);
- analogWrite(RIGHT_EN, PWM_RUN); //PWM Run Right Motor
- analogWrite(LEFT_EN, PWM_RUN); //PWM Run Left Motor
- }
- //Turn Left - Right Sensor OFF - Left FWD HIGH & Right REV HIGH
- if (left_sensor == 0 && right_sensor == 1) {
- digitalWrite(LEFT_FWD, HIGH);
- digitalWrite(RIGHT_FWD, LOW);
- digitalWrite(LEFT_REV, LOW);
- digitalWrite(RIGHT_REV, HIGH);
- analogWrite(RIGHT_EN, PULSE_PWM); //PWM Pulse Right Motor
- analogWrite(LEFT_EN, PULSE_PWM); //PWM Pulse Left Motor
- delay(20);
- analogWrite(RIGHT_EN, PWM_RUN); //PWM Run Right Motor
- analogWrite(LEFT_EN, PWM_RUN); //PWM Run Left Motor
- }
- //All Stop - All motor pins set LOW
- if (left_sensor == 1 && right_sensor == 1) {
- digitalWrite(LEFT_FWD, LOW);
- digitalWrite(RIGHT_FWD, LOW);
- digitalWrite(LEFT_REV, LOW);
- digitalWrite(RIGHT_REV, LOW);
- digitalWrite(RIGHT_EN, LOW);
- digitalWrite(LEFT_EN, LOW);
- }
- }
- lastRightSensor = right_sensor; //creates the state engine for right IR sensor
- lastLeftSensor = left_sensor; //creates the state engine for left IR sensor
- }
Advertisement
Add Comment
Please, Sign In to add comment