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;
}
}
チャット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
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;
}
}