Guest User

UNO CODE

a guest
Apr 1st, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ArduinoBLE.h>
  3. #include <Stepper.h>
  4.  
  5. ///////////////////////////////////////////////////////
  6. // ----------------------------------------------------
  7. /// THIS CODE RUNS ON THE Arduino R4 - TANK
  8. // ----------------------------------------------------
  9. ///////////////////////////////////////////////////////
  10.  
  11. ////////////////////////////////////////////////////////////////////
  12. // this code sends the ultra sonic distance data to the controller
  13. // we are the peripheral...
  14. ////////////////////////////////////////////////////////////////////
  15.  
  16. // WE ARE UPDATING THE BUTTON CHARACTERISTIC AND READING THE LED STATE CHARACTERISITC
  17. // we send button value
  18. // we recieve LED value
  19.  
  20. // variables for button:
  21. const int buttonPin = 2;
  22. int oldButtonState = LOW;
  23.  
  24. BLEService duplexService("1000"); // create service
  25. BLEByteCharacteristic buttonCharacteristic("1001", BLERead | BLENotify); // create button characteristic and allow remote device to get notifications
  26. BLEIntCharacteristic joystick_1_characteristic("1002", BLERead | BLEWrite); // create joystick_1 characteristic and allow remote device to read and write
  27. BLEIntCharacteristic joystick_2_characteristic("1003", BLERead | BLEWrite); // create joystick_2 characteristic and allow remote device to read and write
  28.  
  29. const int stepsPerRevolution = 800;
  30.  
  31. const int stepMotor1 = 3; //step
  32. const int dirMotor1 = 2; //dir
  33. const int stepMotor2 = 5; //step
  34. const int dirMotor2 = 4; //dir
  35. const int stepMotor3 = 6; //step
  36. const int dirMotor3 = 7; //dir
  37. const int stepMotor4 = 9; //step
  38. const int dirMotor4 = 8; //dir
  39.  
  40. Stepper motor1(stepsPerRevolution, stepMotor1, dirMotor1);
  41. Stepper motor2(stepsPerRevolution, stepMotor2, dirMotor2);
  42. Stepper motor3(stepsPerRevolution, stepMotor3, dirMotor3);
  43. Stepper motor4(stepsPerRevolution, stepMotor4, dirMotor4);
  44.  
  45. const int MOTOR_SPEED = 60;
  46.  
  47. int POS = 0;
  48.  
  49.  
  50. void setup()
  51. {
  52.  
  53. motor1.setSpeed(0);
  54. motor2.setSpeed(0);
  55. motor3.setSpeed(0);
  56. motor4.setSpeed(0);
  57.  
  58. motor1.step(0);
  59. motor2.step(0);
  60. motor3.step(0);
  61. motor4.step(0);
  62.  
  63. Serial.begin(9600);
  64. pinMode(LED_BUILTIN, OUTPUT); // use the LED as an output
  65. pinMode(buttonPin, INPUT_PULLUP); // use button pin as an input
  66.  
  67. BLE.begin(); //init BLE module
  68. BLE.setLocalName("duplexPeripheral"); // set the local name that the peripheral advertises
  69. BLE.setAdvertisedService(duplexService); // set the UUID for the service the peripheral advertises:
  70.  
  71. duplexService.addCharacteristic(joystick_1_characteristic);
  72. duplexService.addCharacteristic(joystick_2_characteristic);
  73. duplexService.addCharacteristic(buttonCharacteristic);
  74.  
  75. BLE.addService(duplexService); // add the service
  76.  
  77. joystick_1_characteristic.writeValue(0);
  78. joystick_2_characteristic.writeValue(0);
  79. buttonCharacteristic.writeValue(0);
  80.  
  81. BLE.advertise(); // start advertising on BLE network
  82. Serial.println("Peripheral is running");
  83.  
  84.  
  85. }
  86.  
  87. void loop() {
  88. BLEDevice central = BLE.central();
  89.  
  90. if (central)
  91. {
  92. Serial.println("Got central connection!");
  93. while (central.connected())
  94. {
  95. byte buttonValue = digitalRead(buttonPin);
  96.  
  97. //check if joystick 1 characteristic has been updated...
  98. if (joystick_1_characteristic.written())
  99. {
  100. POS = joystick_1_characteristic.value();
  101. // if (joy_1_value >1000)
  102. // {
  103. // buttonCharacteristic.writeValue(20);
  104. // } else {
  105. // buttonCharacteristic.writeValue(50);
  106. // }
  107. Serial.print("Position: ");
  108. Serial.println(POS);
  109. }
  110.  
  111. //check if joystick 2 characteristic has been updated...
  112. // if (joystick_2_characteristic.written())
  113. // {
  114. // int joy_2_value = joystick_2_characteristic.value();
  115. // digitalWrite(LED_BUILTIN, joy_2_value);
  116. // Serial.print(" JOY_2 data: ");
  117. // Serial.println(joy_2_value);
  118. // }
  119. if (POS == 0)
  120. {
  121. motor1.setSpeed(0);
  122. motor2.setSpeed(0);
  123. motor3.setSpeed(0);
  124. motor4.setSpeed(0);
  125.  
  126. motor1.step(0);
  127. motor2.step(0);
  128. motor3.step(0);
  129. motor4.step(0);
  130. }
  131. if(POS == 1);
  132. {
  133. motor1.setSpeed(60);
  134. motor2.setSpeed(60);
  135. motor3.setSpeed(60);
  136. motor4.setSpeed(60);
  137.  
  138. motor1.step(1);
  139. motor2.step(1);
  140. motor3.step(1);
  141. motor4.step(1);
  142.  
  143. }
  144. if (POS == 2)
  145. {
  146. motor1.setSpeed(0);
  147. motor2.setSpeed(0);
  148. motor3.setSpeed(0);
  149. motor4.setSpeed(0);
  150.  
  151. motor1.step(0);
  152. motor2.step(0);
  153. motor3.step(0);
  154. motor4.step(0);
  155. }
  156. if (POS == 3) //Right
  157. {
  158. motor1.setSpeed(40);
  159. motor2.setSpeed(40);
  160. motor3.setSpeed(40);
  161. motor4.setSpeed(40);
  162.  
  163. motor1.step(1);
  164. motor2.step(-1);
  165. motor3.step(1);
  166. motor4.step(-1);
  167. }
  168. if (POS == 4) //Back right
  169. {
  170. motor1.setSpeed(0);
  171. motor2.setSpeed(0);
  172. motor3.setSpeed(0);
  173. motor4.setSpeed(0);
  174.  
  175. motor1.step(0);
  176. motor2.step(0);
  177. motor3.step(0);
  178. motor4.step(0);
  179. }
  180. if (POS == 5) //Backwards
  181. {
  182. motor1.setSpeed(60);
  183. motor2.setSpeed(60);
  184. motor3.setSpeed(60);
  185. motor4.setSpeed(60);
  186.  
  187. motor1.step(-1);
  188. motor2.step(-1);
  189. motor3.step(-1);
  190. motor4.step(-1);
  191. }
  192. if (POS == 6) //Back left
  193. {
  194. motor1.setSpeed(0);
  195. motor2.setSpeed(0);
  196. motor3.setSpeed(0);
  197. motor4.setSpeed(0);
  198.  
  199. motor1.step(0);
  200. motor2.step(0);
  201. motor3.step(0);
  202. motor4.step(0);
  203. }
  204. if (POS == 7) //Left
  205. {
  206. motor1.setSpeed(40);
  207. motor2.setSpeed(40);
  208. motor3.setSpeed(40);
  209. motor4.setSpeed(40);
  210.  
  211. motor1.step(-1);
  212. motor2.step(1);
  213. motor3.step(-1);
  214. motor4.step(1);
  215. }
  216. if (POS == 8) //forward left
  217. {
  218. motor1.setSpeed(0);
  219. motor2.setSpeed(0);
  220. motor3.setSpeed(0);
  221. motor4.setSpeed(0);
  222.  
  223. motor1.step(0);
  224. motor2.step(0);
  225. motor3.step(0);
  226. motor4.step(0);
  227. }
  228.  
  229.  
  230. }
  231.  
  232. // when the central disconnects, print it out:
  233. Serial.print("Disconnected from central: ");
  234. Serial.println(central.address());
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment