Stap 4: De Arduino Robo PEZ schets
Ik ben het plaatsen van deze code in het publieke domein.
*/
-------------------------------
#include < Servo.h >
#include < Ultrasonic.h >
#define TRIGGER_PIN 12 / / ultrasone sensor TRIGGER
#define ECHO_PIN 13 / / ultrasone sensor ECHO
#define afstand 20 / / in Centimeters
#define wachten 5000 / / in milliseconden dus dit 5 seconden is.
#define SERVO_PIN 9
#define CLOSE_POS 90 / / servo in nauwe/positie
#define OPEN_POS 180 / / servo in OPEN/rug positie
Ultrasonic ultrasone (TRIGGER_PIN, ECHO_PIN);
Servo myservo; servo-object om te bepalen van de servo PEZ maken
VOID Setup
{
Serial.begin(9600);
myservo.attach(SERVO_PIN); hecht de servo om pin
myservo.write(CLOSE_POS); servo wil sluiten/UP positie
}
void loop
{
float cmMsec, inMsec;
lange microsec = ultrasonic.timing();
cmMsec = ultrasonic.convert (microsec, Ultrasonic::CM);
inMsec = ultrasonic.convert (microsec, Ultrasonic::IN);
Dit is voor foutopsporing
Serial.Print ("MS:");
Serial.Print(Microsec);
Serial.Print (", CM: ');
Serial.Print(cmMsec);
Serial.Print (", IN:");
Serial.println(inMsec);
delay(1000);
Als (cmMsec > = afstand) / / als niemand binnen afstand van PEZ mond dicht
{
myservo.write(CLOSE_POS); vertellen van de servo naar CLOSE/omhoog
delay(15); wacht 15ms voor de servo te bereiken de positie
}
Als (cmMsec < = afstand) / / als iemand op afstand, open mond
{
myservo.write(OPEN_POS); vertellen van de servo naar OPEN/rug
delay(Waiting); wacht ergens toe grijpen pellet van het snoep
}
}