#include <Servo.h>
#include <Adafruit_NeoPixel.h>
// Pin definitions
#define BUTTON_1_PIN 2
#define BUTTON_2_PIN 4
#define BUTTON_3_PIN 7 // Left button 1
#define RESET_BUTTON_PIN 8
#define EYE_RED_PIN 3
#define EYE_GREEN_PIN 5
#define EYE_BLUE_PIN 6
#define NEOPIXEL_PIN 11
#define SERVO_POWER_PIN 12 // Servo power control pin
// Neopixel object creation
#define NUM_PIXELS 1
Adafruit_NeoPixel pixels(NUM_PIXELS, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800);
// Servo objects
Servo servo1;
Servo servo3;
// Button state variables
bool button1State = false;
bool button2State = false;
bool button3State = false; // Left button 1
bool resetButtonState = false;
// Servo position constants
#define CLOSED_POSITION 90
#define OPEN_POSITION 180
// Neopixel color variables
#define PIXEL_COLOR_SPEED 10
#define FRIEND_ZONE_DURATION 180000 // 3 minutes
unsigned long friendZoneTimer = 0;
// Variable to track servo state
bool servosOpen = false;
void setup() {
// Initialize servos
servo1.attach(9);
servo3.attach(10);
// Initialize Neopixel
pixels.begin();
pixels.setBrightness(50); // Adjust brightness if needed
// Initialize button pins
pinMode(BUTTON_1_PIN, INPUT_PULLUP);
pinMode(BUTTON_2_PIN, INPUT_PULLUP);
pinMode(BUTTON_3_PIN, INPUT_PULLUP); // Left button 1
pinMode(RESET_BUTTON_PIN, INPUT_PULLUP);
pinMode(SERVO_POWER_PIN, OUTPUT);
// Initialize servo power control
digitalWrite(SERVO_POWER_PIN, HIGH); // Turn on servo power by default
// Set initial positions for servos and eye colors
servo1.write(CLOSED_POSITION);
servo3.write(CLOSED_POSITION);
analogWrite(EYE_RED_PIN, 255);
analogWrite(EYE_GREEN_PIN, 255);
analogWrite(EYE_BLUE_PIN, 0); // Yellow
// Set initial Neopixel color to yellow
pixels.setPixelColor(0, pixels.Color(255, 255, 0)); // Yellow
pixels.show();
}
void loop() {
// Read button states
button1State = digitalRead(BUTTON_1_PIN);
button2State = digitalRead(BUTTON_2_PIN);
button3State = digitalRead(BUTTON_3_PIN); // Left button 1
resetButtonState = digitalRead(RESET_BUTTON_PIN);
// State 1: Crowded
if (button1State == LOW) {
servo1.write(CLOSED_POSITION);
servo3.write(CLOSED_POSITION);
analogWrite(EYE_RED_PIN, 255); // Red
analogWrite(EYE_GREEN_PIN, 0);
analogWrite(EYE_BLUE_PIN, 0);
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red
pixels.show();
servosOpen = false;
}
// State 2: Space available
else if (button2State == LOW) {
servo1.write(-OPEN_POSITION);
servo3.write(OPEN_POSITION);
analogWrite(EYE_RED_PIN, 0);
analogWrite(EYE_GREEN_PIN, 255); // Green
analogWrite(EYE_BLUE_PIN, 0);
pixels.setPixelColor(0, pixels.Color(0, 255, 0)); // Green
pixels.show();
servosOpen = true;
}
// State 3: Friend in range
else if (button3State == LOW) { // Left button 1
// Blue color for Neopixel
analogWrite(EYE_RED_PIN, 0);
analogWrite(EYE_GREEN_PIN, 0);
analogWrite(EYE_BLUE_PIN, 255); // Blue
pixels.setPixelColor(0, pixels.Color(0, 0, 255)); // Blue
pixels.show();
// Repeat open and close action 20 times
for (int i = 0; i < 20; i++) {
if (servosOpen) {
servo1.write(CLOSED_POSITION);
servo3.write(CLOSED_POSITION);
servosOpen = false;
} else {
servo1.write(-OPEN_POSITION);
servo3.write(OPEN_POSITION);
servosOpen = true;
}
delay(1000); // Short delay between flaps
}
// Rotate through all possible colors for Neopixel
for (int i = 0; i < 256; i++) {
pixels.setPixelColor(0, pixels.Color(i, 255 - i, 0)); // Varying RGB values for different colors
pixels.show();
delay(100); // Delay between color changes
}
}
// Open and close servos until reset button is pressed
else if (resetButtonState == LOW) {
servo1.write(CLOSED_POSITION);
servo3.write(CLOSED_POSITION);
// Turn off servo power
digitalWrite(SERVO_POWER_PIN, LOW);
// Wait for a short time to ensure proper shutdown
delay(100);
}
}