Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. #include "Grove_I2C_Motor_Driver.h"
  2. #define I2C_ADDRESS 0x0f
  3.  
  4.  
  5. int capteur1 = 3; // connected to digital pin
  6. int capteur2 = 6; // connected to digital pin
  7. int capteur3 = 5; // connected to digital pin
  8. int capteur4 = 7; // connected to digital pin
  9.  
  10.  
  11. void setup() {
  12. Motor.begin(I2C_ADDRESS);
  13. pinMode(capteur1, INPUT); // initialize the digital pin as an output:
  14. pinMode(capteur2, INPUT); // initialize the digital pin as an output:
  15. pinMode(capteur3, INPUT); // initialize the digital pin as an output:
  16. pinMode(capteur4, INPUT); // initialize the digital pin as an output:
  17.  
  18. Serial.begin(9600); // initialize serial communications at 9600 bps:
  19. }
  20.  
  21. void loop() {
  22.  
  23. if (LOW == digitalRead(capteur2) && LOW == digitalRead(capteur3)) {
  24. Motor.speed(MOTOR1, 40);
  25. Motor.speed(MOTOR2, 40);
  26. }
  27.  
  28. if (HIGH == digitalRead(capteur2)) {
  29. Motor.speed(MOTOR2, -40);
  30. Motor.speed(MOTOR1, 70);
  31. }
  32.  
  33.  
  34. if (HIGH == digitalRead(capteur3)) {
  35. Motor.speed(MOTOR2, 70);
  36. Motor.speed(MOTOR1, -40);
  37. }
  38. if (HIGH == digitalRead(capteur3) && HIGH == digitalRead(capteur2)) {
  39. Motor.speed(MOTOR2, 30);
  40. Motor.speed(MOTOR1, 30);
  41. }
  42.  
  43. if (HIGH == digitalRead(capteur4)) {
  44. while (HIGH != digitalRead(capteur3)) {
  45. Motor.speed(MOTOR1, -100);
  46. Motor.speed(MOTOR2, 100);
  47. }
  48. }
  49.  
  50. if (HIGH == digitalRead(capteur1)) {
  51. while (HIGH != digitalRead(capteur2)) {
  52. Motor.speed(MOTOR1, 100);
  53. Motor.speed(MOTOR2, -100);
  54. }
  55. }
  56.  
  57. if (HIGH == digitalRead(capteur1) && HIGH == digitalRead(capteur4)) {
  58.  
  59.  
  60. int alea;
  61. alea = rand() % 2;
  62.  
  63. if ( alea == 0) {
  64. while (HIGH != digitalRead(capteur3)) {
  65. Motor.speed(MOTOR1, 100);
  66. Motor.speed(MOTOR2, -100);
  67.  
  68. }
  69. }
  70.  
  71. if ( alea == 1) {
  72. while (HIGH != digitalRead(capteur2)) {
  73. Motor.speed(MOTOR1, 100);
  74. Motor.speed(MOTOR2, -100);
  75.  
  76. }
  77. }
  78. }
  79. }
  80. 11:18
  81.  
  82. #include "Grove_I2C_Motor_Driver.h"
  83. #define I2C_ADDRESS 0x0f
  84. #define COMPTE_TOUR 2
  85.  
  86.  
  87. int tour = 0;
  88. int capteur1 = 3; // connected to digital pin
  89. int capteur2 = 6; // connected to digital pin
  90. int capteur3 = 5; // connected to digital pin
  91. int capteur4 = 7; // connected to digital pin
  92.  
  93. struct path {
  94. int x;
  95. int y;
  96. };
  97. path voiture = {4, 0}; //coordonnees voiture
  98. path point = {4, 5}; //coordonnees point d'arrivée
  99. path Map = {5, 8};
  100. int direct = 1; //direction de la voiture 1=x+ 2=x- 3=y+ 4=y-
  101.  
  102. void setup() {
  103. Motor.begin(I2C_ADDRESS);
  104. pinMode(capteur1, INPUT); // initialize the digital pin as an output:
  105. pinMode(capteur2, INPUT); // initialize the digital pin as an output:
  106. pinMode(capteur3, INPUT); // initialize the digital pin as an output:
  107. pinMode(capteur4, INPUT); // initialize the digital pin as an output:
  108. attachInterrupt(digitalPinToInterrupt(COMPTE_TOUR), compte_les_inters, RISING);
  109. Serial.begin(9600); // initialize serial communications at 9600 bps:
  110. }
  111.  
  112.  
  113.  
  114. void compte_les_inters() {
  115. tour++;
  116. }
  117.  
  118.  
  119. void loop() {
  120.  
  121.  
  122.  
  123. if (LOW == digitalRead(capteur2) && LOW == digitalRead(capteur3)) {
  124. tour = 0;
  125.  
  126. if (direct == 1) {
  127. Motor.speed(MOTOR1, 100);
  128. Motor.speed(MOTOR2, 100);
  129. voiture.x = voiture.x + (tour / 120);
  130. }
  131. else if (direct == 2) {
  132. Motor.speed(MOTOR1, 100);
  133. Motor.speed(MOTOR2, 100);
  134. voiture.x = voiture.x - (tour / 120);
  135. }
  136. else if (direct == 3) {
  137. Motor.speed(MOTOR1, 100);
  138. Motor.speed(MOTOR2, 100);
  139. voiture.y = voiture.y + (tour / 120);
  140. }
  141. else if (direct == 4) {
  142. Motor.speed(MOTOR1, 100);
  143. Motor.speed(MOTOR2, 100);
  144. voiture.y = voiture.y - (tour / 120);
  145. }
  146. }
  147.  
  148.  
  149.  
  150. if (HIGH == digitalRead(capteur2)) {
  151. Motor.speed(MOTOR2, -40);
  152. Motor.speed(MOTOR1, 70);
  153. }
  154.  
  155.  
  156. if (HIGH == digitalRead(capteur3)) {
  157. Motor.speed(MOTOR2, 70);
  158. Motor.speed(MOTOR1, -40);
  159. }
  160.  
  161. if (HIGH == digitalRead(capteur3) && HIGH == digitalRead(capteur2)) {
  162. Motor.speed(MOTOR2, 25);
  163. Motor.speed(MOTOR1, 25);
  164. }
  165.  
  166. if (HIGH == digitalRead(capteur4)) {
  167. tour = 0;
  168. if (direct == 1 && voiture.y > point.y) {
  169. while (HIGH != digitalRead(capteur3)) {
  170. Motor.speed(MOTOR1, 100);
  171. Motor.speed(MOTOR2, -100);
  172. }
  173. if ((tour / 120) > 0.5) {
  174. direct = 2;
  175. } else {
  176. direct = 1;
  177. }
  178. }
  179.  
  180.  
  181. if (direct == 1 && voiture.y < point.y ) {
  182. while (HIGH != digitalRead(capteur3)) {
  183. Motor.speed(MOTOR1, -100);
  184. Motor.speed(MOTOR2, 100);
  185. direct = 4;
  186.  
  187.  
  188. }
  189. }
  190. if (direct == 1 && (voiture.x + 1) > point.x) {
  191. while (HIGH != digitalRead(capteur3)) {
  192. Motor.speed(MOTOR1, -100);
  193. Motor.speed(MOTOR2, 100);
  194. direct = 4;
  195. }
  196.  
  197. }
  198. if (direct == 2 && voiture.y < point.y) {
  199. while (HIGH != digitalRead(capteur3)) {
  200. Motor.speed(MOTOR1, 100);
  201. Motor.speed(MOTOR2, -100);
  202. direct = 1;
  203. }
  204. if ((tour / 120) > 0.5) {
  205. direct = 1;
  206. } else {
  207. direct = 2;
  208. }
  209. }
  210.  
  211.  
  212.  
  213.  
  214. if (direct == 2 && voiture.y < point.y ) {
  215. while (HIGH != digitalRead(capteur3)) {
  216. Motor.speed(MOTOR1, -100);
  217. Motor.speed(MOTOR2, 100);
  218. direct = 3;
  219.  
  220.  
  221. }
  222. }
  223. if (direct == 2 && (voiture.x + 1) > point.x) {
  224. while (HIGH != digitalRead(capteur3)) {
  225. Motor.speed(MOTOR1, -100);
  226. Motor.speed(MOTOR2, 100);
  227. direct = 3;
  228. }
  229.  
  230. }
  231. if (direct == 3 && voiture.x > point.x ) {
  232.  
  233. while (HIGH != digitalRead(capteur3)) {
  234. Motor.speed(MOTOR1, 100);
  235. Motor.speed(MOTOR2, -100);
  236. direct = 1;
  237. }
  238. if ((tour / 120) > 0.5) {
  239. direct = 4;
  240. } else {
  241. direct = 3;
  242. }
  243.  
  244. }
  245.  
  246. if (direct == 3 && voiture.x < point.x ) {
  247. while (HIGH != digitalRead(capteur3)) {
  248. Motor.speed(MOTOR1, -100);
  249. Motor.speed(MOTOR2, 100);
  250. direct = 1;
  251.  
  252.  
  253. }
  254. }
  255. if (direct == 3 && (voiture.y + 1) > point.y) {
  256. while (HIGH != digitalRead(capteur3)) {
  257. Motor.speed(MOTOR1, -100);
  258. Motor.speed(MOTOR2, 100);
  259. direct = 1;
  260. }
  261.  
  262. }
  263.  
  264. if (direct == 4 && voiture.x < point.x ) {
  265.  
  266.  
  267. while (HIGH != digitalRead(capteur3)) {
  268. Motor.speed(MOTOR1, 100);
  269. Motor.speed(MOTOR2, -100);
  270.  
  271. }
  272. if ((tour / 120) > 0.5) {
  273. direct = 3;
  274. } else {
  275. direct = 4;
  276. }
  277. }
  278.  
  279. if (direct == 4 && voiture.x > point.x ) {
  280. while (HIGH != digitalRead(capteur3)) {
  281. Motor.speed(MOTOR1, -100);
  282. Motor.speed(MOTOR2, 100);
  283. direct = 2;
  284.  
  285.  
  286. }
  287. }
  288. if (direct == 4 && (voiture.y + 1) > point.y) {
  289. while (HIGH != digitalRead(capteur3)) {
  290. Motor.speed(MOTOR1, -100);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement