mardi 22 février 2011

Codes - 2

An XBee module attached to an Arduino board is also embarked on the mobile.


The orange wire is connected to pin 12 of the board and turns on and off the ultrasonic emission according to the following code:

int inPin9 = 9;
int inPin10 = 10;
int outputPin = 12;
int valUS, valMoteur1, valMoteur2;

void setup()
{
  Serial.begin(9600);
  pinMode(outputPin, OUTPUT);
  pinMode(inPin9, INPUT);
  pinMode(inPin10, INPUT);
}

void loop()
{
  valMoteur1 = digitalRead(inPin9);
  valMoteur2 = digitalRead(inPin10);
  if(valMoteur1 == HIGH || valMoteur2 == HIGH) {
    digitalWrite(outputPin, LOW);
  }
  if (Serial.available()) {
    valUS = Serial.read();
    if (valUS == 'A' || valUS == 'C') {
      digitalWrite(outputPin, HIGH);
    }
    if (valUS == 'B' || valUS == 'D') {
      digitalWrite(outputPin, LOW);
    }
  }
}



The gray wire is turning pin 10 HIGH or LOW according to the state of the motors (see previous post Codes - 1). The letters 'A', 'B', 'C', and 'D' are sent wireless from another, fixed, XBee attached to another Arduino board connected to a PC according to the following code:


int inUS1 = 5;
int inUS2 = 6;
int val1, val2;
unsigned long t, th1, th2;
int n = 1;
int frontiere = 1;

void setup()
{
  Serial.begin(9600);
  pinMode(inUS1, INPUT);
  pinMode(inUS2, INPUT);
}

void loop()
    {
     frontiere = 1;
  val1 = digitalRead(inUS1);
   val2 = digitalRead(inUS2);
   if (n < 0){
  Serial.print('A');
  t = micros();
  while (val1 == LOW){
    val1 = digitalRead(inUS1);
    th1 = micros() - t;
    if(th1 > 17000)
    {
  val1 = HIGH;
  frontiere = -1;
    }
    }
    Serial.print(th1);
  Serial.print(10, BYTE);
    Serial.print('B');  
  Serial.print(10, BYTE);
  }else{
  Serial.print('C');
  t = micros();
  while (val2 == LOW){
    val2 = digitalRead(inUS2);
    th2 = micros() - t;
    if(th2 > 17000)
    {
    val2 = HIGH;
    frontiere = -1;
    }
    }
    Serial.print(th2);
  Serial.print(10, BYTE);
  Serial.print('D');
  Serial.print(10, BYTE);
   }
   n = n * (-1);
   Serial.print('E');
   Serial.print(frontiere);
  Serial.print(10, BYTE);
  delay(100);
  }



The two ultrasonic receivers are connected to pin 5 and 6, they turn HIGH anytime a 40 kHz ultrasonic pulse is received the "while()" functions measure the time it takes for the US pulse to get from the transmitter to the receiver (we suppose obviously that the radio order 'A' or 'C' to turn on the emitter travels at infinite at infinite speed). "frontiere" is +1 when the mobile is moving and -1 when it is stopped.

Aucun commentaire:

Enregistrer un commentaire