ESP, IoT МАШИНКА НА ESP32(в состоянии модернизации)+андройд приложуха

МАШИНКА НА ESP32(в состоянии модернизации)+андройд приложуха
Всем привет.Меня зовут Иван.Скажу сразу Я не программист,не разработчик.и вообще 2-3 месяца назад ничего об этом не знал)Решил выложить то чем сейчас занимаюсь все свободное время.Просьба знатоков не судить строго.Мой проект машинки на (пока что) блютуз управлении .все как вы любите из Г и палок.куплены только двигатели с колесами и esp32.+ws2812.
IMG_20201206_201449.jpg
блютуз есп32(для драйвера безуправления скоростью):
#include <Servo.h>

//ESP32 Bluetooh car
//By Technical_Tamizha
//Circuit Diagram for this project is in my Youtube channel
//Channel Link : https://www.youtube.com/channel/UC1VT8SUJ7yvIkE4eCzXVSNA

#include <WS2812FX.h>
#include "ESP32_RMT_Driver.h"

// The ESP32's RMT hardware supports up to 8 channels, so it
// can drive up to 8 independent WS2812FX instances. We'll use 2.
WS2812FX ws2812fx1 = WS2812FX(12, 12, NEO_GRB  + NEO_KHZ800); // 144 RGB LEDs driven by GPIO_12


#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

char receivedChar;// received value will be stored as CHAR in this variable


const int in1 = 16;
const int in2 = 17;
// connections for drive Motor FL & BL
const int in3 = 14;
const int in4 = 27;
int MaxSpd = 255;
int SERVO_1 = 21;
int SERVO_2 = 33;
Servo SERVO1;
Servo SERVO2;
const int LEDG = 32;
const int LEDB = 35;
const int LEDR = 34;



void setup() {
  Serial.begin(115200);
  SerialBT.begin("VaNBot"); //You can change your Bluetooth device name
  pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDR, OUTPUT);
  SERVO1.attach(SERVO_1);
  SERVO2.attach(SERVO_2);
   ws2812fx1.init();
   ws2812fx1.setBrightness(204); // set the overall LED brightnesses
   rmt_tx_int(RMT_CHANNEL_0, ws2812fx1.getPin()); // assign ws2812fx1 to RMT channel 0
  // set the custom show function to forgo the NeoPixel
    // start'em up

}

void Forward(){
   
    digitalWrite(in1, MaxSpd);
digitalWrite(in2, LOW);
digitalWrite(in3, MaxSpd);
digitalWrite(in4, LOW);
digitalWrite(LEDB, HIGH);

}
void Backward(){
   digitalWrite(in1, LOW);
digitalWrite(in2, MaxSpd);
digitalWrite(in3, LOW);
digitalWrite(in4, MaxSpd);
digitalWrite(LEDG, HIGH);

}
void Left(){
    digitalWrite(in1, LOW);
digitalWrite(in2, MaxSpd);
digitalWrite(in3, HIGH);
digitalWrite(in4, MaxSpd);
}
void LeftF(){
   digitalWrite(in1, 50);
digitalWrite(in2, LOW);
digitalWrite(in3, MaxSpd);
digitalWrite(in4, HIGH);
}
void LeftB(){
digitalWrite(in1, LOW);
digitalWrite(in2, MaxSpd);
digitalWrite(in3, HIGH);
digitalWrite(in4, 50);
}
void Right(){
    digitalWrite(in1, MaxSpd);
digitalWrite(in2, HIGH);
digitalWrite(in3, MaxSpd);
digitalWrite(in4, LOW);
}
void RightF(){
digitalWrite(in1, MaxSpd);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, 100);
}
void RightB(){
digitalWrite(in1, HIGH);
digitalWrite(in2, 100);
digitalWrite(in3, MaxSpd);
digitalWrite(in4, LOW);
}
void Ledon() {
ws2812fx1.start();
ws2812fx1.setMode(FX_MODE_RUNNING_COLOR, BLUE);
}
void Ledoff() {
  ws2812fx1.stop();
}
  void Blueon() {
  digitalWrite(LEDB, HIGH);

}
void Blueoff() {
  digitalWrite(LEDB, LOW);
}
  void Redon() {
  digitalWrite(LEDR, HIGH);
  }
void Stop(){
  digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void myCustomShow1() {
  uint8_t *pixels = ws2812fx1.getPixels();
  // numBytes is one more then the size of the ws2812fx's *pixels array.
  // the extra byte is used by the driver to insert the LED reset pulse at the end.
  uint16_t numBytes = ws2812fx1.getNumBytes() + 1;
  rmt_write_sample(RMT_CHANNEL_0, pixels, numBytes, false); // channel 0
}
//void girarSERVO1(int grados){
  //SERVO1.write(grados);
//}
//void girarSERVO2(int grados){
  //SERVO2.write(grados);
//}

void loop() {
ws2812fx1.service();

    receivedChar =(char)SerialBT.read();
  if (Serial.available()) {
    SerialBT.write(Serial.read());

  }
  if (SerialBT.available()) {
 
    Serial.print ("Received:");//print on serial monitor
    Serial.println(receivedChar);//print on serial monitor
 
    if(receivedChar == 'F')
    {
      Forward();
    }

    if(receivedChar == 'B')
    {

      Backward();
    }      
     if(receivedChar == 'L')
    {

      Left();
    }    
    if(receivedChar == 'R')
    {

      Right();
    }
     if(receivedChar == 'Z')
    {

      LeftF();
    }    
    if(receivedChar == 'Q')
    {

      RightF();
    }
    if(receivedChar == 'E')
    {
        LeftB();
    }    
    if(receivedChar == 'W')
    {

      RightB();
    }
    if(receivedChar == 'S')
    {
   
      Stop();
    }
    if(receivedChar == 'M')
    {
   
     Ledon();
    }
     if(receivedChar == 'm')
    {
   
      Ledoff();
    }
    if(receivedChar == 'N')
    {
   
      Blueon();
    }
     if(receivedChar == 'n')
    {
   
      Blueoff();
    }
    if(receivedChar == 'X')
    {
   
      Redon();
    }
  }
  delay(20);
}
блютуз есп32(для драйвера с управлением скоростью):
#include <esp32_arduino_rmt_van_rx.h>
#include <esp32_rmt_van_rx.h>

#include <Servo.h>

//ESP32 Bluetooh car
//By Technical_Tamizha
//Circuit Diagram for this project is in my Youtube channel
//Channel Link : https://www.youtube.com/channel/UC1VT8SUJ7yvIkE4eCzXVSNA
#include <WS2812FX.h>
#include "ESP32_RMT_Driver.h"

// The ESP32's RMT hardware supports up to 8 channels, so it
// can drive up to 8 independent WS2812FX instances. We'll use 2.
WS2812FX ws2812fx1 = WS2812FX(8, 12, NEO_GRB  + NEO_KHZ800); // 144 RGB LEDs driven by GPIO_12


#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

char receivedChar;// received value will be stored as CHAR in this variable

const int DIR_2 = 17; //ESP32 pins (MR=Right Motor) (ML=Left Motor) (1=Forward) (2=Backward)
const int DIR_1 = 14;
const int SPEED_1 = 27;
const int SPEED_2 = 16 ;
int SERVO_1 = 12;
int SERVO_2 = 13;
Servo SERVO1;
Servo SERVO2;
const int LEDG = 5;
const int LEDB = 23;
const int LEDR = 14;
int SPEED = 255;
void setup() {
  Serial.begin(115200);
  SerialBT.begin("VaNBot"); //You can change your Bluetooth device name
  pinMode(SPEED_1, OUTPUT);
  pinMode(SPEED_2, OUTPUT);
  pinMode(DIR_1, OUTPUT);
  pinMode(DIR_2, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDR, OUTPUT);
  SERVO1.attach(SERVO_1);
  SERVO2.attach(SERVO_2);
   ws2812fx1.init();
   ws2812fx1.setBrightness(254); // set the overall LED brightnesses
   rmt_tx_int(RMT_CHANNEL_0, ws2812fx1.getPin()); // assign ws2812fx1 to RMT channel 0
ws2812fx1.setCustomShow(myCustomShow1); // set the custom show function to forgo the NeoPixel
   ws2812fx1.start(); // start'em up

}

void Forward(){
         ws2812fx1.setSegment(0, 0, 8-1, FX_MODE_DUAL_SCAN , BLUE, 300, FADE_GLACIAL); // setup each ws2812fx's effect

    digitalWrite(DIR_1, HIGH);
    digitalWrite(DIR_2, HIGH);
    digitalWrite(SPEED_1, 255);
    digitalWrite(SPEED_2, 255);
}
void Backward(){
      digitalWrite(DIR_1, LOW);
    digitalWrite(DIR_2, LOW);
    digitalWrite(SPEED_1, 255);
    digitalWrite(SPEED_2, 255);
         ws2812fx1.setSegment(0, 0, 8-1, FX_MODE_LARSON_SCANNER  ,RED, 200, REVERSE ); // setup each ws2812fx's effect

}
void Left(){
     digitalWrite(DIR_1, HIGH);
    digitalWrite(DIR_2, LOW);
    digitalWrite(SPEED_1, 255);
    digitalWrite(SPEED_2, 255);
             ws2812fx1.setSegment(0, 0, 4-1, FX_MODE_LARSON_SCANNER  ,BLUE, 200, REVERSE ); // setup each ws2812fx's effect

}
void LeftF(){
     digitalWrite(DIR_1, HIGH);
    digitalWrite(DIR_2, HIGH);
    digitalWrite(SPEED_1, 255);
    digitalWrite(SPEED_2, 50);
}
void LeftB(){
digitalWrite(DIR_1,LOW);
    digitalWrite(DIR_2,LOW);
    digitalWrite(SPEED_1, 255);
    digitalWrite(SPEED_2, 50);
}
void Right(){
                          ws2812fx1.setSegment(0, 0, 8-4, FX_MODE_LARSON_SCANNER  ,BLUE, 200, REVERSE ); // setup each ws2812fx's effect

    digitalWrite(DIR_1, LOW);
    digitalWrite(DIR_2, HIGH);
    digitalWrite(SPEED_1, 255);
    digitalWrite(SPEED_2, 255);
}
void RightF(){
digitalWrite(DIR_1, HIGH);
    digitalWrite(DIR_2, HIGH);
    digitalWrite(SPEED_1, 50);
    digitalWrite(SPEED_2, 255);
    ws2812fx1.setSegment(0, 0, 8-4, FX_MODE_STROBE_RAINBOW ,PURPLE, 200, FADE_FAST ); // setup each ws2812fx's effect

}
void RightB(){
digitalWrite(DIR_1,LOW);
    digitalWrite(DIR_2,LOW);
    digitalWrite(SPEED_1, 50);
    digitalWrite(SPEED_2, 255);
        ws2812fx1.setSegment(0, 0, 8-1, FX_MODE_STROBE_RAINBOW , PURPLE, 200, FADE_FAST ); // setup each ws2812fx's effect

}
void Greenon() {
  digitalWrite(LEDG, HIGH);
  ws2812fx1.setSegment(0, 0, 8-1, FX_MODE_SCAN,GREEN , 1000, GAMMA );
}
void Greenoff() {
  digitalWrite(LEDG, LOW);
  ws2812fx1.setBrightness(100);
}
  void Blueon() {
  digitalWrite(LEDB, HIGH);
   ws2812fx1.setSegment(0, 0, 8-4, FX_MODE_COMET, WHITE , 1000, NO_OPTIONS);
   ws2812fx1.setSegment(1, 0, 3-1, FX_MODE_RUNNING_LIGHTS , PINK , 500, FADE_SLOW);

}
void Blueoff() {
  digitalWrite(LEDB, LOW);
ws2812fx1.setSegment(1, 0, 3-1,  FX_MODE_THEATER_CHASE  , PURPLE , 500, FADE_SLOW);

}
  void Redon() {
     ws2812fx1.setSegment(0, 0, 10-1, FX_MODE_DUAL_SCAN  , BLUE , 1000, SIZE_XLARGE);

  digitalWrite(LEDR, HIGH);
  }
void Stop(){
     digitalWrite(LEDR, LOW);
    digitalWrite(SPEED_1, 0);
    digitalWrite(SPEED_2, 0);
ws2812fx1.setSegment(0, 0, 10-1, FX_MODE_CHASE_BLACKOUT_RAINBOW  , (ORANGE, BLUE) , 1000, NO_OPTIONS);
}
void myCustomShow1(void) {
  uint8_t *pixels = ws2812fx1.getPixels();
  // numBytes is one more then the size of the ws2812fx's *pixels array.
  // the extra byte is used by the driver to insert the LED reset pulse at the end.
  uint16_t numBytes = ws2812fx1.getNumBytes() + 1;
  rmt_write_sample(RMT_CHANNEL_0, pixels, numBytes, false); // channel 0
}//void girarSERVO1(int grados){
  //SERVO1.write(grados);
//}
//void girarSERVO2(int grados){
  //SERVO2.write(grados);
//}

void loop() {

ws2812fx1.service(); // service each ws2812fx instance

    receivedChar =(char)SerialBT.read();
  if (Serial.available()) {
    SerialBT.write(Serial.read());

  }
  if (SerialBT.available()) {
 
    Serial.print ("Received:");//print on serial monitor
    Serial.println(receivedChar);//print on serial monitor
 
    if(receivedChar == 'F')
    {
      Forward();
    }

    if(receivedChar == 'B')
    {

      Backward();
    }      
     if(receivedChar == 'L')
    {

      Left();
    }    
    if(receivedChar == 'R')
    {

      Right();
    }
     if(receivedChar == 'Z')
    {

      LeftF();
    }    
    if(receivedChar == 'Q')
    {

      RightF();
    }
    if(receivedChar == 'E')
    {
        LeftB();
    }    
    if(receivedChar == 'W')
    {

      RightB();
    }
    if(receivedChar == 'S')
    {
   
      Stop();
    }
    if(receivedChar == 'M')
    {
   
           Greenon();

    }
     if(receivedChar == 'm')
    {
   
      Greenoff();
    }
    if(receivedChar == 'N')
    {
   
      Blueon();
    }
     if(receivedChar == 'n')
    {
   
      Blueoff();
    }
    if(receivedChar == 'X')
    {
   
      Redon();
    }
  }
  delay(20);
}
esp32 rmt driver:
#include "driver/rmt.h"

#define APB_CLK_MHZ 80 // default RMT CLK source (80MHz)
#define RMT_CLK_DIV  2 // RMT CLK divider
#define RMT_TICK (RMT_CLK_DIV * 1000 / APB_CLK_MHZ) // 25ns

// timing parameters for WS2812B LEDs. you may need to
// tweek these if you're using a different kind of LED
#define T1_TICKS      250 / RMT_TICK // 250ns
#define T2_TICKS      625 / RMT_TICK // 625ns
#define T3_TICKS      375 / RMT_TICK // 375ns
#define RESET_TICKS 50000 / RMT_TICK // 50us

/*
* Convert uint8_t type of data to rmt format data.
*/
static void IRAM_ATTR u8_to_rmt(const void* src, rmt_item32_t* dest, size_t src_size,
                         size_t wanted_num, size_t* translated_size, size_t* item_num) {
    if(src == NULL || dest == NULL) {
        *translated_size = 0;
        *item_num = 0;
        return;
    }
    const rmt_item32_t bit0  = {{{ T1_TICKS,            1, T2_TICKS + T3_TICKS, 0 }}}; //Logical 0
    const rmt_item32_t bit1  = {{{ T1_TICKS + T2_TICKS, 1, T3_TICKS           , 0 }}}; //Logical 1
    const rmt_item32_t reset = {{{ RESET_TICKS/2      , 0, RESET_TICKS/2      , 0 }}}; //Reset
    size_t size = 0;
    size_t num = 0;
    uint8_t *psrc = (uint8_t *)src;
    rmt_item32_t* pdest = dest;
    while (size < src_size && num < wanted_num) {
      if(size < src_size - 1) { // have more pixel data, so translate into RMT items
        (pdest++)->val =  (*psrc & B10000000) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B01000000) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B00100000) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B00010000) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B00001000) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B00000100) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B00000010) ? bit1.val : bit0.val;
        (pdest++)->val =  (*psrc & B00000001) ? bit1.val : bit0.val;
        num += 8;
      } else { // no more pixel data, last RMT item is the reset pulse
        (pdest++)->val =  reset.val;
        num++;
      }
      size++;
      psrc++;
    }
    *translated_size = size;
    *item_num = num;
}

/*
* Initialize the RMT Tx channel
*/
static void rmt_tx_int(rmt_channel_t channel, uint8_t gpio) {
    rmt_config_t config;
    config.rmt_mode = RMT_MODE_TX;
    config.channel = channel;
    config.gpio_num = gpio_num_t(gpio);
    config.clk_div = RMT_CLK_DIV;
    config.mem_block_num = 1; // 64 pulse "items" per block
    config.tx_config.loop_en = 0;
    config.tx_config.carrier_en = 0;
    config.tx_config.idle_output_en = 1;
    config.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;

    rmt_config(&config);
    rmt_driver_install(config.channel, 0, 0);
    rmt_translator_init(config.channel, u8_to_rmt);
}
приложение андройд(тоже сам собирал)
 

Вложения

Изменено:

Комментарии