I'm trying to build a simple motion tracking turret using 3 servos and an ESP32Cam. Im also using an Arduino IDE connected to the servos and camera.
I receive this error message when I compile the code:
"Arduino: 1.8.15 (Windows 10), Board: "AI Thinker ESP32-CAM, 240MHz (WiFi/BT), QIO, 80MHz"
esp32:10:1: error: 'ESP32Servo' does not name a type
ESP32Servo panServo(panPin);
^
esp32:11:1: error: 'ESP32Servo' does not name a type
ESP32Servo tiltServo(tiltPin);
^
C:\Users\emman\Documents\Arduino\esp32\esp32.ino: In function 'void setup()':
esp32:23:3: error: 'panServo' was not declared in this scope
panServo.attach(panPin);
^
esp32:24:3: error: 'tiltServo' was not declared in this scope
tiltServo.attach(tiltPin);
^
C:\Users\emman\Documents\Arduino\esp32\esp32.ino: In function 'void loop()':
esp32:45:9: error: 'motionY' was not declared in this scope
if (motionY > 100) {
^
esp32:46:7: error: 'panServo' was not declared in this scope
panServo.write(panPos);
^
esp32:48:7: error: 'panServo' was not declared in this scope
panServo.write(0);
^
esp32:52:5: error: 'tiltServo' was not declared in this scope
tiltServo.write(tiltPos);
^
C:\Users\emman\Documents\Arduino\esp32\esp32.ino: In function 'bool detectMotion()':
esp32:81:76: error: 'esp_camera_fb_to_grayscale' was not declared in this scope
esp_camera_fb_to_grayscale(fb->buf, grayscaleImage, fb->width, fb->height);
^
C:\Users\emman\Documents\Arduino\esp32\esp32.ino: In function 'void stopServos()':
esp32:116:3: error: 'panServo' was not declared in this scope
panServo.write(0);
^
esp32:117:3: error: 'tiltServo' was not declared in this scope
tiltServo.write(0);
^
exit status 1
'ESP32Servo' does not name a type
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
"
There is an error with the ESP32Servo library. I've reinstalled it and checked if the version is compatible. It's v0.13 and my Arduino IDE is version v1.8.15.
Here is my code:
#include <Wire.h>
#include "esp_camera.h"
#include <ESP32Servo.h>
// Define the servo pins
const int panPin = 2;
const int tiltPin = 3;
// Create the servo objects
ESP32Servo panServo(panPin);
ESP32Servo tiltServo(tiltPin);
// Initialize the servos
void setup() {
Serial.begin(9600);
Wire.begin();
// Set the pins as outputs
pinMode(panPin, OUTPUT);
pinMode(tiltPin, OUTPUT);
// Attach the servos
panServo.attach(panPin);
tiltServo.attach(tiltPin);
// Initialize camera
camera_config_t config;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x", err);
while (true);
}
}
// The main loop
void loop() {
// Check if there is any motion
if (detectMotion()) {
// Move servos so that the camera is centered on the movement
int panPos = 90;
int tiltPos = 90;
// Determine the direction of motion
// If the motionY is greater than a threshold, then the pan servo should be adjusted to the right
if (motionY > 100) {
panServo.write(panPos);
} else {
panServo.write(0);
}
// Center the servos
tiltServo.write(tiltPos);
// Pull the trigger
//servo3.write(90);
delay(100);
//servo3.write(0);
} else {
// Stop the servos
stopServos();
}
// Delay for a few milliseconds
delay(100);
}
// Function to detect motion
bool detectMotion() {
// Capture an image
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return false;
}
// Delay for a few milliseconds to let the camera stabilize
delay(10);
// Convert the image data to grayscale
uint8_t *grayscaleImage = new uint8_t[fb->width * fb->height];
esp_camera_fb_to_grayscale(fb->buf, grayscaleImage, fb->width, fb->height);
// Find the center of the image
int centerX = fb->width / 2;
int centerY = fb->height / 2;
// Determine the direction of motion
int motionY = 0;
for (int i = 0; i < fb->width; i++) {
motionY += grayscaleImage[centerX * fb->height + i];
}
// Free the grayscale image
delete[] grayscaleImage;
esp_camera_fb_return(fb);
// If the motionY is greater than a threshold, then there is motion
return motionY > 100;
}
// Adjust servos so that the camera is centered on the movement
void adjustServos() {
// Check if the trigger should be pulled
//if (motionDetected) {
// Pull the trigger
servo3.write(90);
//} else {
// Release the trigger
servo3.write(0);
//}
}
// Stop the servos
void stopServos() {
panServo.write(0);
tiltServo.write(0);
Is there anything I am doing wrong?
Any tips or help is much appreciated, thank you!