Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ref: https://www.keuwl.com/electronics/rduino/bluet/10-stepper-motor/
- #define L1B 27 //left motor1 l293d pin2 (backward)
- #define L1F 26 //left motor1 l293d pin7 (forward)
- #define R1B 33 //right motor2 l293d pin15 (backward)
- #define R1F 32 //right motor2 l293d pin10 (forward)
- #define S1L 14 //left enable pin for left motor1 l293d pin1 (speed)
- #define S1R 25 //right enable pin for right motor2 l293d pin9 (speed)
- #define TAL 13 //enable taillight
- #define BAL 12 //enable bracklight
- #define LGH 5 //enable lights
- #define EML 4 //enable emergencelights
- #define INT 0 //int ADPS9960
- #define SDA 34 //sda ADPS9960
- #define SCL 35 //scl ADPS9960
- #define L2B 17 //left motor3 l293dB pin2 (backward)
- #define L2F 16 //left motor3 l293dB pin7 (forward)
- #define R2B 23 //right motor4 l293dB pin15 (backward)
- #define R2F 22 //right motor4 l293dB pin10 (forward)
- #define S2L 18 //left enable pin for left motor l293dB pin1 (speed)
- #define S2R 19 //right enable pin for right motor l293dB pin9 (speed)
- int LM = 0; //speed of left motor front
- int RM = 0; //speed of right motor front
- int LMR = 0; //speed of left motor rear
- int RMR = 0; //speed of right motor rear
- //Biblioteca para habilitar a comunicação SERIAL
- #include "BluetoothSerial.h"
- //Verifica se o bluetooth esta habilitado corretamente
- #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
- #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
- #endif
- BluetoothSerial SerialBT;
- //void leSerial();
- void leBluetooth();
- //void escreveBluetooth();
- const int freq = 7000;
- const int pwmChannel = 0;
- const int pwmChannel2 = 1;
- const int pwmChannel3 = 2;
- const int pwmChannel4 = 3;
- const int resolution = 8;
- int dutyCycle = 255;
- float xval, yval;
- char dado;
- // StartPressed; //start lights levelone
- // RedPressed; //turbo motors l293dB
- // LeftPressed; //double left
- // RightPressed; //double right
- // CirclePressed; //high ligths
- // TrianglePressed; //emergence ligths
- // CrossPressed; //brack lights
- //-----------------------------------------------------------------------------
- void setup() {
- Serial.begin(115200);
- SerialBT.begin("ESP32_BTEL");
- Serial.println("O dispositivo foi iniciado, agora você pode emparelhá-lo com bluetooth!");
- pinMode(L1B, OUTPUT);
- pinMode(L1F, OUTPUT);
- pinMode(R1B, OUTPUT);
- pinMode(R1F, OUTPUT);
- pinMode(S1L, OUTPUT);
- pinMode(S1R, OUTPUT);
- pinMode(TAL, OUTPUT);
- pinMode(BAL, OUTPUT);
- pinMode(LGH, OUTPUT);
- pinMode(EML, OUTPUT);
- pinMode(INT, OUTPUT);
- pinMode(SDA, OUTPUT);
- pinMode(SCL, OUTPUT);
- pinMode(L2B, OUTPUT);
- pinMode(L2F, OUTPUT);
- pinMode(R2B, OUTPUT);
- pinMode(R2F, OUTPUT);
- pinMode(S2L, OUTPUT);
- pinMode(S2R, OUTPUT);
- ledcSetup(pwmChannel, freq, resolution);
- ledcAttachPin(S1L, pwmChannel);
- ledcAttachPin(S1R, pwmChannel2);
- ledcAttachPin(S2L, pwmChannel3);
- ledcAttachPin(S2R, pwmChannel4);
- }
- //-----------------------------------------------------------------------------
- void loop() {
- leBluetooth();
- delay(20);
- if (yval > 0) {
- // Serial.print(yval); Serial.println(" ");
- digitalWrite(L1B, HIGH);
- digitalWrite(L1F, LOW);
- if (xval >= 0)
- {
- LM = map(abs(abs(yval) - xval), 0, 7, 0, 255);
- RM = map((abs(yval)), 0, 7, 0, 255);
- Serial.print(LM); Serial.print(" "); Serial.println(RM);
- }
- digitalWrite(R1B, HIGH);
- digitalWrite(R1F, LOW);
- if (xval <= 0)
- {
- LM = map((abs(yval)), 0, 7, 0, 255);
- RM = map(abs(abs(yval) + xval), 0, 7, 0, 255);
- Serial.print(LM); Serial.print(" "); Serial.println(RM);
- }
- }
- if (yval < 0 ) {
- // Serial.print(yval); Serial.println(" ");
- digitalWrite(L1B, LOW);
- digitalWrite(L1F, HIGH);
- if (xval >= 0)
- {
- LM = map(abs(abs(yval) - xval), 0, 7, 0, 255);
- RM = map((abs(yval)), 0, 7, 0, 255);
- Serial.print(LM); Serial.print(" "); Serial.println(RM);
- }
- digitalWrite(R1B, LOW);
- digitalWrite(R1F, HIGH);
- if (xval <= 0)
- {
- LM = map((abs(yval)), 0, 7, 0, 255);
- RM = map(abs(abs(yval) + xval), 0, 7, 0, 255);
- Serial.print(LM); Serial.print(" "); Serial.println(RM);
- }
- }
- ledcWrite(pwmChannel, LM);
- ledcWrite(pwmChannel2, RM);
- ledcWrite(pwmChannel3, LMR);
- ledcWrite(pwmChannel4, RMR);
- }
- //-----------------------------------------------------------------------------
- void leBluetooth() {
- if (SerialBT.available())
- {
- dado = SerialBT.read();
- //Serial.print(dado); Serial.print(" ");
- if (dado == 'X')
- {
- xval = SerialBT.parseInt();
- //Serial.print(xval, 2);
- while (dado != '*')
- {
- if (SerialBT.available())
- {
- dado = SerialBT.read(); //Get next character from bluetooth
- if (dado == 'Y')
- {
- //Serial.print(dado); Serial.print(" ");
- yval = SerialBT.parseInt();
- //Serial.print(yval, 2);
- }
- }
- }
- //Serial.println(" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement