#NodeMCU new ping arduino

Dernière modification : 2016/12/30 16:07

New ping arduino library :
Arduino Playground - NewPing Library


#include <NewPing.h>

#define TRIGGER_PIN D6
#define ECHO_PIN D5
#define MAX_DISTANCE 400

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); 

const int ledPin =  D8;      
int ledState = LOW;             
unsigned long previousMillis = 0;       
const long interval = 300;           /

void setup() {
    Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() {

  delay(50);
  unsigned int distance = sonar.ping_cm();
  unsigned long currentMillis = millis();


  if ((distance >= 2) && (distance <= 300))  {
    Serial.print(distance);
    Serial.println(" cm");
    if (distance <= 15) {


      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        if (ledState == LOW) {
          ledState = HIGH;
        } else {
          ledState = LOW;
        }
        digitalWrite(ledPin, ledState);
      }
    }
  }
}




===

version condensée (à coupler avec gestion des moteurs)




#include <NewPing.h>
#define TRIGGER_PIN  D7  
#define ECHO_PIN     D8  
#define MAX_DISTANCE 200 
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
  Serial.begin(115200); 
}
void loop() {
  delay(50);                    
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm());
  Serial.println("cm");
}


===

motorisation 4wd