Étape 4: L’esquisse de la PEZ Arduino Robo
Je suis placer ce code dans le domaine public.
*/
-------------------------------
#include < Servo.h >
#include < Ultrasonic.h >
#define TRIGGER_PIN 12 / / capteur à ultrasons TRIGGER
#define ECHO_PIN 13 / / ECHO du capteur à ultrasons
#define DISTANCE 20 / / en centimètres
#define attend 5000 / / en millisecondes alors il s’agit de 5 secondes.
#define SERVO_PIN 9
#define CLOSE_POS 90 / / servo en gros/position
#define OPEN_POS 180 / / position du servo dans OPEN/dos
Ultrason ultrasonique (TRIGGER_PIN, ECHO_PIN) ;
Servo myservo ; créer l’objet de servo pour contrôler le servo PEZ
void setup()
{
Serial.Begin(9600) ;
myservo.Attach(SERVO_PIN) ; attache le servo pour épingler
myservo.Write(CLOSE_POS) ; mettre servo en position CLOSE/UP
}
void loop()
{
flotteur cmMsec, inMsec ;
microsec long = ultrasonic.timing() ;
cmMsec = ultrasonic.convert (microsec, Ultrasonic::CM) ;
inMsec = ultrasonic.convert (microsec, Ultrasonic::IN) ;
Il s’agit pour le débogage
Serial.Print ("MS:") ;
Serial.Print(MICROSEC) ;
Serial.Print (", CM:") ;
Serial.Print(cmMsec) ;
Serial.Print (", IN:") ;
Serial.println(inMsec) ;
Delay(1000) ;
Si (cmMsec > = DISTANCE) / / si la personne à distance de PEZ fermer la bouche
{
myservo.Write(CLOSE_POS) ; dire de servo pour aller à fermer/UP
Delay(15) ; attend 15 ms pour le servo atteindre la position
}
Si (cmMsec < = DISTANCE) / / si quelqu'un est à distance, ouvrir la bouche
{
myservo.Write(OPEN_POS) ; dire de servo pour aller à ouvert/arrière
Delay(Waiting) ; attend un certain temps pour permettre l’accaparement des granulés de bonbons
}
}