safwan092

Tx_Code_Hand_controlled_Car_Using_Bluetooth_Arduino_MPU6050_

May 31st, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include "Wire.h"
  2. #include "MPU6050.h"
  3. #include<SoftwareSerial.h>
  4.  
  5. #define RX 4
  6. #define TX 5
  7.  
  8. SoftwareSerial ss(RX, TX);
  9.  
  10. int data1, data2;
  11.  
  12. // Hardware configuration
  13. MPU6050 accelgyro;
  14.  
  15. int16_t ax, ay, az; // x-axis and y-axis values, for this application z-axis is not needed.
  16. int16_t gx, gy, gz; // these values are not used, just declaring to pass in the function.
  17.  
  18. #define TX_ENABLE_KEY 2
  19. #define TX_LED 3
  20.  
  21. int Pot_Val_Y = 0, Pot_Val_X = 0, Up_key = 0, Dn_key = 0, Left_key = 0, Right_key = 0;
  22. unsigned char Tx_command = 0, Speed_index = 0, Tx_Enable_Flag = 0, TX_Key_Pressed = 0;
  23. unsigned char Tx_Array[2];
  24.  
  25.  
  26.  
  27. void setup() {
  28. Serial.begin(115200);
  29. ss.begin(9600);
  30. pinMode(TX_ENABLE_KEY, INPUT_PULLUP);
  31. pinMode(TX_LED, OUTPUT);
  32.  
  33. //radio.write(&Tx_command, sizeof(Tx_command));
  34.  
  35. Wire.begin();
  36. Serial.println("Initializing I2C devices...");
  37. accelgyro.initialize();
  38.  
  39. // verify connection
  40. Serial.println("Testing device connections...");
  41. Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  42.  
  43. Tx_Array[0] = 0;
  44. Tx_Array[1] = 0;
  45.  
  46. }
  47.  
  48. void loop()
  49. {
  50. accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // we are getting all 6 value from MPU6050 i.e. 3 Accelerometer values and 3 Gyro values viz. ax,ay,az,gx,gy and gz.
  51.  
  52. if (!(PIND & 0x04) && (TX_Key_Pressed == 0))
  53. {
  54. TX_Key_Pressed = 1;
  55. if (Tx_Enable_Flag == 0)
  56. {
  57. Tx_Enable_Flag = 1;
  58. PORTD |= 0x08;
  59. //Serial.println("A");
  60. }
  61. else
  62. {
  63. Tx_Enable_Flag = 0;
  64. PORTD &= 0xF7;
  65. //Serial.println("B");
  66. }
  67. //PORTD |= 0x08;
  68. Serial.println("Pressed");
  69. }
  70. else if ((PIND & 0x04) && (TX_Key_Pressed == 1))
  71. {
  72. TX_Key_Pressed = 0;
  73. Serial.println("Released");
  74. }
  75.  
  76. if ((ay <= -4000) || ((ay >= 4000)))
  77. {
  78. //Serial.print("A , "); // uncomment A,B,C..... if you are debugging
  79. if ((ax >= -4000) || ((ax <= 4000)))
  80. {
  81. //Serial.print("B , ");
  82. if ((ay <= -4000))
  83. {
  84. //Serial.print("C , ");
  85. Tx_command = 1; // forward
  86. Speed_index = (ay + 4000) / -2000 + 1; // more the negative value more the speed. ay valu varies from -16384 to +16384 same for ax and others.
  87. if (Speed_index > 5)
  88. {
  89. Speed_index = 5;
  90. }
  91. }
  92.  
  93. if ((ay >= 4000))
  94. {
  95. //Serial.print("D , ");
  96. Tx_command = 2; // Backward
  97. Speed_index = (ay - 4000) / 2000 + 1; // more the positive value more the speed. ay valu varies from -16384 to +16384 same for ax and others.
  98. if (Speed_index > 5)
  99. {
  100. Speed_index = 5;
  101. }
  102. }
  103. }
  104. else
  105. {
  106. //Serial.print("E , ");
  107. Tx_command = 0;
  108. Speed_index = 0;
  109. }
  110. }
  111. else if ((ax <= -4000) || ((ax >= 4000)))
  112. {
  113. //Serial.print("F , ");
  114. if ((ay >= -4000) || ((ay <= 4000)))
  115. {
  116. //Serial.print("G , ");
  117. if ((ax <= -4000))
  118. {
  119. //Serial.print("H , ");
  120. Tx_command = 4; // Right
  121. Speed_index = (ax + 4000) / -2000 + 1; // more the negative value more the speed. ax valu varies from -16384 to +16384 same for ay and others.
  122. if (Speed_index > 5)
  123. {
  124. Speed_index = 5;
  125. }
  126. }
  127.  
  128. if ((ax >= 4000))
  129. {
  130. //Serial.print("I , ");
  131. Tx_command = 3; // Left
  132. Speed_index = (ax - 4000) / 2000 + 1; // more the positive value more the speed. ax valu varies from -16384 to +16384 same for ay and others.
  133. if (Speed_index > 5)
  134. {
  135. Speed_index = 5;
  136. }
  137. }
  138. }
  139. else
  140. {
  141. //Serial.print("J , ");
  142. Tx_command = 0;
  143. Speed_index = 0;
  144. }
  145. }
  146. else
  147. {
  148. //Serial.print("K , ");
  149. Tx_command = 0;
  150. Speed_index = 0;
  151. }
  152. Serial.print(Tx_command);
  153. Serial.print(" , ");
  154. Serial.println(Speed_index);
  155. if (Tx_Enable_Flag == 0)
  156. {
  157. Tx_Array[0] = Tx_command;
  158. Tx_Array[1] = Speed_index;
  159. sendDataViaBT(Tx_Array[0], Tx_Array[1]);
  160. //radio.write(&Tx_Array, 2);
  161. // 1st byte = Direction , 2nd Byte = Speed
  162. }
  163. delay(100);
  164. }
  165.  
  166.  
  167. void sendDataViaBT(int aA, int bB) {
  168. data1 = aA;
  169. data2 = bB;
  170. ss.print(data1); ss.print("A");
  171. ss.print(data2); ss.print("B");
  172. ss.print("\n");
  173. Serial.println(data1);
  174. Serial.println(" A ");
  175. Serial.println(data2);
  176. Serial.println(" B ");
  177. delay(30);
  178. }
Advertisement
Add Comment
Please, Sign In to add comment