summaryrefslogtreecommitdiff
path: root/trunk/darwin/firmware/Sanguino/Sanguino3G/testers/imperial_stepper/imperial_stepper.pde
blob: 857fdc2cc3edf448a5900975a001c446d8fe74f0 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Yep, this is actually -*- c++ -*-

//x axis pins
#define X_STEP_PIN      15
#define X_DIR_PIN       18
#define X_ENABLE_PIN    19
#define X_MIN_PIN       20
#define X_MAX_PIN       21

//y axis pins
#define Y_STEP_PIN      23
#define Y_DIR_PIN       22
#define Y_ENABLE_PIN    24
#define Y_MIN_PIN       25
#define Y_MAX_PIN       26

//z axis pins
#define Z_STEP_PIN      27
#define Z_DIR_PIN       28
#define Z_ENABLE_PIN    29
#define Z_MIN_PIN       30
#define Z_MAX_PIN       31

//pin for controlling the PSU.
#define PS_ON_PIN       14

void init_psu()
{
#ifdef PS_ON_PIN
  pinMode(PS_ON_PIN, OUTPUT);
  turn_psu_on();
#endif
}

void turn_psu_on()
{
#ifdef PS_ON_PIN
  digitalWrite(PS_ON_PIN, LOW);
  delay(2000); //wait for PSU to actually turn on.
#endif
}

void turn_psu_off()
{
#ifdef PS_ON_PIN
  digitalWrite(PS_ON_PIN, HIGH);
#endif
}

void setup()
{
  Serial.begin(38400);
  Serial.println("You have failed me for the last time.");

  //initialize all our pins.
  pinMode(X_STEP_PIN, OUTPUT);
  pinMode(X_DIR_PIN, OUTPUT);
  pinMode(X_ENABLE_PIN, OUTPUT);
  pinMode(X_MIN_PIN, INPUT);
  pinMode(X_MAX_PIN, INPUT);

  pinMode(Y_STEP_PIN, OUTPUT);
  pinMode(Y_DIR_PIN, OUTPUT);
  pinMode(Y_ENABLE_PIN, OUTPUT);
  pinMode(Y_MIN_PIN, INPUT);
  pinMode(Y_MAX_PIN, INPUT);

  pinMode(Z_STEP_PIN, OUTPUT);
  pinMode(Z_DIR_PIN, OUTPUT);
  pinMode(Z_ENABLE_PIN, OUTPUT);
  pinMode(Z_MIN_PIN, INPUT);
  pinMode(Z_MAX_PIN, INPUT);
  
  digitalWrite(X_MIN_PIN, HIGH);
  digitalWrite(X_MAX_PIN, HIGH);
  digitalWrite(Y_MIN_PIN, HIGH);
  digitalWrite(Y_MAX_PIN, HIGH);
  digitalWrite(Z_MIN_PIN, HIGH);
  digitalWrite(Z_MAX_PIN, HIGH);

  init_psu();
  calculate_tones();
}

void loop()
{
  Serial.println("Forward!");
  digitalWrite(X_DIR_PIN, HIGH);
  digitalWrite(Y_DIR_PIN, HIGH);
  digitalWrite(Z_DIR_PIN, HIGH);
  play_song(X_MAX_PIN, Y_MAX_PIN, Z_MAX_PIN);

  delay(500);

  Serial.println("Reverse!");
  digitalWrite(X_DIR_PIN, LOW);
  digitalWrite(Y_DIR_PIN, LOW);
  digitalWrite(Z_DIR_PIN, LOW);
  play_song(X_MIN_PIN, Y_MIN_PIN, Z_MIN_PIN);

  delay(500);
}

boolean at_switch(byte pin)
{
  return !digitalRead(pin); 
}

#define TONE_COUNT 27

float frequencies[TONE_COUNT] = {
  196.00, //G2   0
  207.65, //G#2   1
  220.00, //A2    2
  233.08, //Bb2   3
  246.94, //B2    4
  261.63, //C3    5
  277.18, //C#3   6
  293.66, //D3    7
  311.13, //D#3   8
  329.63, //E3    9
  349.23, //F3    10
  369.99, //F#3   11
  392.00, //G3    12
  415.30, //G#3   13
  440.00, //A3    14
  466.16, //Bb3   15
  493.88, //B3    16
  523.25, //C4    17
  554.37, //C#4   18
  587.33, //D4    19
  622.25, //D#4   20
  659.26, //E4    21
  698.46, //F4    22
  739.99, //F#4   23
  783.99, //G4    24
  830.61, //G#4   25
  880.00  //A4    26
};


int tones[TONE_COUNT];

#define NOTE_COUNT 66
int notes[] = {
  12,12,12, 8,15,12, 8,15,12,   // 9
  19,19,19,20,15,12, 8,15,12,   // 9
  24,12,12,24,23,22,21,20,21,   // 9
  13,18,17,16,15,14,15,         // 7
   8,11, 8,11,15,12,15,19,      // 8
  24,12,12,24,23,22,21,20,21,   // 9
  13,18,17,16,15,14,15,         // 7
   8,11, 8,15,12, 8,15,12       // 8
};
int lengths[] = {
    4, 4, 4, 3, 1, 4, 3, 1, 8,
    4, 4, 4, 3, 1, 4, 3, 1, 8,
    4, 3, 1, 4, 3, 1, 1, 1, 4,
    2, 4, 3, 1, 1, 1, 4,
    2, 4, 3, 1, 4, 3, 1, 8,
    4, 3, 1, 4, 3, 1, 1, 1, 4,
    2, 4, 3, 1, 1, 1, 4,
    2, 4, 3, 1, 4, 3, 1, 8
};

void calculate_tones()
{
  for (byte i=0; i<TONE_COUNT; i++)
    tones[i] = (int)(1000000.0/ (2.0 * frequencies[i]));
}

void play_song(byte xPin, byte yPin, byte zPin)
{
  digitalWrite(X_ENABLE_PIN, LOW); //enable
  digitalWrite(Y_ENABLE_PIN, LOW); //enable
  digitalWrite(Z_ENABLE_PIN, LOW); //enable

  for (byte i=0; i<NOTE_COUNT; i++)
  {
    if (!at_switch(xPin) && !at_switch(yPin) && !at_switch(zPin))
    {
      play_note(tones[notes[i]], 80000*lengths[i]);
      delay(10); 
    }
  }
  digitalWrite(X_ENABLE_PIN, HIGH); //disable
  digitalWrite(Y_ENABLE_PIN, HIGH); //disable
  digitalWrite(Z_ENABLE_PIN, HIGH); //disable
}

void play_note(int note, long time)
{
  int count = round(time / note);

  for (int i=0; i<count; i++)
  {
    digitalWrite(X_STEP_PIN, HIGH);
    digitalWrite(Y_STEP_PIN, HIGH);
    digitalWrite(Z_STEP_PIN, HIGH);
    delayMicroseconds(note);
    digitalWrite(X_STEP_PIN, LOW);
    digitalWrite(Y_STEP_PIN, LOW);
    digitalWrite(Z_STEP_PIN, LOW);
    delayMicroseconds(note);
  }
}