Étape 2: Arduino IDE partie
Maintenant nous pouvons passer à la partie codante de notre projet.
Nous avons IDE Arduino côté et traitement. La dernière partie nous aider à lire les informations de notre microcontrôleur et l’écrire dans un fichier.
Alors je suppose que vous savez déjà comment configurer votre IDE Arduino. Il suffit pour s’assurer de choisir le bon conseil et le port (dans le menu Outils -> Conseil d’administration -> Arduino Uno (dans mon cas) et outils -> Port -> COM4 (vous devez choisir votre Arduino est connecté au port))
Ensuite, créez un nouvel élan de croquis codage:)
#define trigPin 12 #define echoPin 13 #define blue 11 #define red 10 void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(red, OUTPUT); pinMode(blue, OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); /* PulseIn waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds or 0 if no complete pulse was received within the timeout. The speed of sound is 340 m/s or 29 microseconds per centimeter. The ping travels out and back, so to find the distance of the object we take half of the distance traveled. */ distance = (duration/2) / 29; if (distance < 100) { digitalWrite(blue,LOW); digitalWrite(red,HIGH); } else { digitalWrite(red,LOW); digitalWrite(blue,HIGH); } Serial.println(distance); delay(500); }