forked from 3DPrintingGeek/Animatronic_Raven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_raven_mod.ino
132 lines (112 loc) · 2.95 KB
/
simple_raven_mod.ino
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <Servo.h>
// Pin definitions
#define SERVO_PIN 3
#define TRIGGER_PIN 2
#define LED_PIN 13
#define EYE1_PIN 7
#define EYE2_PIN 8
#define SOUND_PIN 4
// Constants
#define REFRESH_PERIOD_MS 20
#define MAX_POS 150
#define MIN_POS 30
#define MOVE_INTERVAL 5
#define SERVO_SPEED 1
// Global variables
Servo myservo;
int pos = 0; // Current servo position
volatile bool active = false;
// Function prototypes
void triggerIsr();
void birdMove(int dest);
void maybeTwitch();
void maybeBlink();
void blink();
void makeSound();
void setup()
{
// Initialize pins
pinMode(SOUND_PIN, OUTPUT);
digitalWrite(SOUND_PIN, LOW);
pinMode(EYE1_PIN, OUTPUT);
pinMode(EYE2_PIN, OUTPUT);
pinMode(TRIGGER_PIN, INPUT_PULLUP);
// Attach interrupt for trigger
attachInterrupt(digitalPinToInterrupt(TRIGGER_PIN), triggerIsr, RISING);
// Attach servo
myservo.attach(SERVO_PIN);
// Initialize random seed
randomSeed(analogRead(0));
// Serial for debugging (optional)
Serial.begin(9600);
}
void loop()
{
if (active) {
for (int i = 0; i < 10; i++) {
int target = random(MIN_POS, MAX_POS);
// Perform bird actions
birdMove(target);
maybeBlink();
maybeTwitch();
makeSound();
delay(500);
}
// Reset eyes and active state
digitalWrite(EYE1_PIN, LOW);
digitalWrite(EYE2_PIN, LOW);
active = false;
}
}
// Interrupt Service Routine for trigger
void triggerIsr() {
active = true;
}
// Move the bird (servo) to a target position
void birdMove(int dest) {
int start = myservo.read();
int direction = (dest > start) ? 1 : -1;
for (pos = start; pos != dest; pos += direction * SERVO_SPEED) {
myservo.write(pos);
delay(MOVE_INTERVAL);
}
myservo.write(dest); // Ensure final position is reached
}
// Random twitching behavior
void maybeTwitch() {
if (random(3) == 0) { // 1 in 3 chance to twitch
int timesTwitch = random(3, 6);
for (int i = 0; i < timesTwitch; i++) {
int currentPos = myservo.read();
int twitchAmount = random(-10, 11);
int target = constrain(currentPos + twitchAmount, MIN_POS, MAX_POS);
birdMove(target);
}
}
}
// Random blinking behavior
void maybeBlink() {
if (random(2) == 0) { // 50% chance to blink
blink();
if (random(5) == 0) { // 20% chance to blink twice
delay(200);
blink();
}
}
}
// Blink eyes
void blink() {
digitalWrite(EYE1_PIN, HIGH);
digitalWrite(EYE2_PIN, HIGH);
delay(100);
digitalWrite(EYE1_PIN, LOW);
digitalWrite(EYE2_PIN, LOW);
}
// Make sound
void makeSound() {
if (random(10) == 0) { // 10% chance to make sound
digitalWrite(SOUND_PIN, HIGH);
delay(random(100, 500)); // Random duration sound
digitalWrite(SOUND_PIN, LOW);
}
}