Advertisement
RuiViana

TestaBT.ino

Oct 19th, 2021 (edited)
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. // Ref: https://www.keuwl.com/electronics/rduino/bluet/10-stepper-motor/
  2. #define L1B 27 //left motor1 l293d pin2 (backward)
  3. #define L1F 26 //left motor1 l293d pin7 (forward)
  4. #define R1B 33 //right motor2 l293d pin15 (backward)
  5. #define R1F 32 //right motor2 l293d pin10 (forward)
  6. #define S1L 14 //left enable pin for left motor1 l293d pin1 (speed)
  7. #define S1R 25 //right enable pin for right motor2 l293d pin9 (speed)
  8.  
  9. #define TAL 13 //enable taillight
  10. #define BAL 12 //enable bracklight
  11. #define LGH 5 //enable lights
  12. #define EML 4 //enable emergencelights
  13. #define INT 0 //int ADPS9960
  14. #define SDA 34 //sda ADPS9960
  15. #define SCL 35 //scl ADPS9960
  16.  
  17. #define L2B 17 //left motor3 l293dB pin2 (backward)
  18. #define L2F 16 //left motor3 l293dB pin7 (forward)
  19. #define R2B 23 //right motor4 l293dB pin15 (backward)
  20. #define R2F 22 //right motor4 l293dB pin10 (forward)
  21. #define S2L 18 //left enable pin for left motor l293dB pin1 (speed)
  22. #define S2R 19 //right enable pin for right motor l293dB pin9 (speed)
  23.  
  24. int LM = 0; //speed of left motor front
  25. int RM = 0; //speed of right motor front
  26. int LMR = 0; //speed of left motor rear
  27. int RMR = 0; //speed of right motor rear
  28.  
  29. //Biblioteca para habilitar a comunicação SERIAL
  30. #include "BluetoothSerial.h"
  31.  
  32. //Verifica se o bluetooth esta habilitado corretamente
  33. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
  34. #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
  35. #endif
  36. BluetoothSerial SerialBT;
  37.  
  38. //void leSerial();
  39. void leBluetooth();
  40. //void escreveBluetooth();
  41.  
  42. const int freq = 7000;
  43. const int pwmChannel = 0;
  44. const int pwmChannel2 = 1;
  45. const int pwmChannel3 = 2;
  46. const int pwmChannel4 = 3;
  47. const int resolution = 8;
  48. int dutyCycle = 255;
  49. float xval, yval;
  50. char dado;
  51.  
  52. // StartPressed; //start lights levelone
  53. // RedPressed; //turbo motors l293dB
  54. // LeftPressed; //double left
  55. // RightPressed; //double right
  56. // CirclePressed; //high ligths
  57. // TrianglePressed; //emergence ligths
  58. // CrossPressed; //brack lights
  59.  
  60. //-----------------------------------------------------------------------------
  61. void setup() {
  62.   Serial.begin(115200);
  63.   SerialBT.begin("ESP32_BTEL");
  64.   Serial.println("O dispositivo foi iniciado, agora você pode emparelhá-lo com bluetooth!");
  65.  
  66.   pinMode(L1B, OUTPUT);
  67.   pinMode(L1F, OUTPUT);
  68.   pinMode(R1B, OUTPUT);
  69.   pinMode(R1F, OUTPUT);
  70.   pinMode(S1L, OUTPUT);
  71.   pinMode(S1R, OUTPUT);
  72.   pinMode(TAL, OUTPUT);
  73.   pinMode(BAL, OUTPUT);
  74.   pinMode(LGH, OUTPUT);
  75.   pinMode(EML, OUTPUT);
  76.   pinMode(INT, OUTPUT);
  77.   pinMode(SDA, OUTPUT);
  78.   pinMode(SCL, OUTPUT);
  79.   pinMode(L2B, OUTPUT);
  80.   pinMode(L2F, OUTPUT);
  81.   pinMode(R2B, OUTPUT);
  82.   pinMode(R2F, OUTPUT);
  83.   pinMode(S2L, OUTPUT);
  84.   pinMode(S2R, OUTPUT);
  85.  
  86.   ledcSetup(pwmChannel, freq, resolution);
  87.   ledcAttachPin(S1L, pwmChannel);
  88.   ledcAttachPin(S1R, pwmChannel2);
  89.   ledcAttachPin(S2L, pwmChannel3);
  90.   ledcAttachPin(S2R, pwmChannel4);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void loop() {
  94.   leBluetooth();
  95.   delay(20);
  96.  
  97.   if (yval > 0)  {
  98.     // Serial.print(yval); Serial.println(" ");
  99.     digitalWrite(L1B, HIGH);
  100.     digitalWrite(L1F, LOW);
  101.     if (xval >= 0)
  102.     {
  103.       LM = map(abs(abs(yval) - xval), 0, 7, 0, 255);
  104.       RM = map((abs(yval)), 0, 7, 0, 255);
  105.       Serial.print(LM); Serial.print(" "); Serial.println(RM);
  106.     }
  107.  
  108.     digitalWrite(R1B, HIGH);
  109.     digitalWrite(R1F, LOW);
  110.     if (xval <= 0)
  111.     {
  112.       LM = map((abs(yval)), 0, 7, 0, 255);
  113.       RM = map(abs(abs(yval) + xval), 0, 7, 0, 255);
  114.       Serial.print(LM); Serial.print(" "); Serial.println(RM);
  115.     }
  116.   }
  117.  
  118.  
  119.   if (yval < 0 ) {
  120.     // Serial.print(yval); Serial.println(" ");
  121.     digitalWrite(L1B, LOW);
  122.     digitalWrite(L1F, HIGH);
  123.     if (xval >= 0)
  124.     {
  125.       LM = map(abs(abs(yval) - xval), 0, 7, 0, 255);
  126.       RM = map((abs(yval)), 0, 7, 0, 255);
  127.       Serial.print(LM); Serial.print(" "); Serial.println(RM);
  128.     }
  129.  
  130.     digitalWrite(R1B, LOW);
  131.     digitalWrite(R1F, HIGH);
  132.     if (xval <= 0)
  133.     {
  134.       LM = map((abs(yval)), 0, 7, 0, 255);
  135.       RM = map(abs(abs(yval) + xval), 0, 7, 0, 255);
  136.       Serial.print(LM); Serial.print(" "); Serial.println(RM);
  137.     }
  138.   }
  139.  
  140.   ledcWrite(pwmChannel, LM);
  141.   ledcWrite(pwmChannel2, RM);
  142.   ledcWrite(pwmChannel3, LMR);
  143.   ledcWrite(pwmChannel4, RMR);
  144. }
  145. //-----------------------------------------------------------------------------
  146. void leBluetooth() {
  147.   if (SerialBT.available())
  148.   {
  149.     dado = SerialBT.read();
  150.     //Serial.print(dado); Serial.print(" ");
  151.     if (dado == 'X')
  152.     {
  153.       xval = SerialBT.parseInt();
  154.       //Serial.print(xval, 2);
  155.       while (dado != '*')
  156.       {
  157.         if (SerialBT.available())
  158.         {
  159.           dado = SerialBT.read(); //Get next character from bluetooth
  160.           if (dado == 'Y')
  161.           {
  162.             //Serial.print(dado); Serial.print(" ");
  163.             yval = SerialBT.parseInt();
  164.             //Serial.print(yval, 2);
  165.           }
  166.         }
  167.       }
  168.       //Serial.println(" ");
  169.     }
  170.   }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement