diff options
author | Franciszek Malinka <franciszek.malinka@gmail.com> | 2022-06-12 11:20:06 +0200 |
---|---|---|
committer | Franciszek Malinka <franciszek.malinka@gmail.com> | 2022-06-12 11:20:06 +0200 |
commit | 1c9dae803882f6f5cbbd17c661e0a9f627ce8038 (patch) | |
tree | 706fa0d2bdb41f1b645b8b127999b7952bce6c0b | |
parent | d27ca93ac86697f8be6af3c751a5a527f3c07c88 (diff) |
-rw-r--r-- | arduino-src/robot.ino | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/arduino-src/robot.ino b/arduino-src/robot.ino new file mode 100644 index 0000000..2eda6f2 --- /dev/null +++ b/arduino-src/robot.ino @@ -0,0 +1,82 @@ +#define STEP_DELAY 800 /* The bigger, the slower the stepper motors */ +#define DELAY 1 +#define STEPS_PER_FULL 200 + + +/* Those arrays are indexed by the enum Face */ +const int step_pins[] = {2, 4, 6, 8, 10, 12}; +const int dir_pins[] = {3, 5, 7, 9, 11, 13}; + +enum Face { + RIGHT, + LEFT, + UP, + DOWN, + FRONT, + BACK, +}; + +enum Rot { + CLOCKWISE = 0, + CNTRWISE = 1, + HALFROT, +}; + +void make_steps(int spin, int dpin, int dir, int steps=1) { + digitalWrite(dpin, !dir); + for (int i = 0; i < steps; i++) { + digitalWrite(spin, HIGH); + delayMicroseconds(STEP_DELAY); + digitalWrite(spin, LOW); + delayMicroseconds(STEP_DELAY); + } +} + +void rotate_face(Face face, Rot rot) { + int spin = step_pins[face]; + int dpin = dir_pins[face]; + + String msg = "Rotating " + String(face) + " " + String(rot); + Serial.println(msg); + switch (rot) { + case CLOCKWISE: + Serial.println("XDDDD"); + make_steps(spin, dpin, CLOCKWISE, STEPS_PER_FULL/4); + break; + case CNTRWISE: + make_steps(spin, dpin, CNTRWISE, STEPS_PER_FULL/4); + break; + case HALFROT: + make_steps(spin, dpin, CLOCKWISE, STEPS_PER_FULL/2); + break; + default: + break; + } +} + +bool get_rotation(Face &face, Rot &rot) { + byte b; + if (!Serial.readBytes(&b, 1)) { + return false; + } + + face = b/3; + rot = b%3; + + return true; +} + +void setup() { + Serial.begin(115200); + for (int stepper = 0; stepper < 6; stepper++) { + pinMode(step_pins[stepper], OUTPUT); + pinMode(dir_pins[stepper], OUTPUT); + } +} + +void loop() { + Face face = UP; + Rot rot = CLOCKWISE; + while (!get_rotation(face, rot)); + rotate_face(face, rot); +} |