Pesquisar este blog

domingo, 23 de maio de 2010

Tank Bluetooth

Chegou essa semana o Bluetooth Mate, com ele foi possivel controlar o meu Tank usando o celular =D*

abaixo algumas imagens








Código fonte(esta versão esta sendo utilizado o ping sensor para realizar a verificação da distancia e enviar para o celular):

#include

AF_DCMotor motor1(3, MOTOR12_64KHZ); // create motor #3, 64KHz pwm
AF_DCMotor motor2(4, MOTOR12_64KHZ); // create motor #3, 64KHz pwm

int var=0;
const int pingPin = 15;


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
 
  motor1.setSpeed(255);
  motor2.setSpeed(255);    
}

void loop() {
 
 
  if(Serial.available()>0){
      var=Serial.read();
      if(var=='w')
        frente();
      else
        if(var=='a')
          esquerda();
        else
          if(var=='s')
            re();
          else
            if(var=='d')
              direita();
     
  }
  else{
    parar();
    long duration, inches, cm;
 
   pinMode(pingPin, OUTPUT);
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(5);
   digitalWrite(pingPin, LOW);
   pinMode(pingPin, INPUT);
   duration = pulseIn(pingPin, HIGH);

   // convert the time into a distance
   inches = microsecondsToInches(duration);
   cm = microsecondsToCentimeters(duration);

   Serial.print(inches);
   Serial.print("in, ");
   Serial.print(cm);
   Serial.print("cm");
   Serial.println();
  }
 
  delay(115);
}


void re(){
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor1.run(FORWARD);     
  motor2.run(FORWARD);     
}

void frente(){
  motor1.setSpeed(255);
  motor2.setSpeed(255);
  motor1.run(BACKWARD);     
  motor2.run(BACKWARD);     
}

void parar(){
 
  motor1.run(RELEASE);     
  motor2.run(RELEASE);     
}


void esquerda(){
  motor1.setSpeed(230);
  motor2.setSpeed(220);
  motor1.run(FORWARD);     
  motor2.run(BACKWARD);  
 
}

void direita(){
  motor1.setSpeed(220);
  motor2.setSpeed(230);
  motor1.run(BACKWARD);     
  motor2.run(FORWARD);

}

 long microsecondsToInches(long microseconds)
 {
   // According to Parallax's datasheet for the PING))), there are
   // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
   // second).  This gives the distance travelled by the ping, outbound
   // and return, so we divide by 2 to get the distance of the obstacle.
   // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
   return microseconds / 74 / 2;
 }

 long microsecondsToCentimeters(long microseconds)
 {
   // The speed of sound is 340 m/s or 29 microseconds per centimeter.
   // The ping travels out and back, so to find the distance of the
   // object we take half of the distance travelled.
   return microseconds / 29 / 2;
 }



Um comentário: