親子で作る!自動で歩く昆虫ロボット(覚醒説明資料とプログラム)

GADGET RENESASの岡宮です。

ロボット覚醒プロジェクト第一弾「親子で作る!自動で歩く昆虫ロボット」の資料と、

マイコンボード用のプログラムを掲載します。

-----------------------------------------------------------------------------------------------------

/*

ロボット覚醒プロジェクト:
物に避けて進むプログラム

This program is public domain.
*/

#define LED_R 22
#define LED_G 23
#define LED_B 24

void setup() {
// put your setup code here, to run once:
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:
unsigned int eye_right = analogRead(A0);
unsigned int eye_left = analogRead(A1);

if (eye_right < 700 && eye_left < 700){ // Both eye see nothing
// forward
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, LOW);
analogWrite(5, 200);
analogWrite(3, 0);
analogWrite(6, 255);
analogWrite(9, 0);
} else if (eye_right >= 700 && eye_left < 700) { // Right eye see a thing
// turn left
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
analogWrite(5, 0);
analogWrite(3, 0);
analogWrite(6, 255);
analogWrite(9, 0);
} else if (eye_right < 700 && eye_left >= 700) { // Left eye see a thing
// turn right
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, HIGH);
analogWrite(5, 200);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
} else {
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
analogWrite(5, 200);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 255);
}
delay(500);

}

--------------------------------------------------------------

/*

ロボット覚醒プロジェクト:
物に近づいていくプログラム

This program is public domain.
*/

#define LED_R 22
#define LED_G 23
#define LED_B 24

void setup() {
// put your setup code here, to run once:
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
}

void loop() {
// put your main code here, to run repeatedly:
unsigned int eye_right = analogRead(A0);
unsigned int eye_left = analogRead(A1);

if (eye_right < 700 && eye_left < 700){ // Both eye see nothing
// stop
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
analogWrite(5, 0);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
} else if (eye_right >= 700 && eye_left < 700) { // Right eye see a thing
// turn right
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, HIGH);
analogWrite(5, 200);
analogWrite(3, 0);
analogWrite(6, 0);
analogWrite(9, 0);
} else if (eye_right < 700 && eye_left >= 700) { // Left eye see a thing
// turn left
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
analogWrite(5, 0);
analogWrite(3, 0);
analogWrite(6, 255);
analogWrite(9, 0);
} else {
// forward
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, LOW);
analogWrite(5, 200);
analogWrite(3, 0);
analogWrite(6, 255);
analogWrite(9, 0);
}
delay(500);

}

--------------------------------------------------------------------------------

/*

ロボット覚醒プロジェクト:
距離センサーの値をモニターするだけのプログラム

This program is public domain.
*/

#define LED_R 22
#define LED_G 23
#define LED_B 24

void setup() {
// put your setup code here, to run once:
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);

Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
unsigned int eye_right = analogRead(A0);
unsigned int eye_left = analogRead(A1);

Serial.print("left:");
Serial.print(eye_left);
Serial.print(" ");
Serial.print("right:");
Serial.println(eye_right);

if (eye_right < 700 && eye_left < 700){ // Both eye see nothing
// forward
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, LOW);
} else if (eye_right >= 700 && eye_left < 700) { // Right eye see a thing
// turn left
digitalWrite(LED_R, LOW);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
} else if (eye_right < 700 && eye_left >= 700) { // Left eye see a thing
// turn right
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, LOW);
digitalWrite(LED_B, HIGH);
} else {
digitalWrite(LED_R, HIGH);
digitalWrite(LED_G, HIGH);
digitalWrite(LED_B, HIGH);
}
delay(500);

}