The brain of the automata is an Atmega microcontroller, either Atmega8 or Atmega368. It is programed with Arduino in order to drive the two DC motors which move the mobile under the control of infrared detectors.
Here is the code:
int inPin5 = 0;
int val5 = 0;
int inPin0 = 5;
int val0 = 0;
int seuil = 500;
int outPin7= 2;
int outPin8= 3;
long randNumber;
long randStop;
unsigned long n;
int motorPin1 = 9;
int motorPin2 = 10;
int motorPin3 = 11;
int motorPin4 = 12;
int delayTime = 500;
int tourned = 80;
int tourneg = 80;
void setup() {
randomSeed(analogRead(3));
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
pinMode(outPin7, OUTPUT);
pinMode(outPin8, OUTPUT);
}
void moteuravance(int mot1, boolean mot2, int mot3, boolean mot4);
void moteur(int mot1, boolean mot2, int mot3, boolean mot4, int duree);
void loop() {
if(millis() < 20000){
moteuravance(0, false, 0, false);
}else{
randStop = random(10000, 60000);
n = millis() % randStop;
val5 = analogRead(inPin5);
val0 = analogRead(inPin0);
if (val0 > seuil && val5 < seuil)
{
delayTime = random(1000,2000);
randNumber = random(5, 20);
digitalWrite(outPin7, true);
moteur(0, false, tourned, true, delayTime);
tourned = tourned - randNumber;
tourneg = 80;
}if (val0 < seuil && val5 > seuil)
{
delayTime = random(1000,2000);
randNumber = random(5, 20);
digitalWrite(outPin7, true);
moteur(tourneg, true, 0, false, delayTime);
tourneg = tourneg - randNumber;
tourned = 80;
}if (val0 > seuil && val5 > seuil)
{
digitalWrite(outPin7, false);
digitalWrite(outPin8, false);
moteur(80, false, 80, false, 2000);
tourned = 80;
tourneg = 80;
}if (val0 < seuil && val5 < seuil)
{
digitalWrite(outPin7, false);
digitalWrite(outPin8, false);
moteuravance(160, true, 160, true);
}
while(n < randStop/2 && n > randStop/4)
{
n = millis() % randStop;
moteuravance(0, false, 0, false);
}
}
}
void moteuravance(int mot1, boolean mot2, int mot3, boolean mot4)
{
analogWrite(motorPin1, mot1);
digitalWrite(motorPin2, mot2);
analogWrite(motorPin3, mot3);
digitalWrite(motorPin4, mot4);
}
void moteur(int mot1, boolean mot2, int mot3, boolean mot4, int duree)
{
analogWrite(motorPin1, mot1);
digitalWrite(motorPin2, mot2);
analogWrite(motorPin3, mot3);
digitalWrite(motorPin4, mot4);
delay(duree);
}
While the automata is inside the white circle the IR detectors send a signal greater than a threshold "seuil" on both pin 0 and 5 and both motors turn in the forward direction with the "moteuravance" function. When one of the IR detector goes above the black color surrounding the white circle it goes below the threshold and the mobile turns away from the boundary to remain inside the circle. outPin 7 and outPin 8 are used to trigger specific actions through an XBee module embarked on an Arduino board.
Aucun commentaire:
Enregistrer un commentaire