*プログラム*
// DCモーターの制御
#include // Servoライブラリのインクルード
#include // AFMotorライブラリのインクルード
#include “SoftwareSerial.h”
#include “DFRobotDFPlayerMini.h”
#define BUTTON 19
DFRobotDFPlayerMini myDFPlayer;
SoftwareSerial mySerial(14, 15); // RX, TX
AF_DCMotor motor1(1); // モーター①をモーター端子1に接続
AF_DCMotor motor2(2); // モーター①をモーター端子1に接続
Servo myservo1;
Servo myservo2;// サーボモーターオブジェクトの生成
const int Servo1_PIN = 9; // サーボ接続ピンの指定
const int Servo2_PIN = 10;
int numfile; //楽曲ファイル数
int lastval=0; //ボタンの値保存用
//スマートフォンからのデータを受け取る
char data;
#define RX_PIN 14
#define TX_PIN 15
SoftwareSerial mySoftwareSerial(RX_PIN, TX_PIN); // RX, TX
void setup() {
pinMode( 16, OUTPUT );
//2つのモーターのそれぞれのピンを出力に設定
Serial.begin(9600);
motor1.run(RELEASE); // モーターを一旦停止させる
motor2.run(RELEASE); // モーターを一旦停止させる
myservo1.attach(Servo1_PIN); // サーボモーターの設定
myservo2.attach(Servo2_PIN); // サーボモーターの設定
delay(2000); //モジュール起動の待機時間
pinMode(BUTTON, INPUT);
randomSeed(analogRead(0));
// SoftwareSerialとDFPlayerの初期化
mySoftwareSerial.begin(9600);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
while(true);
}
numfile = myDFPlayer.readFileCounts(DFPLAYER_DEVICE_SD);
}
void loop() {
lastval = (lastval << 1) | digitalRead(BUTTON); // チャタリング回避 motor1.setSpeed(255); // モータースピードを設定(0~255の範囲) motor2.setSpeed(255); // モータースピードを設定(0~255の範囲) if (Serial.available() > 0) { //もし、スマートフォンからのデータがあったら
data = Serial.read();
}
if (data == ‘F’) { //↑ボタンを押すと前進
Serial.println(“前進”);
myservo1.write(60); // 指定角度にサーボを動かす
motor1.run(FORWARD); // モーター1とモーター2を正回転させる
} else if (data == ‘R’) { //→ボタンを押すと右回転
Serial.println(“右回転”);
myservo1.write(30);
} else if (data == ‘L’) { //→ボタンを押すと右回転
Serial.println(“左回転”);
myservo1.write(90);
} else if (data == ‘I’) { //←ボタンを押すと左回転
Serial.println(“右回転前進”);
myservo1.write(30);
motor1.run(FORWARD);
} else if (data == ‘J’) { //←ボタンを押すと左回転
Serial.println(“右回転後進”);
myservo1.write(30);
motor1.run(BACKWARD);
} else if (data == ‘H’) { //←ボタンを押すと左回転
Serial.println(“左回転後進”);
myservo1.write(90);
motor1.run(BACKWARD);
} else if (data == ‘G’) { //←ボタンを押すと左回転
Serial.println(“左回転後進”);
myservo1.write(90);
motor1.run(FORWARD);
} else if (data == ‘B’) { //↓ボタンを押すと後進
Serial.println(“後進”);
myservo1.write(60);
motor1.run(BACKWARD); // モーター1とモーター2を逆回転させる
//mp3_play (1);
//delay (6000);
//mp3_stop ();
}else if (data == ‘W’) { //ボタンが押されなければ停止
Serial.println(“ARM120”);
myservo2.write(120);
motor1.run(RELEASE);
}else if (data == ‘w’) { //ボタンが押されなければ停止
Serial.println(“ARM60”);
myservo2.write(60);
motor1.run(RELEASE);
} else if (data == ‘U’) { //ボタンが押されなければ停止
Serial.println(“head”);
myservo1.write(60);
motor2.run(FORWARD);
} else if (data == ‘u’) { //ボタンが押されなければ停止
Serial.println(“headoff”);
myservo1.write(60);
motor2.run(RELEASE);
} else if (data == ‘V’) { //ボタンが押されなければ停止
Serial.println(“music”);
myDFPlayer.play(random(numfile)+1); //ランダムにファイルを再生 [1, numfile]
delay (10);
} else if (data == ‘v’) { //ボタンが押されなければ停止
Serial.println(“music_off”);
} else if (data == ‘X’){
digitalWrite( 16, HIGH );
} else if (data == ‘x’){
digitalWrite( 16, LOW );
} else if (data == ‘S’) { //ボタンが押されなければ停止
myservo1.write(60);
motor1.run(RELEASE);
}
}