No.170111 |
|||||||
KD-07, Rover with 2 Eyes |
|||||||
|
|||||||
|
|||||||
|
|||||||
|
|||||||
|
|||||||
// とりあえず動いた
#define eyeL A0
#define eyeR A1
int notes[] = {262,294,330,440}; // 周波数を配列で記憶
int digitalLow,valL,valR;
void setup()
{
Serial.begin(9600);
pinMode(3,OUTPUT); // R Motor Forward
pinMode(2,OUTPUT); // R Motor Reverce
pinMode(5,OUTPUT); // L Motor Forward
pinMode(4,OUTPUT); // L Motor Reverce
pinMode(10, OUTPUT); // LED Left, Yellow
pinMode(9, OUTPUT); // LED Front, Blue
pinMode(8, OUTPUT); // LED Right, Yellow
pinMode(7, OUTPUT); // LED Rear, Red
}
void loop()
{
PORTD=0;
if(Serial.available() > 21)
{
if(Serial.read()==0x7E)
{
for(int i=1; i<=19; i++)
{
byte discard=Serial.read();
}
digitalLow=Serial.read();
if(digitalLow == 0x0D) //リモコン入力1101
{
int count = 0;
while (count < 5)
{
digitalWrite(9,HIGH); // LED 青 点灯
digitalWrite(3,HIGH);
digitalWrite(2,LOW);
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
delay(200); // 0.2秒毎に距離を測定
valR=analogRead(eyeR); // 右側に障害を発見
if(valR >= 348) // 障害物までの距離15cm以下なら
{
digitalWrite(9, LOW); // LED 青 消灯
tone(6, notes[3]);
digitalWrite(7, HIGH); // LED 赤 点灯
delay (100);
noTone(6);
digitalWrite(3,LOW); // 後退
digitalWrite(2,HIGH);
digitalWrite(5,LOW);
digitalWrite(4,HIGH);
delay(1200);
digitalWrite(7, LOW); // LED 赤 消灯
tone(6, notes[0]);
digitalWrite(10, HIGH); // LED 左 点灯
digitalWrite(3,HIGH); // 左旋回
digitalWrite(2,LOW);
digitalWrite(5,LOW);
digitalWrite(4,HIGH);
delay(1000);
noTone(6);
digitalWrite(10, LOW); // LED 左 消灯
count ++;
}
valL=analogRead(eyeL); // 右側に障害を発見
if(valL >= 348) // 障害物までの距離15cm以下なら
{
digitalWrite(9, LOW); // LED 青 消灯
tone(6, notes[3]);
digitalWrite(7, HIGH); // LED 赤 点灯
delay (100);
noTone(6);
digitalWrite(3,LOW); // 後退
digitalWrite(2,HIGH);
digitalWrite(5,LOW);
digitalWrite(4,HIGH);
delay(1200);
digitalWrite(7, LOW); // LED 赤 消灯
tone(6, notes[1]);
digitalWrite(8, HIGH); // LED 右 点灯
digitalWrite(3,LOW); // 右旋回
digitalWrite(2,HIGH);
digitalWrite(5,HIGH);
digitalWrite(4,LOW);
delay(1000);
noTone(6);
digitalWrite(8, LOW);
count ++;
}
}
}
}
}
}
|
|||||||
|
|||||||
Back |