Bonjour, je réalise actuellement un robot ramasseur mais le capteur détecte sur une trop grande distance , comment modifier la distance de de détection hcsr04 ( j'aimerais 11 cm grand maximum ) voila mon code :
/* Utilisation du capteur Ultrason HC-SR04 pour contrôler un servomoteur*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// définition des broches utilisées
int servo = 9;
int trig = 12;
int echo = 11;
long lecture_echo;
long cm;
int val;
long lastCm = 0;
#include <Stepper.h>
const int enable = 30;
int nombreDePas = 48*64;
Stepper monMoteur(nombreDePas,8,13,10,6);
void setup()
{
myservo.attach(servo);
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
pinMode(echo, INPUT);
Serial.begin(9600);
monMoteur.setSpeed(9);
}
void loop()
{
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
lecture_echo = pulseIn(echo, HIGH);
cm = lecture_echo / 58;
int diff = int(abs(lastCm - cm)); // detection de grand saut
if(diff < 20){ // seulement si diff est faible
val = min(cm,179);
if( val< 179){
Serial.print(diff);
Serial.print(" : ");
Serial.println(val);
myservo.write(val);
}
}
delay(10);
lastCm = cm;
monMoteur.step(100);
monMoteur.step(-200);
}