#include <Servo.h>
#include <Firmata.h>
#define MAX_SERVOS 5 // Define maximum number of servos
#define TOTAL_PINS 5 // Define the number of pins used for servos
Servo servos[MAX_SERVOS]; // Create an array for servo objects
byte servoPinMap[TOTAL_PINS] = {9, 10, 11, 12, 13}; // Map pins for each finger
byte servoCount = 0; // Count of attached servos
void analogWriteCallback(byte pin, int value) {
// Ensure the pin is mapped and within range
for (int i = 0; i < MAX_SERVOS; #include <Servo.h>
#include <Firmata.h>
#define MAX_SERVOS 5 // Define maximum number of servos
#define TOTAL_PINS 5 // Define the number of pins used for servos
#define MIN_ANGLE 0 // Minimum angle (0 degrees)
#define MAX_ANGLE 180 // Maximum angle (180 degrees)
Servo servos[MAX_SERVOS]; // Create an array for servo objects
byte servoPinMap[TOTAL_PINS] = {9, 10, 11, 12, 13}; // Map pins for each finger
byte servoCount = 0; // Count of attached servos
void analogWriteCallback(byte pin, int value) {
// Ensure the pin is mapped and within range
for (int i = 0; i < MAX_SERVOS; i++) {
if (pin == servoPinMap[i]) {
// Constrain value between 0 and 180 degrees
value = constrain(value, MIN_ANGLE, MAX_ANGLE);
servos[i].write(value); // Write the constrained value to the servo
break;
}
}
}
void systemResetCallback() {
servoCount = 0; // Reset the count of attached servos
}
void setup() {
// Set firmware version
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
// Attach callbacks
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.attach(SYSTEM_RESET, systemResetCallback);
// Begin communication
Firmata.begin(57600);
// Attach each servo to its respective pin
for (int i = 0; i < MAX_SERVOS; i++) {
servos[i].attach(servoPinMap[i]); // Attach each servo to its mapped pin
}
}
void loop() {
// Process incoming commands
while (Firmata.available()) {
Firmata.processInput();
}
}
if (pin == servoPinMap[i]) {
servos[i].write(value); // Write the value to the servo
break;
}
}
}
void systemResetCallback() {
servoCount = 0; // Reset the count of attached servos
}
void setup() {
// Set firmware version
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
// Attach callbacks
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.attach(SYSTEM_RESET, systemResetCallback);
// Begin communication
Firmata.begin(57600);
// Attach each servo to its respective pin
for (int i = 0; i < MAX_SERVOS; i++) {
servos[i].attach(servoPinMap[i]); // Attach servo to its mapped pin
}
}
void loop() {
// Process incoming commands
while (Firmata.available()) {
Firmata.processInput();
}
}
No comments:
Post a Comment