Advertisement
mohammedehab2002

Untitled

Mar 13th, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. int r[5],m,bm,col;
  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.   if (m==0b00100 && bm==0b11011)
  24.   col=0;
  25.   if (m==0b11011 && bm==0b00100)
  26.   col=1;
  27.   if (col)
  28.   {
  29.     for (int i=0;i<5;i++)
  30.     m^=(1<<i);
  31.   }
  32. }
  33. void findLine(int dir)
  34. {
  35.   motors(F*!dir,f,F*dir,f);
  36.   delay(100);
  37.   linefollower();
  38.   while (!r[2])
  39.   linefollower();
  40. }
  41. bool readCol(int sensor)
  42. {
  43.   bool r=0;
  44.   motors(B,f,B,f);
  45.   delay(50);
  46.   for (int i=0;i<3;i++)
  47.   {
  48.     motors(0,0,0,0);
  49.     pulseIn(color[sensor][4],HIGH);
  50.     pulseIn(color[sensor][4],LOW);
  51.     int c=pulseIn(color[sensor][4],HIGH);
  52.     r|=1; // l mofrood howwa c green walla l2 m4 1;
  53.     motors(F,f,F,f);
  54.     delay(50);
  55.   }
  56.   motors(B,f,B,f);
  57.   delay(100);
  58.   motors(0,0,0,0);
  59.   return r;
  60. }
  61. void intersection(int dir)
  62. {
  63.   if (readCol(dir))
  64.   {
  65.     motors(F,f,F,f);
  66.     delay(100);
  67.     findLine(dir);
  68.   }
  69.   else
  70.   {
  71.     motors(F,f,F,f);
  72.     delay(100);
  73.     linefollower();
  74.     if (!m)
  75.     findLine(dir);
  76.     else
  77.     motors(F,f,F,f);
  78.   }
  79. }
  80. bool intersection2()
  81. {
  82.   bool a=readCol(right),b=readCol(left);
  83.   if (a && b)
  84.   findLine(right);
  85.   else if (a && !b)
  86.   {
  87.     motors(F,f,F,f);
  88.     delay(100);
  89.     findLine(left);
  90.   }
  91.   else if (!a && b)
  92.   {
  93.     motors(F,f,F,f);
  94.     delay(100);
  95.     findLine(right);
  96.   }
  97.   else
  98.   motors(F,f,F,f);
  99. }
  100. void setup()
  101. {
  102.   Serial.begin(9600);
  103.   pinMode(2,OUTPUT);
  104.   pinMode(3,OUTPUT);
  105.   pinMode(8,OUTPUT);
  106.   pinMode(9,OUTPUT);
  107.   for (int i=0;i<5;i++)
  108.   pinMode(line[i],INPUT);
  109.   for (int i=0;i<4;i++)
  110.   {
  111.     pinMode(color[0][i],OUTPUT);
  112.     pinMode(color[1][i],OUTPUT);
  113.   }
  114.   pinMode(color[0][4],INPUT);
  115.   pinMode(color[1][4],INPUT);
  116.   bm=0b00100;
  117. }
  118. void loop()
  119. {
  120.   bm=m;
  121.   m=0;
  122.   linefollower();
  123.   if (m==0b00000)
  124.   motors(F,f,F,f);
  125.   else if (m==0b00001)
  126.   findLine(left);
  127.   else if (m==0b00010)
  128.   findLine(left);
  129.   else if (m==0b00011)
  130.   intersection(right);
  131.   else if (m==0b00100)
  132.   motors(F,f,F,f);
  133.   else if (m==0b00101)
  134.   intersection(right);
  135.   else if (m==0b00110)
  136.   intersection(right);
  137.   else if (m==0b00111)
  138.   intersection(right);
  139.   else if (m==0b10000)
  140.   findLine(right);
  141.   else if (m==0b01000)
  142.   findLine(left);
  143.   else if (m==0b11000)
  144.   intersection(left);
  145.   else if (m==0b10100)
  146.   intersection(left);
  147.   else if (m==0b01100)
  148.   intersection(left);
  149.   else if (m==0b11100)
  150.   intersection(left);
  151.   else
  152.   intersection2();
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement