Ceci est une ancienne révision du document !
So, we’ll carry on from last month and add the sonar sensor to the circuit. Again, these are pretty cheap, but also pretty powerful, and, as we’ll see, pretty accurate too. The sensor I have has five pins. Some have only four, as, even with mine, only four are used. One pin is for 5V with one for GND. The other two are for echo and trigger. Mine is wired up as I’ve indicated in the diagram. With the code from last month (http://pastebin.com/UYQe58xB), we can now detect a distance and display it on the colour screen. You can see a video of it in action here: https://www.youtube.com/watch?v=Tbg-qyeJM6U. As you can see from the ruler (in the video) it’s pretty accurate! Let me run through a couple of lines of code with notes and thoughts: #include <NewPing.h> Some of the echo and trigger stuff can be tricky, but with the NewPing library, things are a bit easier: https://code.google.com/p/arduino-new-ping/ #define TRIGGER_PIN 5 #define ECHO_PIN 3 #define MAX_DISTANCE 200
Donc, nous allons poursuivre l'article du mois dernier en ajoutant le sonar au circuit.
A nouveau, ils sont bon marché mais assez puissants et aussi, comme nous allons le voir, plutôt précis.
Le capteur que j'ai a 5 picots. Certains n'en ont que quatre, car, même le mien n'en utilise que quatre. Le 5V est sur un picot, la masse sur un autre. Les deux autres sont pour l'écho et le seuil. Le mien est câblé comme sur le schéma.
Avec le code vu le mois dernier (http://pastebin.com/UYQe58xB), nous pouvons maintenant mesurer une distance et l'afficher sur l'écran couleur.
Nous pouvons voir une vidéo du module en pleine action ici : https://www.youtube.com/watch?v=Tbg-qyeJM6U. Comme vous pouvez le constater avec la règle (dans la vidéo), c'est plutôt précis.
The three defines are pretty self-explanatory. Three and five are the Arduino pins, and the maximum distance is in centimeters. Some estimations of these units have them hitting a target over 400cm away! In the main loop(): Make the integer uS become equal to the sonar ping in centimeters. There is also a sonar.ping_in() command for inches. int uS = sonar.ping_cm(); We set the colour (as we did last month) but here we switch fonts: myGLCD.setFont(SevenSegNumFont); Maybe this one caught you out last month:
myGLCD.print(String(uS)+String(“ ”), LEFT, 24); This one took me a bit to figure out. When I was using: myGLCD.print(uS, LEFT, 24); I was getting all kinds of string errors. Turns out it’s (apparently) best to convert the integer to a string before printing it, so the line is now: myGLCD.print(String(uS)+String(“ ”), LEFT, 24); This prints the number in uS (but as a string) to the screen, puts a space after it, and prints it aligned left at line 24. Why a space? Well, when you run it, you’ll see that, without the space, it leaves the old number behind and you can end up going from 13 to 93. It actually means 9 but has only overwritten the 1 from the 13 leaving the three behind. It’s a bad botch, I admit, but that’s why I’m an amateur at this!