Advertisement
mohammedehab2002

Untitled

Mar 12th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. int r[5],m,t;
  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. void 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],HIGH);
  35.   pulseIn(color[sensor][4],LOW);
  36.   int c=pulseIn(color[sensor][4],HIGH);
  37.   return 0; // l mofrood return c green walla l2
  38. }
  39. void intersection(int dir)
  40. {
  41.   motors(0,0,0,0);
  42.   if (readCol(dir))
  43.   {
  44.     motors(F,f,F,f);
  45.     delay(100);
  46.     findLine(dir);
  47.   }
  48.   else
  49.   {
  50.     motors(F,f,F,f);
  51.     delay(100);
  52.     linefollower();
  53.     if (!m)
  54.     findLine(dir);
  55.     else
  56.     motors(F,f,F,f);
  57.   }
  58. }
  59. bool intersection2(int dep)
  60. {
  61.   bool found=1;
  62.   motors(0,0,0,0);
  63.   bool a=readCol(right),b=readCol(left);
  64.   if (a && b)
  65.   findLine(right);
  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
  79.   {
  80.     if (dep>=3)
  81.     return 0;
  82.     motors(B,f,B,f);
  83.     delay(50);
  84.     found=intersection2(dep+1);
  85.   }
  86.   if (!dep && !found)
  87.   {
  88.     motors(F,f,F,f);
  89.     delay(250);
  90.   }
  91. }
  92. int popcount(int mask)
  93. {
  94.   int c=0;
  95.   while (mask)
  96.   {
  97.     mask&=(mask-1);
  98.     c++;
  99.   }
  100.   return (c>2);
  101. }
  102. void setup()
  103. {
  104.   Serial.begin(9600);
  105.   pinMode(2,OUTPUT);
  106.   pinMode(3,OUTPUT);
  107.   pinMode(8,OUTPUT);
  108.   pinMode(9,OUTPUT);
  109.   for (int i=0;i<5;i++)
  110.   pinMode(line[i],INPUT);
  111.   for (int i=0;i<4;i++)
  112.   {
  113.     pinMode(color[0][i],OUTPUT);
  114.     pinMode(color[1][i],OUTPUT);
  115.   }
  116.   pinMode(color[0][4],INPUT);
  117.   pinMode(color[1][4],INPUT);
  118. }
  119. void loop()
  120. {
  121.   m=0;
  122.   linefollower();
  123.   t=((t>>1)|(popcount(m)<<2));
  124.   if ((bool)(t&(1<<2))!=(bool)(t&(1<<1)))
  125.   {
  126.     motors(F,f,F,f);
  127.     delay(50);
  128.     linefollower();
  129.     t=((t>>1)|(popcount(m)<<2));
  130.     motors(B,f,B,f);
  131.     delay(50);
  132.   }
  133.   if (popcount(t))
  134.   {
  135.     for (int i=0;i<5;i++)
  136.     m^=(1<<i);
  137.   }
  138.   if (m==0b00000)
  139.   motors(F,f,F,f);
  140.   else if (m==0b00001)
  141.   findLine(left);
  142.   else if (m==0b00010)
  143.   findLine(left);
  144.   else if (m==0b00011)
  145.   intersection(right);
  146.   else if (m==0b00100)
  147.   motors(F,f,F,f);
  148.   else if (m==0b00101)
  149.   intersection(right);
  150.   else if (m==0b00110)
  151.   intersection(right);
  152.   else if (m==0b00111)
  153.   intersection(right);
  154.   else if (m==0b10000)
  155.   findLine(right);
  156.   else if (m==0b01000)
  157.   findLine(left);
  158.   else if (m==0b11000)
  159.   intersection(left);
  160.   else if (m==0b10100)
  161.   intersection(left);
  162.   else if (m==0b01100)
  163.   intersection(left);
  164.   else if (m==0b11100)
  165.   intersection(left);
  166.   else
  167.   intersection2(0);
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement