daily pastebin goal
38%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Line Follower with Avoider
  2.  
  3.  
  4. //motor Speed
  5. int HighSpeed = 140; //max 255 std HML 110 90 70 HS 160 100 80
  6. int MidSpeed = 110;
  7. int LowSpeed = 110;
  8. int SpeedDelay = 10;
  9. int stop_distance = 8;
  10.  
  11.  
  12.  
  13. // L298N Control Pins
  14. const int MotorKiriMaju = 5;
  15. const int MotorKiriMundur = 4;
  16. const int MotorKananMaju = 3;
  17. const int MotorKananMundur = 2;
  18. const int SpeedKiri = 9;
  19. const int SpeedKanan = 10;
  20.  
  21.  
  22. //HC-SR04 Sensor connection
  23.   const int trigPin = 11;
  24.   const int echoPin = 12;  
  25.  
  26. // Line Sensor Pins
  27.   const int lefts = 6;
  28.   const int rights = 7;
  29.  
  30.   long duration;
  31.   int distance;
  32.  
  33.   int left_sensor_state;
  34.   int right_sensor_state;
  35.  
  36. void setup() {
  37.   pinMode(MotorKananMundur,OUTPUT);
  38.   pinMode(MotorKananMaju,OUTPUT); //mo ka maju
  39.   pinMode(MotorKiriMundur,OUTPUT);
  40.   pinMode(MotorKiriMaju,OUTPUT); //mo ki maju
  41.  
  42.   pinMode(lefts,INPUT);
  43.   pinMode(rights,INPUT);
  44.  
  45.   pinMode(trigPin, OUTPUT);
  46.   pinMode(echoPin, INPUT);
  47.   //begin serial communication
  48.   Serial.begin(9600);
  49.   delay(3000);
  50.  
  51. }
  52.  
  53. void maju()
  54. {
  55.   Serial.println("going forward");
  56.   digitalWrite(MotorKiriMaju,HIGH);
  57.   digitalWrite(MotorKananMaju,HIGH);
  58.   digitalWrite(MotorKananMundur,LOW);
  59.   digitalWrite(MotorKiriMundur,LOW);
  60.   analogWrite (SpeedKiri, HighSpeed);
  61.   analogWrite (SpeedKanan, HighSpeed);
  62.   delay(SpeedDelay);
  63. }
  64.  
  65. void majutd() //tanpa delay
  66. {
  67.   Serial.println("going forward");
  68.   digitalWrite(MotorKiriMaju,HIGH);
  69.   digitalWrite(MotorKananMaju,HIGH);
  70.   digitalWrite(MotorKananMundur,LOW);
  71.   digitalWrite(MotorKiriMundur,LOW);
  72.   analogWrite (SpeedKiri, HighSpeed);
  73.   analogWrite (SpeedKanan, HighSpeed);
  74.  
  75. }
  76.  
  77. void mundur()
  78. {
  79.   digitalWrite(MotorKiriMundur,HIGH);
  80.   digitalWrite(MotorKananMundur,HIGH);
  81.   digitalWrite(MotorKiriMaju,LOW);
  82.   digitalWrite(MotorKananMaju,LOW);
  83.   analogWrite (SpeedKiri, HighSpeed);
  84.   analogWrite (SpeedKanan, HighSpeed);
  85. }
  86.  
  87. void kanan()
  88. {
  89.   Serial.println("turning right");
  90.   digitalWrite(MotorKiriMaju,HIGH);
  91.   digitalWrite(MotorKananMundur,HIGH);
  92.   digitalWrite(MotorKananMaju,LOW);
  93.   digitalWrite(MotorKiriMundur,LOW);
  94.   analogWrite (SpeedKiri, MidSpeed);
  95.   analogWrite (SpeedKanan, LowSpeed);
  96.   //delay(SpeedDelay);
  97. }
  98.  
  99. void kiri()
  100. {
  101.   Serial.println("turning left");
  102.   digitalWrite(MotorKananMaju,HIGH);
  103.   digitalWrite(MotorKiriMundur,HIGH);
  104.   digitalWrite(MotorKiriMaju,LOW);
  105.   digitalWrite(MotorKananMundur,LOW);
  106.   analogWrite (SpeedKiri, LowSpeed);
  107.   analogWrite (SpeedKanan, MidSpeed);
  108.   delay(SpeedDelay);
  109. }
  110.  
  111. void berhenti()
  112. {
  113.  
  114.   Serial.println("stop");
  115.   digitalWrite(MotorKananMaju,LOW);
  116.   digitalWrite(MotorKiriMundur,LOW);
  117.   digitalWrite(MotorKiriMaju,LOW);
  118.   digitalWrite(MotorKananMundur,LOW);
  119. }
  120.  
  121. void loop(){
  122. left_sensor_state = digitalRead(lefts);
  123. right_sensor_state = digitalRead(rights);
  124.  
  125.  
  126.   //sensor jarak
  127.   digitalWrite(trigPin, LOW);
  128.   delayMicroseconds(2);
  129.   digitalWrite(trigPin, HIGH);
  130.   delayMicroseconds(10);
  131.   digitalWrite(trigPin, LOW);
  132.   duration = pulseIn(echoPin, HIGH);
  133.   distance= duration*0.034/2;
  134.   Serial.print("Distance: ");
  135.   Serial.println(distance);
  136.  
  137.   //line detected by both
  138.   if(right_sensor_state == LOW && left_sensor_state == LOW){
  139.    
  140.     maju();
  141.   }
  142.  
  143.   //line detected by right sensor
  144.   if(right_sensor_state == HIGH && left_sensor_state == LOW){
  145.  
  146.     kanan();
  147.   }
  148.  
  149.   //line detected by left sensor
  150.   if(right_sensor_state == LOW && left_sensor_state == HIGH){
  151.    
  152.     kiri();
  153.   }
  154.  
  155.   //line detected by none
  156.   if(right_sensor_state == HIGH && left_sensor_state == HIGH){
  157.     //stop
  158.     berhenti();
  159. //    while(true){
  160. //      } //berhenti total
  161.   }
  162.  
  163.  
  164.  if(distance < stop_distance)
  165.  {
  166.  
  167.   mundur();
  168.   delay(300);
  169.   berhenti();
  170.   delay(300);
  171.   kiri();
  172.   delay(300);
  173.   maju();
  174.   delay(600);
  175.   kanan();
  176.   delay(300);
  177.   maju();
  178.   delay(800);
  179.   kanan();
  180.   delay(300);
  181.   majutd();
  182.  
  183.  
  184.   left_sensor_state == HIGH;
  185.  
  186.   while(left_sensor_state == LOW){
  187.  
  188.   left_sensor_state = digitalRead(lefts);
  189.   right_sensor_state = digitalRead(rights);
  190.   Serial.println("in the first while");
  191.  
  192. }
  193.         mundur();
  194.         delay(100);
  195.         kiri();
  196.         delay (300);        
  197. }
  198. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top