学習させた用語とindex†
0 | 前 |
1 | 後ろ |
2 | 左 |
3 | 右 |
4 | こんにちは |
5 | へいshiri |
6 | おみくじ |
7 | 元気? |
プログラム†
#include <time.h>
#include <SoftwareSerial.h>
#define motorR1 8
#define motorR2 9
#define motorRout 10
#define motorL1 11
#define motorL2 12
#define motorLout 13
SoftwareSerial mySerial(6, 7); // RX, TX
int i;
int phrase_state;
int motor_sec;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
mySerial.println("?");
srand(time(NULL));
pinMode(motorR1, OUTPUT); //信号用ピン
pinMode(motorR2, OUTPUT); //信号用ピン
pinMode(motorL1, OUTPUT); //信号用ピン
pinMode(motorL2, OUTPUT); //信号用ピン
i = 0;
phrase_state = 1;
motor_sec = 3000;
}
//静止
void stop() {
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, LOW);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, LOW);
}
//前進
void forward() {
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
}
//後退
void back() {
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
}
void left() {
digitalWrite(motorR1, HIGH);
digitalWrite(motorR2, LOW);
digitalWrite(motorL1, LOW);
digitalWrite(motorL2, HIGH);
}
void right() {
digitalWrite(motorR1, LOW);
digitalWrite(motorR2, HIGH);
digitalWrite(motorL1, HIGH);
digitalWrite(motorL2, LOW);
}
void motorCtr(int select, int sec) {
switch (select) {
case 0:
stop();
break;
case 1:
forward();
break;
case 2:
back();
break;
case 3:
left();
break;
case 4:
right();
break;
}
analogWrite(motorRout, 255); //出力値:1~255
analogWrite(motorLout, 255); //出力値:1~255
delay(sec);
}
void loop() {
//i = ++i % 5;
//motorCtr(i, motor_sec);
byte x;
if (Serial.available()) {
x = Serial.read();
if (x == 'S') {
/* motor stops */
motorCtr(0, motor_sec);
} else if (x == 'F') {
/* motor foward */
motorCtr(1, motor_sec);
} else if (x == 'B') {
/* motor back */
motorCtr(2, motor_sec);
} else if (x == 'R') {
/* motor right */
motorCtr(3, motor_sec);
} else if (x == 'L') {
/* motor left */
motorCtr(4, motor_sec);
} else if (x == 'G') {
/* greeting */
mySerial.print("konnitiwa--\r");
} else if (x == '') {
mySerial.print("watashiwashiridewaarimasen");
} else if (x == '') {
int omikuzi = rand() % 3;
switch(omikuzi) {
case 0:
mySerial.print("daikitidesu");
mySerial.print("omedetougozaimasuooataridesu");
break;
case 1:
mySerial.print("kyoudesu");
mySerial.print("kyouwaunngawaruimitaidesu");
break;
case 2:
mySerial.print("yazawaeikiti");
delay(1000);
mySerial.print("gomennnasai");
break;
}
mySerial.print("");
}
} else {
int action = rand() % 5;
switch (action) {
case 0:
mySerial.print("sorosorotukaretana---\r");
break;
default:
motorCtr(random() % 5, 1000);
}
}
}