aboutsummaryrefslogtreecommitdiff
path: root/arduino-src/robot.ino
blob: 2eda6f246e3c4eb3c6143a8b29b0eba358b4f46d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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);
}