2023年06月19日

スマホで動くミニ四駆を作ってみた!

スマホで動くミニ四駆を制作しました

チャットgtpさんと相談しながらつくったものです

動画

以下
プログラム

#include "Arduino.h"
#include
#include "BluetoothSerial.h"
#include

#define NUM_LEDS 1
#define DATA_PIN 27

CRGB leds[NUM_LEDS];

int pinForward = 25;//ta7291p
int pinBack = 22;
int pinSpeedForwardBack = 21;//vref
int pinFrontSteering = 32;

Servo servo1;

byte commands[4] = {0x00, 0x00, 0x00, 0x00};
byte prevCommands[4] = {0x01, 0x01, 0x01, 0x01};

unsigned long timer0 = 2000;
unsigned long timer2 = 0;

BluetoothSerial BTSerial;

void setup() {
FastLED.addLeds(leds, NUM_LEDS);
servo1.setPeriodHertz(50);
servo1.attach(pinFrontSteering, 500, 2400);
BTSerial.begin("7805mini");
Serial.begin(115200);

pinMode(pinForward, OUTPUT);
pinMode(pinBack, OUTPUT);
pinMode(pinSpeedForwardBack, OUTPUT);

leds[0] = CRGB(0x06, 0x06, 0x04); // LEDカラー: 16進数表記
FastLED.show();
}

void loop() {
if (BTSerial.available() == 4) {
timer2 = millis();
memcpy(prevCommands, commands, 4);
for (int i = 0; i < 4; i++) {
commands[i] = BTSerial.read();
}

Serial.print("Received commands: ");
for (int i = 0; i < 4; i++) {
Serial.print(commands[i], DEC);
Serial.print(" ");
}
Serial.println();

if (commands[0] == 241) {
digitalWrite(pinForward, HIGH);
digitalWrite(pinBack, LOW);
analogWrite(pinSpeedForwardBack, commands[1]);

int val = commands[2];
servo1.write(val);
leds[0] = CRGB(0xF0, 0x00, 0x00); // LEDカラー: 赤 (16進数表記)
FastLED.show();
} else if (commands[0] == 242) {
digitalWrite(pinForward, LOW);
digitalWrite(pinBack, HIGH);
analogWrite(pinSpeedForwardBack, commands[1]);

int val = commands[2];
servo1.write(val);
leds[0] = CRGB(0x00, 0xF0, 0x00); // LEDカラー: 緑 (16進数表記)
FastLED.show();
} else if (commands[0] == 243) {
digitalWrite(pinForward, LOW);
digitalWrite(pinBack, LOW);
analogWrite(pinSpeedForwardBack, 0);

int val = commands[2];
servo1.write(val);
leds[0] = CRGB(0xA0, 0x00, 0xA0); // LEDカラー: 紫 (16進数表記)
FastLED.show();
}

timer0 = timer2;
}
}





Posted by ハロ  at 16:29 │Comments(2)

この記事へのコメント
お元気ですか~
Posted by ヤマナカ at 2023年06月21日 15:48
元気ですよー
子供も増えて家も買っちゃいました。
落ち着いたら、またAPSやりたいです
まだ、床に落ちてるもの全部口の中入れるのでまだまだかかりそうですが
Posted by ハロ at 2023年07月11日 06:39
<ご注意>
書き込まれた内容は公開され、ブログの持ち主だけが削除できます。

プロフィール
ハロ
ハロ
スピードシューティング・APS・FPSをこよなく愛するハロです。
主にAPSを本腰で挑戦中
/"ハンドガン"/   
<フリー>
公式186-4×
練習会191-3×
練習200-3×
<オープン>
公式190-5×
練習会 186-5×
練習196-6×
/"ライフル"/
<フリー>
公式 121-2×
練習会160-3×
練習
<オープン>
公式 134-3×
/"取得バッチ"/
ハンドガン
グランドマスター
ライフル
シャープシューター
< 2024年04月 >
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
QRコード
QRCODE
読者登録
メールアドレスを入力して登録する事で、このブログの新着エントリーをメールでお届けいたします。解除は→こちら
現在の読者数 10人

過去記事
アクセスカウンタ
↑LO↑ ↑たぬきそふと↑ たぬきそふと PCゲーム『少女教育』を応援中!

放浪息子