arduino程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #include <Otto.h> Otto Otto; #define LeftLeg 2 // left leg pin, servo[0] #define RightLeg 3 // right leg pin, servo[1] #define LeftFoot 4 // left foot pin, servo[2] #define RightFoot 5 // right foot pin, servo[3] #define Buzzer 13 //buzzer pin long ultrasound_distance_simple() { long duration, distance; digitalWrite(9,LOW); delayMicroseconds(2); digitalWrite(9, HIGH); delayMicroseconds(10); digitalWrite(9, LOW); duration = pulseIn(10, HIGH); distance = duration/58; return distance; } void setup() { Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer); Otto.home(); pinMode(9, OUTPUT); pinMode(10, INPUT); } void loop() { if (ultrasound_distance_simple() < 5) { Otto.walk(1,1000,-1); // BACKWARD } delay(2*1000); } |
範例二、
arduino程式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include <Otto.h> Otto Otto; #define LeftLeg 2 // left leg pin, servo[0] #define RightLeg 3 // right leg pin, servo[1] #define LeftFoot 4 // left foot pin, servo[2] #define RightFoot 5 // right foot pin, servo[3] #define Buzzer 13 //buzzer pin long ultrasound_distance_simple() { long duration, distance; digitalWrite(9,LOW); delayMicroseconds(2); digitalWrite(9, HIGH); delayMicroseconds(10); digitalWrite(9, LOW); duration = pulseIn(10, HIGH); distance = duration/58; return distance; } void setup() { Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer); Otto.home(); pinMode(9, OUTPUT); pinMode(10, INPUT); } void loop() { if (ultrasound_distance_simple() < 15) { Otto.playGesture(OttoConfused); for (int count=0 ; count<2 ; count++) { Otto.walk(1,1000,-1); // BACKWARD } for (int count=0 ; count<4 ; count++) { Otto.turn(1,1000,1); // LEFT } } Otto.walk(1,1000,1); // FORWARD } |
沒有留言:
張貼留言