Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. int BluetoothTX = 3;
  3. int BluetoothRX = 2;
  4.  
  5. const int inputMidLeft = 12;
  6. const int inputMidRight = 8;
  7. const int inputFarLeft = 13;
  8. const int inputFarRight = 7;
  9.  
  10. bool midLeftVal;
  11. bool midRightVal;
  12. bool farLeftVal;
  13. bool farRightVal;
  14. bool autoDriveCheck = false;
  15.  
  16. //definations af motorenes pins
  17. const int motor1A = 11;
  18. const int motor1B = 10;
  19. const int motor2A = 6;
  20. const int motor2B = 5;
  21.  
  22. int verdi = 0;
  23.  
  24. const int Gun = 4;
  25.  
  26. SoftwareSerial bluetooth(BluetoothTX, BluetoothRX);
  27.  
  28. String state = "";
  29.  
  30. void setup() {
  31. // Defination af outpusts
  32. bluetooth.begin(115200);
  33. Serial.begin(115200);
  34. pinMode(motor1A, OUTPUT);
  35. pinMode(motor1B, OUTPUT);
  36. pinMode(motor2A, OUTPUT);
  37. pinMode(motor2B, OUTPUT);
  38. pinMode(Gun, OUTPUT);
  39.  
  40. //senscore
  41. pinMode(inputMidLeft, INPUT);
  42. pinMode(inputMidRight, INPUT);
  43. pinMode(inputFarLeft, INPUT);
  44. pinMode(inputFarRight, INPUT);
  45.  
  46. }
  47.  
  48.  
  49. void frem (){
  50. analogWrite (motor1A, 255);
  51. analogWrite (motor1B, 0);
  52. analogWrite (motor2A, 255);
  53. analogWrite (motor2B, 0);
  54. }
  55.  
  56. void bak (){
  57. analogWrite (motor1A, 0);
  58. analogWrite (motor1B, 255);
  59. analogWrite (motor2A, 0);
  60. analogWrite (motor2B, 255);
  61. }
  62.  
  63. void nej(){
  64. analogWrite (motor1A, 0);
  65. analogWrite (motor1B, 0);
  66. analogWrite (motor2A, 0);
  67. analogWrite (motor2B, 0);
  68. }
  69.  
  70. void right(){
  71. analogWrite (motor1A, 255);
  72. analogWrite (motor1B, 0);
  73. analogWrite (motor2A, 190);
  74. analogWrite (motor2B, 0);
  75. }
  76.  
  77. void left(){
  78. analogWrite (motor1A, 190);
  79. analogWrite (motor1B, 0);
  80. analogWrite (motor2A, 255);
  81. analogWrite (motor2B, 0);
  82.  
  83. }
  84.  
  85. void sright(){
  86. analogWrite (motor1A, 255);
  87. analogWrite (motor1B, 0);
  88. analogWrite (motor2A, 0);
  89. analogWrite (motor2B, 0);
  90. }
  91.  
  92. void sleft(){
  93. analogWrite (motor1A, 0);
  94. analogWrite (motor1B, 0);
  95. analogWrite (motor2A, 255);
  96. analogWrite (motor2B, 0);
  97. }
  98.  
  99. void msright(){
  100. analogWrite (motor1A, 0);
  101. analogWrite (motor1B, 255);
  102. analogWrite (motor2A, 255);
  103. analogWrite (motor2B, 0);
  104. }
  105.  
  106. void msleft(){
  107. analogWrite (motor1A, 255);
  108. analogWrite (motor1B, 0);
  109. analogWrite (motor2A, 0);
  110. analogWrite (motor2B, 255);
  111. }
  112.  
  113. void loop() {
  114.  
  115.  
  116. if (bluetooth.available()>0){
  117. delay(10);
  118. char c = bluetooth.read();
  119. state += c;
  120. }
  121.  
  122.  
  123. if (state == "i"){
  124. autoDriveCheck = !autoDriveCheck;
  125. }
  126.  
  127.  
  128.  
  129. if (autoDriveCheck == false) {
  130.  
  131. if (state.length() > 0) {
  132. //Serial.println("false");
  133. Serial.print(state);
  134. Serial.println("False");
  135.  
  136. if(state == "a"){
  137. frem();
  138. }
  139. else if(state == "b"){
  140. nej();
  141. }
  142.  
  143. if(state == "c"){
  144. sleft();
  145. }
  146. else if(state == "d"){
  147. nej();
  148. }
  149.  
  150. if(state == "e"){
  151. sright();
  152. }
  153. else if(state == "f") {
  154. nej();
  155. }
  156.  
  157. if(state == "g"){
  158. bak();
  159. }
  160. else if(state == "h") {
  161. nej();
  162. }
  163.  
  164. if(state == "q")
  165. {
  166. analogWrite (Gun, 255);
  167. }
  168. else if(state == "r")
  169. {
  170. analogWrite (Gun, 0);
  171. }
  172.  
  173. state ="";}
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. if (autoDriveCheck == true){
  181.  
  182.  
  183. if (state.length() > 0) {
  184. //Serial.println("true");
  185. Serial.print(state);
  186. Serial.println("True");
  187.  
  188. //senscore
  189. midLeftVal = digitalRead(inputMidLeft);
  190. midRightVal = digitalRead(inputMidRight);
  191. farLeftVal = digitalRead(inputFarLeft);
  192. farRightVal = digitalRead(inputFarRight);
  193.  
  194. verdi = farLeftVal*1+midLeftVal*2+midRightVal*4+farRightVal*8;
  195.  
  196. switch (verdi) {
  197. case 15:
  198. //bak
  199. bak();
  200. break;
  201. case 4:
  202. msright();
  203. break;
  204. case 2:
  205. msleft();
  206. break;
  207. case 0:
  208. frem();
  209. break;
  210. case 9:
  211. frem();
  212. break;
  213. case 5:
  214. frem();
  215. break;
  216. case 10:
  217. frem();
  218. break;
  219. case 3:
  220. right();
  221. break;
  222. case 11:
  223. right();
  224. break;
  225. case 13:
  226. left();
  227. break;
  228. case 12:
  229. left();
  230. break;
  231. case 7:
  232. sright();
  233. break;
  234. case 1:
  235. sright();
  236. break;
  237. case 14:
  238. sleft();
  239. break;
  240. case 8:
  241. sleft();
  242. break;
  243.  
  244.  
  245. }
  246. }
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement