Random Stuff We Make!
Published © GPL3+

Face-Tracking Animatronic Dragon Robot – an Artificial Intel

A face-tracking animatronic dragon robot that follows your every move!

BeginnerFull instructions provided1 hour627

Things used in this project

Hardware components

HUSKYLENS AI Camera
DFRobot HUSKYLENS AI Camera
×1
Arduino Mega 2560
Arduino Mega 2560
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit

Fritzing File

Code

Arduino Code

Arduino
#include <HUSKYLENS.h>
#include <Wire.h>
#include <Servo.h>

HUSKYLENS huskylens;
Servo servo;

int lastServoPosition = 90; // Initial position
int threshold = 5; // Set a threshold for position changes

void setup() {
    servo.attach(9); // Connect the servo to pin 9
    servo.write(90);
    Serial.begin(115200);
    Wire.begin();
    while (!huskylens.begin(Wire)) {
        Serial.println(F("HUSKYLENS not connected!"));
        delay(100);
    }
    huskylens.writeAlgorithm(ALGORITHM_OBJECT_TRACKING);
}

void loop() {
    if (!huskylens.request()) return;
    
    for (int i = 0; i < huskylens.countBlocks(); i++){
        HUSKYLENSResult result = huskylens.getBlock(i);
        
        int x_center = result.xCenter;
        int width = result.width;
        int y_center = result.yCenter;
        int height = result.height;
        int ID = result.ID;
        
        Serial.print("Object tracked at X: ");
        Serial.print(x_center);
        Serial.print(", Y: ");
        Serial.print(y_center);
        Serial.print(", Width: ");
        Serial.print(width);
        Serial.print(", Height: ");
        Serial.println(height);
        Serial.print("Tracked ID: ");
        Serial.println(ID);
        delay(100);

        // Map the x-coordinate to the servo range (0-180 degrees)
        int servoPosition = map(x_center, 0, 320, 180, 0);
        
        // Calculate the adjustment needed
        int servoAdjustment = servoPosition - lastServoPosition; 

        // Limit the adjustment to prevent the servo from moving too quickly
        if (servoAdjustment > threshold) {
            servoAdjustment = threshold;
        } else if (servoAdjustment < -threshold) {
            servoAdjustment = -threshold;
        }

        // Adjust the servo position and update the last position
        servo.attach(9);
        servo.write(lastServoPosition + servoAdjustment);
        lastServoPosition += servoAdjustment;

        delay(15); // Add a delay to allow the servo to move to the new position
        //servo.detach();
    }
}

Credits

Random Stuff We Make!
18 projects • 65 followers
We at RSWM! try to bring Fiction to Reality through projects which are Interactive, Creative & way too simpler in terms of making.
Contact

Comments

Please log in or sign up to comment.