Advertisement
mohammedehab2002

Untitled

Mar 11th, 2017
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. int r[5],m;
  2. int line[5]={} //line sensor pins;
  3. int color[2][5]={{},{}} //s0,s1,s2,s3,out
  4. #define left 1
  5. #define right 0
  6. #define f 60
  7. #define F 1
  8. #define B 0
  9. void motors(int d1,int s1,int d2,int s2)
  10. {
  11.   analogWrite(2,s1*!d1);
  12.   analogWrite(3,s1*d1);
  13.   analogWrite(8,s2*d2);
  14.   analogWrite(9,s2*!d2);
  15. }
  16. void linefollower()
  17. {
  18.   for (int i=0;i<5;i++)
  19.   {
  20.     r[i]=digitalRead(line[i]);
  21.     m|=(r[i]<<(4-i));
  22.   }
  23. }
  24. int findLine(int dir)
  25. {
  26.   motors(F*!dir,f,F*dir,f);
  27.   delay(100);
  28.   linefollower();
  29.   while (!r[2])
  30.   linefollower();
  31. }
  32. bool readCol(int sensor)
  33. {
  34.   pulseIn(color[sensor][4],LOW);
  35.   int c=pulseIn(color[sensor][4]);
  36.   return 0; // l mofrood return c green walla l2
  37. }
  38. void intersection(int dir)
  39. {
  40.   motors(0,0,0,0);
  41.   if (readCol(dir))
  42.   findline(dir);
  43.   else
  44.   {
  45.     motors(F,f,F,f);
  46.     delay(100);
  47.     linefollower();
  48.     if (!m)
  49.     findLine(dir);
  50.     else
  51.     motors(F,f,F,f);
  52.   }
  53. }
  54. void intersection2(int dep)
  55. {
  56.   motors(0,0,0,0);
  57.   bool a=readCol(right),B=readCol(left);
  58.   if (a && b)
  59.   {
  60.     motors(F,f,B,f);
  61.     delay(200);
  62.     while (!r[2])
  63.     motors(F,f,B,f);
  64.     motors(F,f,F,f);
  65.   }
  66.   else if (a && !b)
  67.   {
  68.     motors(F,f,F,f);
  69.     delay(100);
  70.     findLine(left);
  71.   }
  72.   else if (!a && b)
  73.   {
  74.     motors(F,f,F,f);
  75.     delay(100);
  76.     findLine(right);
  77.   }
  78.   else if (dep<3)
  79.   {
  80.     motors(B,f,B,f);
  81.     delay(50);
  82.     intersection2(dep+1);
  83.   }
  84. }
  85. void setup()
  86. {
  87.   Serial.begin(9600);
  88.   pinMode(2,OUTPUT);
  89.   pinMode(3,OUTPUT);
  90.   pinMode(8,OUTPUT);
  91.   pinMode(9,OUTPUT);
  92.   for (int i=0;i<5;i++)
  93.   pinMode(line[i],INPUT);
  94.   for (int i=0;i<4;i++)
  95.   {
  96.     pinMode(color[0][i],OUTPUT);
  97.     pinMode(color[1][i],OUTPUT);
  98.   }
  99.   pinMode(color[0][4],INPUT);
  100.   pinMode(color[1][4],INPUT);
  101. }
  102. void loop()
  103. {
  104.   linefollower();
  105.   if (m==0b00000)
  106.   motors(F,f,F,f);
  107.   else if (m==0b00001)
  108.   findLine(left);
  109.   else if (m==0b00010)
  110.   findLine(left);
  111.   else if (m==0b00011)
  112.   intersection(right);
  113.   else if (m==0b00100)
  114.   motors(F,f,F,f);
  115.   else if (m==0b00101)
  116.   intersection(right);
  117.   else if (m==0b00110)
  118.   intersection(right);
  119.   else if (m==0b00111)
  120.   intersection(right);
  121.   else if (m==0b10000)
  122.   findLine(right);
  123.   else if (m==0b01000)
  124.   findLine(left);
  125.   else if (m==0b11000)
  126.   intersection(left);
  127.   else if (m==0b10100)
  128.   intersection(left);
  129.   else if (m==0b01100)
  130.   intersection(left);
  131.   else if (m==0b11100)
  132.   intersection(left);
  133.   else
  134.   intersection2(0);
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement