Advertisement
mohammedehab2002

Untitled

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