/
ScalePlayer.ino
190 lines (134 loc) · 4.31 KB
/
ScalePlayer.ino
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
// constants related to the Arduino Nano pin use
const int clkIn = 2; // the digital (clock) input
const int digPin[2] = {3, 4}; // the digital output pins
const int pinOffset = 5; // the first DAC pin (from 5-12)
const int trigTime = 25; // 25 ms trigger timing
// variables for interrupt handling of the clock input
volatile int clkState = LOW;
// variables used to control the current DIO output states
int digState[2] = {LOW, LOW}; // start with both set low
unsigned long digMilli[2] = {0, 0}; // a place to store millis()
int currentScaleValue = 0;
int previousNote = 98;
const int noOfPatterns = 9; // plus one here if a pattern is added to "pattern"
const int patternLength = 9;
int patternValue;
int patternType;
int patternTypeUtility = 1023 / noOfPatterns;
int patternPlace = 0;
int pattern[noOfPatterns][patternLength] = {
{8,7,6,5,4,3,2,1,0},
{0,1,2,3,4,5,6,7,8},
{0,1,0,2,0,3,0,4,3},
{0,2,3,2,3,5,3,5,2},
{0,1,2,3,8,7,6,5,4},
{0,8,1,7,2,6,3,5,4},
{0,1,8,7,2,3,6,5,4},
{0,0,0,1,2,4,3,8,7},
{0,0,0,0,0,1,1,2,2},
};
int scale;
const int noOfScales = 25;
const int scaleLength = 9;
int scaleUtility = 1023 / noOfScales;
int scales[noOfScales][scaleLength] = {
// scales. 99 means "end here"
// Major. 12 scales
{1,3,4,6,8,9,11,13,99}, // c major
{1,3,5,6,8,10,11,13,99}, // d major
{2,4,5,7,9,10,12,14,99}, // C#/Db
{2,4,6,7,9,11,12,14,99}, // D#/Eb
{1,3,5,7,8,10,12,13,99}, // E Major
{1,2,4,6,8,9,11,13,99}, // F Major
{2,3,5,7,9,10,12,14,99}, // F#/Gb
{1,3,4,6,8,10,11,13,99}, // G Major
{2,4,5,7,9,11,12,14,99}, // G#/Ab
{1,3,5,6,8,10,12,13,99}, // A Major
{1,2,4,6,7,9,11,13,99}, // A#/Bb
{2,3,5,7,8,10,12,14,99}, // B Major
// Minor. 12 scales.
{1,3,4,6,8,9,11,13,99}, // A Minor
{2,4,5,7,9,10,12,14,99}, // A#/Bb
{1,3,5,6,8,10,11,13,99}, // B Minor
{1,3,5,6,8,10,12,13,99}, // C Minor
{1,3,5,7,8,10,12,13,99}, // C#/Db
{1,2,4,6,8,9,11,13,99}, // D Minor
{2,3,5,7,9,10,12,14,99}, // D#/Eb
{1,3,4,6,8,10,11,13,99}, // E Minor
{2,4,5,7,9,11,12,14,99}, // F Minor
{1,3,5,6,8,10,12,13,99}, // F#/Gb
{1,2,4,6,7,9,11,13,99}, // G Minor
{1,2,3,5,7,8,12,13,99}, // G#/Ab
// Pentatonic. 12 scales.
{1,4,6,8,11,13,99,99,99}
};
////////////////////////////////////////////////////////
void setup()
{
Serial.begin(9600);
pinMode(clkIn, INPUT);
for (int i=0; i<2; i++) {
pinMode(digPin[i], OUTPUT);
digitalWrite(digPin[i], LOW);
}
for (int i=0; i<8; i++) {
pinMode(pinOffset+i, OUTPUT);
digitalWrite(pinOffset+i, LOW);
}
attachInterrupt(0, isr, RISING);
}
void loop() {
scale = (analogRead(2) / scaleUtility) -1; if (scale == -1) {scale = 0;}
patternType = (analogRead(3) / patternTypeUtility) -1; if (patternType == -1) {patternType = 0;}
if (clkState == HIGH) {
clkState = LOW;
digitalWrite(digPin[0], HIGH);
digitalWrite(digPin[0], LOW);
patternValue = pattern[patternType][patternPlace];
// Serial.print("patternValue: "); Serial.println(patternValue);
// Serial.print("patternPlace: "); Serial.println(patternPlace);
while (scales[scale][patternValue] == 99) {
patternPlace++;
if (patternPlace >= patternLength) {
patternPlace = 0;
}
patternValue = pattern[patternType][patternPlace];
}
int note = (((scales[scale][patternValue]) * 4) + (12 * 4));
dacOutput(note);
if (previousNote != note) {
digitalWrite(digPin[1], HIGH);
digitalWrite(digPin[1], LOW);
}
previousNote = note;
patternPlace++;
if (patternPlace >= patternLength) {
patternPlace = 0;
}
}
}
void isr()
{
clkState = HIGH;
}
void writeStep(boolean on){
if(on){
digitalWrite(digPin[0], HIGH);
digitalWrite(digPin[1], LOW);
}else{
digitalWrite(digPin[0], LOW);
digitalWrite(digPin[1], HIGH);
}
}
// dacOutput(long) - deal with the DAC output
// ------------------------------------------
void dacOutput(long v)
{
// feed this routine a value between 0 and 255 and teh DAC
// output will send it out.
int tmpVal = v;
for (int i=0; i<8; i++) {
digitalWrite(pinOffset + i, tmpVal & 1);
tmpVal = tmpVal >> 1;
}
}