Advertisement
BimoSora

Cars

Jul 23rd, 2020 (edited)
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include <Wire.h>
  3. LiquidCrystal_I2C lcd(0x27,16,2);
  4.  
  5. // RemoteXY select connection mode and include library
  6. #define REMOTEXY_MODE__HARDSERIAL
  7.  
  8. #include <RemoteXY.h>
  9.  
  10. // RemoteXY connection settings
  11. #define REMOTEXY_SERIAL Serial
  12. #define REMOTEXY_SERIAL_SPEED 9600
  13.  
  14. // RemoteXY configurate
  15. #pragma pack(push, 1)
  16. uint8_t RemoteXY_CONF[] =
  17. { 255,6,0,0,0,112,0,10,120,0,
  18. 2,1,4,3,18,9,1,26,31,31,
  19. 78,121,97,108,97,104,0,77,97,116,
  20. 105,0,5,20,71,35,24,24,1,24,
  21. 31,2,1,4,50,18,9,1,26,31,
  22. 31,76,97,109,112,117,32,110,121,97,
  23. 108,97,104,0,76,97,109,112,117,32,
  24. 109,97,116,105,0,2,1,4,36,18,
  25. 9,1,26,31,31,83,105,114,101,110,
  26. 101,32,110,121,97,108,97,104,0,83,
  27. 105,114,101,110,101,32,109,97,116,105,
  28. 0,3,134,58,5,36,7,1,26 };
  29.  
  30. // this structure defines all the variables and events of your control interface
  31. struct {
  32.  
  33. // input variables
  34. uint8_t saklar; // =1 if switch ON and =0 if OFF
  35. int8_t joystick_1_x; // =-100..100 x-coordinate joystick position
  36. int8_t joystick_1_y; // =-100..100 y-coordinate joystick position
  37. uint8_t lampu; // =1 if switch ON and =0 if OFF
  38. uint8_t sirene; // =1 if switch ON and =0 if OFF
  39. uint8_t speed_motor; // =0 if select position A, =1 if position B, =2 if position C, ...
  40.  
  41. // other variable
  42. uint8_t connect_flag; // =1 if wire connected, else =0
  43.  
  44. } RemoteXY;
  45. #pragma pack(pop)
  46.  
  47. /* defined the right motor control pins */
  48. #define PIN_MOTOR_RIGHT_UP 7
  49. #define PIN_MOTOR_RIGHT_DN 6
  50. #define PIN_MOTOR_RIGHT_SPEED 10
  51.  
  52. /* defined the left motor control pins */
  53. #define PIN_MOTOR_LEFT_UP 5
  54. #define PIN_MOTOR_LEFT_DN 4
  55. #define PIN_MOTOR_LEFT_SPEED 9
  56.  
  57. #define PIN_SAKLAR 13
  58. #define PIN_LAMPU 11
  59. #define PIN_SIRENE 12
  60.  
  61. /* defined two arrays with a list of pins for each motor */
  62. unsigned char RightMotor[3] =
  63. {PIN_MOTOR_RIGHT_UP, PIN_MOTOR_RIGHT_DN, PIN_MOTOR_RIGHT_SPEED};
  64. unsigned char LeftMotor[3] =
  65. {PIN_MOTOR_LEFT_UP, PIN_MOTOR_LEFT_DN, PIN_MOTOR_LEFT_SPEED};
  66.  
  67. /*
  68. speed control of the motor
  69. motor - pointer to an array of pins
  70. v - motor speed can be set from -100 to 100
  71. */
  72.  
  73. void Sleep_speed (unsigned char * motor1, int v1)
  74. {
  75. if (v1>100) v1=100;
  76. if (v1<-100) v1=-100;
  77. if (v1>0) {
  78. digitalWrite(motor1[0], HIGH);
  79. digitalWrite(motor1[1], LOW);
  80. analogWrite(motor1[2], v1*0);
  81. }
  82. else if (v1<0) {
  83. digitalWrite(motor1[0], LOW);
  84. digitalWrite(motor1[1], HIGH);
  85. analogWrite(motor1[2], (-v1)*0);
  86. }
  87. else {
  88. digitalWrite(motor1[0], LOW);
  89. digitalWrite(motor1[1], LOW);
  90. analogWrite(motor1[2], 0);
  91. }
  92. }
  93.  
  94. void Wheel1 (unsigned char * motor2, int v2)
  95. {
  96. if (v2>100) v2=100;
  97. if (v2<-100) v2=-100;
  98. if (v2>0) {
  99. digitalWrite(motor2[0], HIGH);
  100. digitalWrite(motor2[1], LOW);
  101. analogWrite(motor2[2], v2*0.51);
  102. }
  103. else if (v2<0) {
  104. digitalWrite(motor2[0], LOW);
  105. digitalWrite(motor2[1], HIGH);
  106. analogWrite(motor2[2], (-v2)*0.51);
  107. }
  108. else {
  109. digitalWrite(motor2[0], LOW);
  110. digitalWrite(motor2[1], LOW);
  111. analogWrite(motor2[2], 0);
  112. }
  113. }
  114.  
  115. void Wheel2 (unsigned char * motor3, int v3)
  116. {
  117. if (v3>100) v3=100;
  118. if (v3<-100) v3=-100;
  119. if (v3>0) {
  120. digitalWrite(motor3[0], HIGH);
  121. digitalWrite(motor3[1], LOW);
  122. analogWrite(motor3[2], v3*1.02);
  123. }
  124. else if (v3<0) {
  125. digitalWrite(motor3[0], LOW);
  126. digitalWrite(motor3[1], HIGH);
  127. analogWrite(motor3[2], (-v3)*1.02);
  128. }
  129. else {
  130. digitalWrite(motor3[0], LOW);
  131. digitalWrite(motor3[1], LOW);
  132. analogWrite(motor3[2], 0);
  133. }
  134. }
  135.  
  136. void Wheel3 (unsigned char * motor4, int v4)
  137. {
  138. if (v4>100) v4=100;
  139. if (v4<-100) v4=-100;
  140. if (v4>0) {
  141. digitalWrite(motor4[0], HIGH);
  142. digitalWrite(motor4[1], LOW);
  143. analogWrite(motor4[2], v4*1.53);
  144. }
  145. else if (v4<0) {
  146. digitalWrite(motor4[0], LOW);
  147. digitalWrite(motor4[1], HIGH);
  148. analogWrite(motor4[2], (-v4)*1.53);
  149. }
  150. else {
  151. digitalWrite(motor4[0], LOW);
  152. digitalWrite(motor4[1], LOW);
  153. analogWrite(motor4[2], 0);
  154. }
  155. }
  156.  
  157. void Wheel4 (unsigned char * motor5, int v5)
  158. {
  159. if (v5>100) v5=100;
  160. if (v5<-100) v5=-100;
  161. if (v5>0) {
  162. digitalWrite(motor5[0], HIGH);
  163. digitalWrite(motor5[1], LOW);
  164. analogWrite(motor5[2], v5*2.04);
  165. }
  166. else if (v5<0) {
  167. digitalWrite(motor5[0], LOW);
  168. digitalWrite(motor5[1], HIGH);
  169. analogWrite(motor5[2], (-v5)*2.04);
  170. }
  171. else {
  172. digitalWrite(motor5[0], LOW);
  173. digitalWrite(motor5[1], LOW);
  174. analogWrite(motor5[2], 0);
  175. }
  176. }
  177.  
  178. void Wheel5 (unsigned char * motor6, int v6)
  179. {
  180. if (v6>100) v6=100;
  181. if (v6<-100) v6=-100;
  182. if (v6>0) {
  183. digitalWrite(motor6[0], HIGH);
  184. digitalWrite(motor6[1], LOW);
  185. analogWrite(motor6[2], v6*2.55);
  186. }
  187. else if (v6<0) {
  188. digitalWrite(motor6[0], LOW);
  189. digitalWrite(motor6[1], HIGH);
  190. analogWrite(motor6[2], (-v6)*2.55);
  191. }
  192. else {
  193. digitalWrite(motor6[0], LOW);
  194. digitalWrite(motor6[1], LOW);
  195. analogWrite(motor6[2], 0);
  196. }
  197. }
  198.  
  199. void setup () {
  200. lcd.begin();
  201. lcd.backlight();
  202. Serial.begin(115200);
  203.  
  204. lcd.setCursor(0,0);
  205. lcd.print("ON");
  206. delay(3000);
  207. lcd.clear();
  208.  
  209. pinMode (PIN_MOTOR_RIGHT_UP, OUTPUT);
  210. pinMode (PIN_MOTOR_RIGHT_DN, OUTPUT);
  211. pinMode (PIN_MOTOR_LEFT_UP, OUTPUT);
  212. pinMode (PIN_MOTOR_LEFT_DN, OUTPUT);
  213.  
  214. pinMode (PIN_SAKLAR, OUTPUT);
  215. pinMode (PIN_LAMPU, OUTPUT);
  216. pinMode (PIN_SIRENE, OUTPUT);
  217.  
  218. /* initialization module RemoteXY */
  219. RemoteXY_Init ();
  220. }
  221.  
  222. void loop () {
  223.  
  224. RemoteXY_Handler ();
  225. if (RemoteXY.saklar!=0) {
  226.  
  227. if (RemoteXY.speed_motor==0) {
  228. Sleep_speed (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  229. /* manage the left motor */
  230. Sleep_speed (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  231. }
  232. else if (RemoteXY.speed_motor==1) {
  233. Wheel1 (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  234. /* manage the left motor */
  235. Wheel1 (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  236. }
  237. else if (RemoteXY.speed_motor==2) {
  238. Wheel2 (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  239. /* manage the left motor */
  240. Wheel2 (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  241. }
  242. else if (RemoteXY.speed_motor==3) {
  243. Wheel3 (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  244. /* manage the left motor */
  245. Wheel3 (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  246. }
  247. else if (RemoteXY.speed_motor==4) {
  248. Wheel4 (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  249. /* manage the left motor */
  250. Wheel4 (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  251. }
  252. else if (RemoteXY.speed_motor==5) {
  253. Wheel5 (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  254. /* manage the left motor */
  255. Wheel5 (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  256. }
  257.  
  258. digitalWrite(PIN_SAKLAR, HIGH);
  259.  
  260. if (RemoteXY.lampu!=0) {
  261. digitalWrite(PIN_LAMPU, HIGH);
  262. }
  263. else {
  264. digitalWrite(PIN_LAMPU, LOW);
  265. }
  266.  
  267. if (RemoteXY.sirene!=0) {
  268. digitalWrite(PIN_SIRENE, HIGH);
  269. }
  270. else
  271. {
  272. digitalWrite(PIN_SIRENE, LOW);
  273. }
  274. }
  275. else {
  276. Sleep_speed (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  277. /* manage the left motor */
  278. Sleep_speed (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  279. digitalWrite(PIN_SAKLAR, LOW);
  280. digitalWrite(PIN_LAMPU, LOW);
  281. digitalWrite(PIN_SIRENE, LOW);
  282. }
  283.  
  284. if (input>150 && input<200){//If the Bot is falling
  285. if (output>0){ //Falling towards front
  286. Forward(); //Rotate the wheels forward
  287. } else if (output<0){ //Falling towards back
  288. Reverse(); //Rotate the wheels backward
  289. }
  290. } else {//If Bot not falling
  291. Stop(); //Hold the wheels still
  292. }
  293. }
  294. }
  295.  
  296. void Forward ()
  297. {
  298. analogWrite(PIN_MOTOR_RIGHT_UP, output);
  299. analogWrite(PIN_MOTOR_RIGHT_DN, LOW);
  300. analogWrite(PIN_MOTOR_LEFT_UP, output);
  301. analogWrite(PIN_MOTOR_LEFT_DN, LOW);
  302. lcd.setCursor(0,0);
  303. lcd.print("F");
  304. Serial.print("F");
  305. }
  306.  
  307. void Reverse ()
  308. {
  309. analogWrite(PIN_MOTOR_RIGHT_UP, LOW);
  310. analogWrite(PIN_MOTOR_RIGHT_DN, output*-1);
  311. analogWrite(PIN_MOTOR_LEFT_UP, LOW);
  312. analogWrite(PIN_MOTOR_LEFT_DN, output*-1);
  313. lcd.setCursor(0,0);
  314. lcd.print("R");
  315. Serial.print("R");
  316. }
  317.  
  318. void Stop ()
  319. {
  320. analogWrite(PIN_MOTOR_RIGHT_UP, LOW);
  321. analogWrite(PIN_MOTOR_RIGHT_DN, LOW);
  322. analogWrite(PIN_MOTOR_LEFT_UP, LOW);
  323. analogWrite(PIN_MOTOR_LEFT_DN, LOW);
  324. lcd.setCursor(0,0);
  325. lcd.print("S");
  326. Serial.print("S");
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement