22 octobre 2013

ultrason , capteur de contact et moteur cc (video sur le blog onglet en haut)

int pwm_a = 9;
int pwm_b = 10;
int dir_a = 7;
int dir_b = 8;
#define BUTTON 4
#define BUT 3
int val = 0;
int vall =0;
#define trigPin 6
 #define echoPin 5

void setup()
{
  Serial.begin (9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
  pinMode(pwm_a, OUTPUT);
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT);
  pinMode(BUTTON,INPUT);// bouton gauche
  pinMode(BUT,INPUT);
  analogWrite(pwm_a,127);
  analogWrite(pwm_b,127);
  }
  void loop()
  {
   long duration, distance;
   digitalWrite(trigPin, LOW); 
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
 //  delayMicroseconds(1000);
   delayMicroseconds(10); //
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration/2) / 29.1;
  val  = digitalRead(BUTTON);
  vall = digitalRead(BUT);
  if (val == HIGH){
    digitalWrite(dir_a,LOW);
    digitalWrite(dir_b,LOW);
 analogWrite(pwm_a,127);
  analogWrite(pwm_b,127);
  }
  else{
    digitalWrite(dir_b,HIGH);
    digitalWrite(dir_a,HIGH);
    delay(250);
    digitalWrite(dir_b,LOW);
    digitalWrite(dir_a, HIGH);
    delay(500);
   
  }
  if (vall == HIGH){
    digitalWrite(dir_a,LOW);
    digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127);
  analogWrite(pwm_b,127); 
}
  else{
    digitalWrite(dir_b,HIGH);
    digitalWrite(dir_a,HIGH);
    delay(250);
    digitalWrite(dir_b,HIGH);
    digitalWrite(dir_a,LOW);
    delay(500);
  }  
   if (distance < 8) { 
     digitalWrite(dir_b,HIGH);
    digitalWrite(dir_a,HIGH);
    delay(500);
    digitalWrite(dir_b,HIGH);
    digitalWrite(dir_a,LOW);
    delay(2000);
 }
   else {
    digitalWrite(dir_a,LOW);
    digitalWrite(dir_b,LOW);
analogWrite(pwm_a,127);
  analogWrite(pwm_b,127);  
  
 }
  }
 

Aucun commentaire:

Enregistrer un commentaire