二つのマイコンを無線で接続出来たらラジコン作りたくなりますよね?ということで作ってみました。3号機です。

はじめに

前回ESP-WROOM-32同士をBluetoothで接続してシリアル通信を無線化しました。
touch-sp.hatenablog.com
これが出来たらラジコン作りたくなります。

ということで作ってみました。

完成品


コントローラー


特徴

Groveコネクタを利用してなるべくはんだ付けをしない方針としました。

コントローラ側はパーツを買ってきて繋げただけです。車側も数か所はんだ付けしただけです。

写真をよく見るとわかりますがホットボンドで雑にパーツを固定しています。

工作が好きな訳じゃないんです。プログラミンがやりたいだけなんです。

Arduinoスケッチ

車側

#include "BluetoothSerial.h"
#include "ESP32Servo.h"

const int motor1 = 26;
const int motor2 = 15;

int motion;  // 0:stop  1:forward  2:back

Servo servo1; // create four servo objects 
const int servo1Pin = 25;

BluetoothSerial SerialBT;

String MACadd = "7C:9E:BD:EA:3D:DA";
uint8_t address[6]  = {0x7C, 0x9E, 0xBD, 0xEA, 0x3D, 0xDA};

void setup() {
  Serial.begin(115200);
  SerialBT.begin("bluetooth", true);  // isMaster=true

  SerialBT.connect(address);
  
  pinMode(motor1, OUTPUT);
  pinMode(motor2, OUTPUT);
  analogWrite(motor1, 0);
  analogWrite(motor2, 0);
  motion = 0;

  servo1.attach(servo1Pin);
}

void loop() {
  if (SerialBT.available()) {
    int var = SerialBT.read();
    switch(var){
      case 0:
        analogWrite(motor1, 0);
        analogWrite(motor2, 0);
        motion = 0;
        break;
      case 1:
        if(motion==2){
          analogWrite(motor1, 0);
          analogWrite(motor2, 0);
          delay(200);
        }
        analogWrite(motor1, 100);
        analogWrite(motor2, 0);
        motion = 1;
        break;
      case 2:
        if(motion==1){
          analogWrite(motor1, 0);
          analogWrite(motor2, 0);
          delay(200);
        }
        analogWrite(motor1, 0);
        analogWrite(motor2, 100);
        motion = 2;
        break;
      default:
        if(var >= 60 && var <=120){
          servo1.write(var);
        }
        break;
    }
    delay(200);
  }
}

コントローラー側

#include "BluetoothSerial.h"
#include "Adafruit_NeoPixel.h"

const int buttonON = LOW;    // ボタンが押されているとピンの値はLOW

const int buttonPin1 = 14; //red button
const int buttonPin2 = 25; // blue button
const int buttonPin3 = 26;

const int LED_button = 15;

const int sensorPin = 4;

BluetoothSerial SerialBT;
Adafruit_NeoPixel led_button = Adafruit_NeoPixel(1, LED_button, NEO_GRB + NEO_KHZ800);

int potVal;
int potVal_arrange;
int angle_current;
int angle_previous;

float float_angle;
int move;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("bluetooth", false); // isMaster=false

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  angle_previous = 90;

  led_button.begin();
  led_button.setPixelColor(0, led_button.Color(100, 0, 0));
  led_button.show();

  delay(500);
}

// 1:forward 2:back 3:stop
void loop() {
  if (digitalRead(buttonPin1) == buttonON) {
    SerialBT.write(1);
    while (digitalRead(buttonPin1) == buttonON);
    delay(100); 
  }
  if (digitalRead(buttonPin2) == buttonON) {
    SerialBT.write(2);
    while (digitalRead(buttonPin2) == buttonON);
    delay(100); 
  }
  if (digitalRead(buttonPin3) == buttonON) {
    SerialBT.write(0);
    while (digitalRead(buttonPin3) == buttonON);
    delay(100); 
  }

  //angle 0 - 2550
  //center 1395

  potVal = analogRead(sensorPin);
  if(potVal != 4095){

    if(potVal > 2550){
      potVal_arrange = 2550;
    } else if (potVal < 1){
      potVal_arrange = 0;
    } else{
      potVal_arrange = potVal;
    }

    float_angle = (potVal_arrange - 1395) / 500;

    move = int(float_angle) * 10;

    angle_current = 90 + move;

    if (angle_previous != angle_current){
      SerialBT.write(angle_current);
      //Serial.print(angle_current);
      angle_previous = angle_current;
      delay(100); 
    }
  }
}

過去に作ったもの

以前はXBeeというものを使って無線化をしていました。

今回使ったもののように最初からBluetooth機能が付いているマイコンって本当に素晴らしい!

初号機

touch-sp.hatenablog.com

2号機

touch-sp.hatenablog.com



このエントリーをはてなブックマークに追加