1 (edited by bil87777 2020-04-06 18:24:44)

Topic: mesure et affichage distance et commande servo moteur

Ce mini projet mesure la distance et commande par un servo moteur l'ouverture d'une poubelle
code
#define REMOTEXY_MODE__ESP8266WIFI_LIB_POINT
#include <ESP8266WiFi.h>
#include <Servo.h>
#include <RemoteXY.h>

// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "RemoteXY"
#define REMOTEXY_WIFI_PASSWORD ""
#define REMOTEXY_SERVER_PORT 6377
Servo servo;

#define TRIGGER 5
#define ECHO    4

// NodeMCU Pin D1 > TRIGGER | Pin D2 > ECHO

// RemoteXY configurate 
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,0,0,14,0,32,0,10,13,0,
  66,132,13,17,36,28,2,24,65,4,
  73,8,9,9,65,2,73,45,9,9,
  67,5,65,25,24,11,2,26,11 };
 
// this structure defines all the variables and events of your control interface
struct {

    // output variables
  int8_t level_1; // =0..100 level position
  uint8_t led_1_r; // =0..255 LED Red brightness
  uint8_t led_2_g; // =0..255 LED Green brightness
  char text_1[11];  // string UTF8 end zero

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////



void setup()
{
  RemoteXY_Init ();
 
 
  // TODO you setup code
   Serial.begin (9600);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(BUILTIN_LED, OUTPUT);

  servo.attach(2);  //D4
  servo.write(0);
  delay(2000);
 
}

void loop()
{
  RemoteXY_Handler ();
 
  long duration, distance;
  digitalWrite(TRIGGER, LOW); 
  delayMicroseconds(2);
 
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
 
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;
 
  Serial.print(distance);
  Serial.println("Centimeter:");
  delay(1000);


 
  if (distance<15)
  {
     RemoteXY.led_1_r = 255;   // then turn on red light
     RemoteXY.led_2_g = 0;
     servo.write(90);
     RemoteXY.level_1 =45;
     dtostrf(distance, 0, 0, RemoteXY.text_1);
  }
  else
 
  {
    RemoteXY.led_1_r = 0;     // turn off red
    RemoteXY.led_2_g = 255;
    servo.write(0);
    RemoteXY.level_1 = 0;
    dtostrf(distance, 0, 0, RemoteXY.text_1);
   
  // TODO you loop code
  // use the RemoteXY structure for data transfer

  }

2

Re: mesure et affichage distance et commande servo moteur

Je pense que ce serait mieux si distance était de type float au lieu de long, et si tu supprimais le delay