Stap 4: Code
1. #include
2. #include
3.
4. //UART PROTOCOL / / /
5. #define UART_FLAG 0XFF
6. //Moto////////////////////////////////
7. / / PROTO: VLAG DEV RICHTING LEGE VLAG
8. #define MOTO 0X00
9. #define vooruit 0X01 //MOTO opdracht
10. #define ACHTERWAARTSE 0X02
11. #define TURNLEFT 0X03
12. #define TURNRIGHT 0X04
13. #define CARSTOP 0X00
14. //Servo///////////////////////////////
15. / / PROTO: VLAG DEV SERVONUM POS VLAG
16. #define SERVO 0X01
17. //Moto snelheid / / /
18. / / PROTO: VLAG DEV MOTOSIDE SNELHEID VLAG
19. #define MOTOSPEED 0X02
20. //////////////////////////////////////
21. int n = 1;
22. int. flagCount = 0;
23. int. tempData = 0;
24. int. UARTReveived = 0;
25. int rxData [5];
26. //-------------------define motor----------------------------------------------//
27. AF_DCMotor motorL(3,MOTOR12_8KHZ); verbinden met M3
28. AF_DCMotor motorR(4,MOTOR12_8KHZ); verbinding maken met M4
29. int. motor_speed = 200; [modifid] motorsnelheid 150-200,---min:100; max: 255
30. int. motor_delay = 400; [modifid] vertragingstijd in stap
31.
32. //-------------------define servo----------------------------------------------//
33. servo hand_t_servo; maken van servo-object om te bepalen van een servo
34. servo hand_d_servo; maken van servo-object om te bepalen van een servo
35. int. hand_t_pos = 90; //
36. int. hand_d_pos = 90; //
37. int. hand_delay = 1; [modifid] snelheid van hand
38.
39. //------------------main program-----------------------------------------------//
40. void loop
41. {}
42. if(Serial.available())
43. {}
44. tempData = Serial.read();
45. delay(3);
46. if(tempData == UART_FLAG && flagCount < 2)
47. {}
48. rxData [0] = tempData;
49. flagCount ++;
50.}
51. anders
52. {}
53. rxData [n] = tempData;
54. n ++;
55.}
56. if(flagCount == 2)
57. {}
58. rxData [4] == UART_FLAG;
59. UARTReveived = 1;
60. de n = 1;
61. flagCount = 0;
62. tempData = 0;
63. Serial.flush();
64.}
65.}
66. if(UARTReveived == 1)
67. {}
68. Serial.print("rxData:");
69. Serial.print(rxData[0]);
70. Serial.println(rxData[1]);
71. if(rxData[1] == MOTO)
72. {}
73. switch(rxData[2])
74. {}
75. case naar voren:
76. carGoFwd();
77. pauze;
78. case achterwaarts:
79. carGoBwd();
80. pauze;
81. case TURNLEFT:
82. carTurnL();
83. pauze;
84. case TURNRIGHT:
85. carTurnR();
86. pauze;
87. case CARSTOP:
88. carStop();
89. pauze;
90.}
91. UARTReveived = 0;
92.}
93. anders als (rxData [1] == SERVO)
94. {}
95. servoSet(rxData[2], rxData[3]);
96. UARTReveived = 0;
97.}
98. anders als (rxData [1] == MOTOSPEED)
99. {}
100. CHNSpeed(rxData[2], rxData[3]);
101. UARTReveived = 0;
102.}
103.}
104.}
105.
106. //CAR BEWEGINGEN
107. nietig carGoFwd()
108. {}
109. motorL.setSpeed(motor_speed);
110. motorR.setSpeed(motor_speed);
111. motorL.run(FORWARD);
112. motorR.run(FORWARD);
113. Serial.print("forward");
114. delay(motor_delay);
115.}
116. nietig carGoBwd()
117. {}
118. motorL.setSpeed(motor_speed);
119. motorR.setSpeed(motor_speed);
120. motorL.run(BACKWARD);
121. motorR.run(BACKWARD);
122. Serial.print("Backward");
123. delay(motor_delay);
124.}
125. nietig carTurnL()
126. {}
127. motorL.setSpeed(motor_speed);
128. motorR.setSpeed(motor_speed);
129. motorL.run(BACKWARD);
130. motorR.run(FORWARD);
131. delay(motor_delay);
132. Serial.print("TurnL");
133.}
134. nietig carTurnR()
135. {}
136. motorL.setSpeed(motor_speed);
137. motorR.setSpeed(motor_speed);
138. motorL.run(FORWARD);
139. motorR.run(BACKWARD);
140. delay(motor_delay);
141. Serial.print("TurnR");
142.}
143. nietig carStop()
144. {}
145. b_motor_stop();
146. Serial.print("carStop");
147. delay(5);
148.}
149. //CAR SNELHEID
150. void CHNSpeed (int wheelDIR, int wheelSpeed)
151. {}
152. if(wheelDIR == 0X01) //LEFT wiel
153. {}
154. motorL.setSpeed(wheelSpeed);
155.}
156. anders if(wheelDIR == 0X02) //RIGHT wiel
157. {}
158. motorR.setSpeed(wheelSpeed);
159.}
160.}
161. //SERVO BEURT
162. void servoSet (int servoNum, int pos)
163. {}
164. if(pos > 180) pos = 160;
165. anders als pos (pos < 0) = 0;
166. switch(servoNum)
167. {}
168. case 0X07:
169. hand_t_servo.write(pos);
170. Serial.print("X");
171. Serial.print(pos);
172. pauze;
173. case 0X08:
174. hand_d_servo.write(pos);
175. Serial.print("Y");
176. Serial.print(pos);
177. pauze;
178.}
179.}
180. void setup
181. {}
182. Serial.begin(9600);
183. b_motor_stop();
184. b_servo_ini();
185. / / delay(2000); wachttijd
186. Serial.println("Hello! WiFi auto");
187.}
188.
189. nietig b_motor_stop() {}
190. motorL.run(RELEASE);
191. motorR.run(RELEASE);
192.}
193.
194. nietig b_servo_ini() {}
195. hand_t_servo.attach(9); de servo op pin 9 hecht aan de servo-object
196. hand_d_servo.attach(10); de servo op pin 10 hecht aan de servo-object
197. hand_t_servo.write(hand_t_pos);
198. hand_d_servo.write(hand_d_pos);
199.}