Go Down

Topic: Fonctionnement d'un ultrason (Read 283 times) previous topic - next topic

Skyangels

Bonjour à tous,

Alors voilà ce que donne mon programme modifié :

Code: [Select]


#include <Servo.h>


Servo scanservo;   //Ping Sensor Servo

int pos = 0;    // variable pour enregistrer la position des servos
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *motor4 = AFMS.getMotor(4);

const int pingPin = 12;
const int scanservopin = 28;
const int distancelimit = 20; //in cm

void setup() {
 
  Serial.begin(9600);
  AFMS.begin();
  motor1->setSpeed(150);
  motor2->setSpeed(150);
  motor3->setSpeed(150);
  motor4->setSpeed(150);
  scanservo.attach(scanservopin);

void loop() {

  go();
int jarak = ping();
  if (jarak<distancelimit){
    stopp();
    char turndir = scan();
        switch (turndir){
            case 'l':
            turnleft();
            break;

            case 'r':
            turnright();
            break;

            case 's':
            turnaround();
            break;
            }
  }

}

int ping(){

  long duration, distance;

  //Send Pulse
  pinMode(pingPin,OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  //Read Echo
  pinMode(pingPin,INPUT);
  duration = pulseIn(pingPin, HIGH);
  // convert the time into a distance
  distance = (duration/2) / 29.1;
 
  return round(distance);

}

void go(){
motor1->run(FORWARD);
motor2->run(FORWARD);
motor3->run(FORWARD);
motor4->run(FORWARD);
 
}
void turnleft(){
motor1->run(BACKWARD);
motor2->run(BACKWARD);
motor3->run(FORWARD);
motor4->run(FORWARD);
delay(1000);
motor1->run(RELEASE);
motor2->run(RELEASE);
motor3->run(RELEASE);
motor4->run(RELEASE);
}

void turnright(){
motor1->run(FORWARD);
motor2->run(FORWARD);
motor3->run(BACKWARD);
motor4->run(BACKWARD);
delay(1000);
motor1->run(RELEASE);
motor2->run(RELEASE);
motor3->run(RELEASE);
motor4->run(RELEASE);
}
void turnaround(){

motor1->run(BACKWARD);
motor2->run(BACKWARD);
motor3->run(FORWARD);
motor4->run(FORWARD);
delay(1000);
motor1->run(RELEASE);
motor2->run(RELEASE);
motor3->run(RELEASE);
motor4->run(RELEASE);
}
void stopp(){
motor1->run(RELEASE);
motor2->run(RELEASE);
motor3->run(RELEASE);
motor4->run(RELEASE);
delay(1000);
}

char scan(){

  int leftscanval, centerscanval, rightscanval;
  char choice;

  //Look left
  scanservo.write(178);
  delay(300);
  leftscanval = ping();

  //Look right
  scanservo.write(4);
  delay(1000);
  rightscanval = ping();

  //center scan servo
  scanservo.write(68);
  delay(500);
  centerscanval = ping();
 

  if ((leftscanval>rightscanval) && (leftscanval>centerscanval)){
  choice = 'l';
  }

  else if ((rightscanval>leftscanval) && (rightscanval>centerscanval)){
  choice = 'r';
  }

  else{
  choice = 's';
  }

  return choice;
}


Et ça ne fonctionne pas. Quelqu'un à une idée ?

kamill

Bonjour,

Qu'est ce qui ne fonctionne pas.
Ajoute un Serial.println(distance); pour le debug

Skyangels

Bonjour Kamill,

quand je fais :

Serial.println(distancelimit)

il n'y a rien dans le serial monitor.

Est-ce que je peux t'envoyer mon programme entier par email pour que tu jettes un oeil si tu as le temps ?


kamill

Bonjour,

Tu as mis l'initialisation de Serial?
Dans le setup(), il faut mettre Serial.begin(9600); si ton moniteur serie est à 9600 bds

Mets ton programme complet ici.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy