Created
December 16, 2023 01:58
-
-
Save shabaz123/f7139cad01719084d8d42be42aabab89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********* animation test ************* | |
* tested with an Arduino Mega | |
**************************************/ | |
#include <Servo.h> | |
#include <animationTools.h> | |
// ********** definitions ************************ | |
// total number of servos | |
#define SERVO_TOTAL 6 | |
// enumerate the servos from 0 to SERVO_TOTAL-1 | |
#define SERVO_COW 0 | |
#define SERVO_SHEEP 1 | |
#define SERVO_PIG 2 | |
#define SERVO_CHICKEN 3 | |
#define SERVO_DOG 4 | |
#define SERVO_CAT 5 | |
// total number of events, set to 20 or less (20 is the limit of the arduinoAnimation library) | |
#define EVENT_TOTAL 10 | |
// ******* global variables ********************** | |
// Arduino pins for the servos | |
const int servoPin[SERVO_TOTAL] = {2, 3, 4, 5, 6, 7}; | |
// animation defined in terms of event time, position(angle), and travel time | |
// Times are in milliseconds, positions are in degrees | |
const int eventTime[SERVO_TOTAL][EVENT_TOTAL] = { | |
{0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}, /* servo 0 */ | |
{5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500}, /* servo 1 */ | |
{0, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 4000}, /* servo 2 */ | |
{0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}, /* servo 3 */ | |
{0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}, /* servo 4 */ | |
{0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}, /* servo 5 */ | |
}; | |
const int eventPosition[SERVO_TOTAL][EVENT_TOTAL] = { | |
{0, 30, 60, 30, 90, 120, 150, 180, 180, 180}, /* servo 0 */ | |
{0, 90, 100, 110, 120, 140, 120, 110, 100, 90}, /* servo 1 */ | |
{0, 90, 90, 90, 90, 90, 90, 90, 90, 0}, /* servo 2 */ | |
{0, 90, 0, 90, 0, 90, 0, 90, 0, 90}, /* servo 3 */ | |
{0, 90, 0, 90, 0, 90, 0, 90, 0, 90}, /* servo 4 */ | |
{0, 90, 0, 90, 0, 90, 0, 90, 0, 90}, /* servo 5 */ | |
}; | |
const int travelTime[SERVO_TOTAL] = { | |
1000, /* servo 0 */ | |
500, /* servo 1 */ | |
1000, /* servo 2 */ | |
1000, /* servo 3 */ | |
1000, /* servo 4 */ | |
1000, /* servo 5 */ | |
}; | |
Servo servo[SERVO_TOTAL]; | |
Timeline timeline[SERVO_TOTAL]; | |
// ********** setup function ********************* | |
void setup() { | |
int i, j; | |
Serial.begin(9600); | |
// initialize the servos | |
for (i = 0; i < SERVO_TOTAL; i++) { | |
servo[i].attach(servoPin[i]); | |
} | |
// initialize the timelines | |
for (i = 0; i < SERVO_TOTAL; i++) { | |
timeline[i].totalKeys = EVENT_TOTAL; | |
timeline[i].travelTime = travelTime[i]; | |
timeline[i].loop = true; | |
for (j = 0; j < EVENT_TOTAL; j++) { | |
timeline[i].timeKey[j] = eventTime[i][j]; | |
timeline[i].valKey[j] = eventPosition[i][j]; | |
} | |
} | |
// set all the servos to the start value | |
for (i = 0; i < SERVO_TOTAL; i++) { | |
servo[i].write(timeline[i].valKey[0]); | |
delay(100); // give the servo time to get there | |
} | |
} | |
void loop() { | |
int i; | |
int angle; | |
for (i=0; i<SERVO_TOTAL; i++) { | |
angle = getTimelineValue(timeline[i]); | |
servo[i].write(angle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment