Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Servo.h>
  3. #include <Wire.h>
  4.  
  5. Servo sL;
  6. Servo sR;
  7. Servo sM;
  8.  
  9. int sLpin = 22;
  10. int sRpin = 23;
  11. int sMpin = 24;
  12.  
  13. int Lref = 92;//92 - stop; <92 - back; >92 - forward
  14. int Rref = 92;//92 - stop; >92 - back; <92 - forward
  15. int Mref = 90;
  16.  
  17.  
  18.  
  19. #define XSensorPin A1
  20. #define YSensorPin A2
  21.  
  22. const byte SlaveDeviceId = 1;
  23. byte LastMasterCommand = 0;
  24.  
  25.  
  26. char str[4];
  27.  
  28. void setup()
  29. {
  30. sL.attach(sLpin);
  31. sR.attach(sRpin);
  32. //sM.attach(sMpin);
  33.  
  34. sL.write(Lref);
  35. sR.write(Rref);
  36. //sM.write(Mref);
  37. //delay(2000);
  38.  
  39. Wire.begin(8); // join i2c bus with address #8
  40. Wire.onReceive(receiveEvent); // register event
  41. Serial.begin(9600); // start serial for output
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48. int x; //variable x determine whether led on master is on or off//
  49.  
  50.  
  51.  
  52.  
  53.  
  54. void loop()
  55. {
  56. delay(100);
  57. }
  58.  
  59. String ss;
  60. String cc;
  61. int a = 0;
  62. int b = 0;
  63.  
  64.  
  65. char c;
  66. int aa;
  67. int bb;
  68. int dd;
  69.  
  70.  
  71.  
  72.  
  73.  
  74. void receiveEvent(int howMany) {
  75. ss = "";
  76.  
  77. while (1 < Wire.available()) { // loop through all but the last
  78.  
  79.  
  80. c = Wire.read(); // receive byte as a character
  81. ss = ss + char(c);
  82. c = Wire.read(); // receive byte as a character
  83. ss = ss + char(c);
  84. c = Wire.read(); // receive byte as a character
  85. ss = ss + char(c);
  86. c = Wire.read(); // receive byte as a character
  87. ss = ss + char(c);
  88. aa = ss.toInt();
  89. // Serial.println(aa);
  90. ss = "";
  91.  
  92. c = Wire.read(); // receive byte as a character
  93. ss = ss + char(c);
  94. c = Wire.read(); // receive byte as a character
  95. ss = ss + char(c);
  96. c = Wire.read(); // receive byte as a character
  97. ss = ss + char(c);
  98. c = Wire.read(); // receive byte as a character
  99. ss = ss + char(c);
  100. bb = ss.toInt();
  101. //Serial.println(bb);
  102. ss = "";
  103.  
  104.  
  105. c = Wire.read(); // receive byte as a character
  106. ss = ss + char(c);
  107. c = Wire.read(); // receive byte as a character
  108. ss = ss + char(c);
  109. c = Wire.read(); // receive byte as a character
  110. ss = ss + char(c);
  111. c = Wire.read(); // receive byte as a character
  112. ss = ss + char(c);
  113. dd = ss.toInt();
  114. // Serial.println(dd);
  115. ss = "";
  116. }
  117. char d = Wire.read(); // receive byte as a character
  118. // Serial.println("---");
  119. int x = Wire.read(); // receive byte as an integer
  120.  
  121.  
  122. aa=map(aa,30,1000,10,-10);
  123. Serial.println(aa);
  124.  
  125. bb=map(bb,30,1000,10,-10);
  126. //Serial.println(bb);
  127.  
  128. dd=map(dd,0,1024,0,180);
  129.  
  130. sL.write(Lref+bb);
  131. sR.write(Rref-bb);
  132.  
  133. if (aa<0)//RIGHT TURN
  134. {
  135. // sL.write(Lref+bb);
  136. //sR.write(Rref-bb+aa);
  137.  
  138. sL.write(Lref+bb);
  139. sR.write(Rref-bb-aa);
  140. }
  141.  
  142. if (aa>0)//LEFT TURN
  143. { sL.write(Lref+bb-aa);
  144. sR.write(Rref-bb);
  145. }
  146.  
  147. //sL.write(Lref-aa);
  148. //sR.write(Rref-bb);
  149.  
  150.  
  151.  
  152.  
  153. //Serial.println(Lref+bb);
  154.  
  155.  
  156. //int Lref = 92;//92 - stop; <92 - back; >92 - forward
  157. //int Rref = 92;//92 - stop; >92 - back; <92 - forward
  158. //int Mref = 90;
  159.  
  160.  
  161.  
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement