Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. //kakubotics
  2. //This program is used to control a robot car using a app that communicates with Arduino trough a bluetooth module
  3.  
  4. #include<AFMotor.h>
  5.  
  6. //creates three objects to control the terminal 2,3 and 4 of motor shield
  7. AF_DCMotor motor1(3);
  8. AF_DCMotor motor2(4);
  9.  
  10.  
  11. char state;
  12. int vSpeed=200; // Default speed, from 0 to 255
  13. //Bluetooth (HC-05 JY-MCU) State pin on pin 2 of Arduino
  14. const int BTState = 2;
  15. //Leds connected to Arduino UNO Pin 7
  16. const int lights1 = 7;
  17. //Leds connected to Arduino UNO Pin 9
  18. const int lights2 = 9;
  19. const int lights3 = 10;
  20. const int lights4 = 11;
  21.  
  22. int i=0,j=0,k=0,m=0;
  23.  
  24.  
  25.  
  26. void setup()
  27. {
  28.  
  29. pinMode(lights1, OUTPUT);
  30. pinMode(lights2, OUTPUT);
  31. pinMode(lights3, OUTPUT);
  32. pinMode(lights4, OUTPUT);
  33. pinMode(BTState, INPUT);
  34.  
  35. Serial.begin(9600); //Set the baud rate to your Bluetooth module.
  36.  
  37. }
  38.  
  39. void loop(){
  40.  
  41. //Stop car when connection lost or bluetooth disconnected
  42. if(digitalRead(BTState)==LOW) { Stop(); }
  43.  
  44. if(Serial.available() > 0){
  45. state = Serial.read(); //Save income data to variable 'state'
  46. Stop(); //initialize with motors stoped
  47. //Change pin mode only if new command is different from previous.
  48. //Serial.println(command);
  49.  
  50. //Change speed if state is equal from 0 to 9. Values must be from 0 to 255 (PWM)
  51. if (state == '0'){
  52. vSpeed=0;}
  53. else if (state == '1'){
  54. vSpeed=100;}
  55. else if (state == '2'){
  56. vSpeed=120;}
  57. else if (state == '3'){
  58. vSpeed=140;}
  59. else if (state == '4'){
  60. vSpeed=160;}
  61. else if (state == '5'){
  62. vSpeed=180;}
  63. else if (state == '6'){
  64. vSpeed=200;}
  65. else if (state == '7'){
  66. vSpeed=220;}
  67. else if (state == '8'){
  68. vSpeed=240;}
  69. else if (state == '9'){
  70. vSpeed=255;}
  71.  
  72. switch(state){
  73. case 'F':
  74. forward();
  75. break;
  76. case 'B':
  77. back();
  78. break;
  79. case 'L':
  80. left();
  81. break;
  82. case 'R':
  83. right();
  84. break;
  85. case 'W':
  86. frontlightson();
  87. break;
  88. case 'w':
  89. frontlightsoff();
  90. break;
  91. case 'U':
  92. backlightson();
  93. break;
  94. case 'u':
  95. backlightsoff();
  96. break;
  97. case 'V':
  98. upon();
  99. break;
  100. case 'v':
  101. upoff();
  102. break;
  103. case 'X':
  104. dowon();
  105. break;
  106. case 'x':
  107. dowoff();
  108. break;
  109. }
  110. }
  111. }
  112.  
  113. void forward()
  114. {
  115. motor1.setSpeed(vSpeed); //Define vSpeed velocity
  116. motor1.run(FORWARD); //rotate the motor clockwise
  117. motor2.setSpeed(vSpeed); //Define vspeed velocity
  118. motor2.run(FORWARD); //rotate the motor clockwise
  119. }
  120.  
  121. void back()
  122. {
  123. motor1.setSpeed(vSpeed);
  124. motor1.run(BACKWARD); //rotate the motor counterclockwise
  125. motor2.setSpeed(vSpeed);
  126. motor2.run(BACKWARD); //rotate the motor counterclockwise
  127. }
  128.  
  129. void left()
  130. {
  131. motor1.setSpeed(vSpeed); //Define maximum velocity
  132. motor1.run(FORWARD); //rotate the motor clockwise
  133. motor2.setSpeed(0);
  134. motor2.run(RELEASE); //turn motor2 off
  135. }
  136.  
  137. void right()
  138. {
  139. motor1.setSpeed(0);
  140. motor1.run(RELEASE); //turn motor1 off
  141. motor2.setSpeed(vSpeed); //Define maximum velocity
  142. motor2.run(FORWARD); //rotate the motor clockwise
  143. }
  144.  
  145. void Stop()
  146. {
  147. motor1.setSpeed(0);
  148. motor2.run(RELEASE); //turn motor1 off
  149. motor2.setSpeed(0);
  150. motor2.run(RELEASE); //turn motor2 off
  151.  
  152. }
  153.  
  154. void upon()
  155. {
  156. if (k==0){
  157. digitalWrite(lights3, HIGH);
  158. k=1;
  159. }
  160. else if (k==1){
  161. digitalWrite(lights3, LOW);
  162. k=0;
  163. }
  164.  
  165.  
  166. }
  167.  
  168. void upoff()
  169. {
  170. if (k==0){
  171. digitalWrite(lights3, HIGH);
  172. k=1;
  173. }
  174. else if (k==1){
  175. digitalWrite(lights3, LOW);
  176. k=0;
  177. }
  178.  
  179.  
  180. }
  181.  
  182. void dowon()
  183. {
  184. if (m==0){
  185. digitalWrite(lights4, HIGH);
  186. m=1;
  187. }
  188. else if (m==1){
  189. digitalWrite(lights4, LOW);
  190. m=0;
  191. }
  192. }
  193.  
  194. void dowoff()
  195. {
  196. if (m==0){
  197. digitalWrite(lights4, HIGH);
  198. m=1;
  199. }
  200. else if (m==1){
  201. digitalWrite(lights4, LOW);
  202. m=0;
  203. }
  204. }
  205.  
  206. void frontlightson()
  207. {
  208. if (i==0){
  209. digitalWrite(lights1, HIGH);
  210. i=1;
  211. }
  212. else if (i==1){
  213. digitalWrite(lights1, LOW);
  214. i=0;
  215. }
  216.  
  217.  
  218. }
  219.  
  220. void frontlightsoff()
  221. {
  222. if (i==0){
  223. digitalWrite(lights1, HIGH);
  224. i=1;
  225. }
  226. else if (i==1){
  227. digitalWrite(lights1, LOW);
  228. i=0;
  229. }
  230.  
  231. }
  232.  
  233. void backlightson()
  234. {
  235. if (j==0){
  236. digitalWrite(lights2, HIGH);
  237. j=1;
  238. }
  239. else if (j==1){
  240. digitalWrite(lights2, LOW);
  241. j=0;
  242. }
  243.  
  244.  
  245.  
  246. }
  247.  
  248. void backlightsoff()
  249. {
  250. if (j==0){
  251. digitalWrite(lights2, HIGH);
  252. j=1;
  253. }
  254. else if (j==1){
  255. digitalWrite(lights2, LOW);
  256. j=0;
  257. }
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement