ENIB 2020 : Coucoumatron : Différence entre versions
(→Code) |
(→Code) |
||
Ligne 17 : | Ligne 17 : | ||
<code> | <code> | ||
− | / | + | /* |
− | |||
* The circuit: | * The circuit: | ||
* * Module HR-SC04 (four pins) or PING))) (and other with | * * Module HR-SC04 (four pins) or PING))) (and other with | ||
Ligne 46 : | Ligne 45 : | ||
*/ | */ | ||
− | #include <Ultrasonic.h> | + | #include <Ultrasonic.h> |
− | #include <Servo.h> | + | #include <Servo.h> |
− | /* | + | /* |
* Pass as a parameter the trigger and echo pin, respectively, | * Pass as a parameter the trigger and echo pin, respectively, | ||
* or only the signal pin (for sensors 3 pins), like: | * or only the signal pin (for sensors 3 pins), like: | ||
* Ultrasonic ultrasonic(13); | * Ultrasonic ultrasonic(13); | ||
*/ | */ | ||
− | Ultrasonic ultrasonic(12, 13); | + | Ultrasonic ultrasonic(12, 13); |
− | Servo arm; | + | Servo arm; |
− | int distance; | + | int distance; |
− | + | ||
− | void setup() { | + | void setup() { |
− | + | Serial.begin(9600); | |
− | + | arm.attach(5); | |
− | + | arm.write(10); | |
− | } | + | } |
− | + | ||
− | void loop() { | + | void loop() { |
− | + | // Pass INC as a parameter to get the distance in inches | |
− | + | distance = ultrasonic.read(); | |
− | + | Serial.println(distance); | |
− | + | if (distance < 50) { | |
− | + | Serial.println("Coucou"); | |
− | + | for (int i = 0; i<2; i++) { | |
− | + | arm.write(100); | |
− | + | delay(500); | |
− | + | arm.write(10); | |
− | + | delay(500); | |
− | + | } | |
− | + | } | |
− | + | delay(500); | |
− | + | } | |
− | |||
− | |||
− | } | ||
</code> | </code> | ||
Version du 20 janvier 2020 à 16:16
Sommaire
[masquer]Photo de l’équipe
Que fait ce projet ?
Lorsqu’on fait coucou (on passe sa main devant le capteur) à la boite, elle répond en faisant coucou à son tour.
Liste des composants
- Arduino
- Capteur de distance Ultrasons
- Servomoteur
- Des fils de connexion
- Du carton ou d'autres matériaux pour réaliser la boite et le bras+main
Code
/*
* The circuit:
* * Module HR-SC04 (four pins) or PING))) (and other with
* three pins), attached to digital pins as follows:
* --------------------- --------------------
* | HC-SC04 | Arduino | | 3 pins | Arduino |
* --------------------- --------------------
* | Vcc | 5V | | Vcc | 5V |
* | Trig | 12 | OR | SIG | 13 |
* | Echo | 13 | | Gnd | GND |
* | Gnd | GND | --------------------
* ---------------------
*
* * A servo on pin 5
*
* created 3 Apr 2014
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
* modified 23 Jan 2017
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
* modified 03 Mar 2017
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
* modified 11 Jun 2018
* by Erick Simões (github: @ErickSimoes | twitter: @AloErickSimoes)
* modified 20 Jan 2020
* by Nils Van Zuijlen (github: @nils-van-zuijlen)
* This example code is released into the MIT License.
*/
#include <Ultrasonic.h>
#include <Servo.h>
/*
* Pass as a parameter the trigger and echo pin, respectively,
* or only the signal pin (for sensors 3 pins), like:
* Ultrasonic ultrasonic(13);
*/
Ultrasonic ultrasonic(12, 13);
Servo arm;
int distance;
void setup() {
Serial.begin(9600);
arm.attach(5);
arm.write(10);
}
void loop() {
// Pass INC as a parameter to get the distance in inches
distance = ultrasonic.read();
Serial.println(distance);
if (distance < 50) {
Serial.println("Coucou");
for (int i = 0; i<2; i++) {
arm.write(100);
delay(500);
arm.write(10);
delay(500);
}
}
delay(500);
}