Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Mert Arduino and Raspberry Pi - Line Following Robot*/
- //Define Pins
- int ENA = 3; //Enable Pin of the Right Motor (must be PWM)
- int RM1 = 1; //Control Pin
- int RM2 = 2;
- int ENB = 6; //Enable Pin of the Left Motor (must be PWM)
- int LM1 = 4;
- int LM2 = 5;
- //Speed of the Motors
- #define ENASpeed 150
- #define ENBSpeed 150
- // we dont want to work with sensor 1 and 6 , it will make more complex the if-else statement
- int Sensor2 = 0;
- int Sensor3 = 0;
- int Sensor4 = 0;
- int Sensor5 = 0;
- void setup() {
- pinMode(ENA, OUTPUT);
- pinMode(RM1, OUTPUT);
- pinMode(RM2, OUTPUT);
- pinMode(ENB, OUTPUT);
- pinMode(LM1, OUTPUT);
- pinMode(LM2, OUTPUT);
- pinMode(Sensor2, INPUT);
- pinMode(Sensor3, INPUT);
- pinMode(Sensor4, INPUT);
- pinMode(Sensor5, INPUT);
- }
- void loop(){
- //Use analogWrite to run motor at adjusted speed
- analogWrite(ENA, ENASpeed);
- analogWrite(ENB, ENBSpeed);
- //Read the Sensor if HIGH (BLACK Line) or LOW (WHITE Line)
- Sensor2 = digitalRead(8);
- Sensor3 = digitalRead(9);
- Sensor4 = digitalRead(10);
- Sensor5 = digitalRead(11);
- //Set conditions for FORWARD, LEFT and RIGHT
- if(Sensor2 == HIGH && Sensor3 == HIGH && Sensor4 == LOW && Sensor5 == LOW){
- //Turn LEFT
- //motor LM Backward
- digitalWrite(LM1, LOW);
- digitalWrite(LM2, HIGH);
- //motor RM Forward
- digitalWrite(RM1, HIGH);
- digitalWrite(RM2, LOW);
- }
- else if (Sensor2 == LOW && Sensor3 == LOW && Sensor4 == HIGH && Sensor5 == HIGH){
- //Turn RIGHT
- //motor LM Forward
- digitalWrite(LM1, HIGH);
- digitalWrite(LM2, LOW);
- //motor RM Backward
- digitalWrite(RM1, LOW);
- digitalWrite(RM2, HIGH);
- }
- else{
- //if(Sensor2 == LOW && Sensor3 == HIGH && Sensor4 == HIGH && Sensor5 == LOW
- //FORWARD
- digitalWrite(LM1, HIGH);
- digitalWrite(LM2, LOW);
- digitalWrite(RM1, HIGH);
- digitalWrite(RM2, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement