Code-Review For Save & Play Robotic Arm
c++
#include <Servo.h>
const int maxFrames = 200;
const int servoCount = 3;
const int keyframeDistance = 75;
int savedAnimation[maxFrames][servoCount];
int savedFrameCount = 0;
const int saveButtonPin = 6;
const int playButtonPin = 7;
const int potPins[servoCount] = {A0, A1, A2, A3};
const int servoPins[servoCount] = {2, 3, 4, 5};
Servo servos[servoCount];
void setup() {
for (int i = 0; i < servoCount; i++) {
servos[i].attach(servoPins[i]);
}
pinMode(saveButtonPin, INPUT);
pinMode(playButtonPin, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(saveButtonPin) == HIGH) {
int frame = 0;
while (digitalRead(saveButtonPin) == HIGH && frame < maxFrames) {
for (int i = 0; i < servoCount; i++) {
int pos = analogRead(potPins[i]) / 6;
servos[i].write(pos);
savedAnimation[frame][i] = pos;
}
frame++;
delay(keyframeDistance);
}
savedFrameCount = frame;
}
else if (digitalRead(playButtonPin) == HIGH) {
for (int frame = 0; frame < savedFrameCount; frame++) {
for (int i = 0; i < servoCount; i++) {
servos[i].write(savedAnimation[frame][i]);
}
delay(keyframeDistance);
}
}
else {
for (int i = 0; i < servoCount; i++) {
int pos = analogRead(potPins[i]) / 6;
servos[i].write(pos);
}
}
}
c++
#include <Servo.h>
const int maxFrames = 200;
const int servoCount = 3;
const int keyframeDistance = 75;
int savedAnimation[maxFrames][servoCount];
int savedFrameCount = 0;
const int saveButtonPin = 6;
const int playButtonPin = 7;
const int potPins[servoCount] = {A0, A1, A2, A3};
const int servoPins[servoCount] = {2, 3, 4, 5};
Servo servos[servoCount];
void setup() {
for (int i = 0; i < servoCount; i++) {
servos[i].attach(servoPins[i]);
}
pinMode(saveButtonPin, INPUT);
pinMode(playButtonPin, INPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(saveButtonPin) == HIGH) {
int frame = 0;
while (digitalRead(saveButtonPin) == HIGH && frame < maxFrames) {
for (int i = 0; i < servoCount; i++) {
int pos = analogRead(potPins[i]) / 6;
servos[i].write(pos);
savedAnimation[frame][i] = pos;
}
frame++;
delay(keyframeDistance);
}
savedFrameCount = frame;
}
else if (digitalRead(playButtonPin) == HIGH) {
for (int frame = 0; frame < savedFrameCount; frame++) {
for (int i = 0; i < servoCount; i++) {
servos[i].write(savedAnimation[frame][i]);
}
delay(keyframeDistance);
}
}
else {
for (int i = 0; i < servoCount; i++) {
int pos = analogRead(potPins[i]) / 6;
servos[i].write(pos);
}
}
}
3 Replies