-
Notifications
You must be signed in to change notification settings - Fork 0
/
CupOfJoy.ino
95 lines (74 loc) · 2.29 KB
/
CupOfJoy.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
#include <MusicWithoutDelay.h>
#include <synth.h>
#include <tables.h>
struct JoystickDTO {
float JoyAngle;
float JoyForce;
JoystickDTO(int pinX, int pinY) {
float nX = analogRead(pinX) - 512;
float nY = analogRead(pinY) - 512;
/*
Serial.print(nX);
Serial.print("\t"); // a space ' ' or tab '\t' character is printed between the two values.
Serial.print(nY);
Serial.print("\t");
*/
JoyAngle = (atan2(nX, nY)+PI) * (180.0/PI);
JoyForce = min(sqrt((nX*nX) + (nY*nY)), 512);
}
// The direction the joystick point at divided to 8 segments
int getSegment() {
if (JoyAngle < 22.5 || JoyAngle > 337.5) {
return 0;
}
return (JoyAngle+22.5)/45;
}
bool isTilted() {
return JoyForce > 50;
}
int getTransposedNote(int transposition) {
if (!isTilted()){
return -1;
}
return 60 + getSegment() + transposition;
}
};
class ToneGenerator {
public:
MusicWithoutDelay Buzzer;
float Volume;
void update() {
Buzzer.setVolume(Volume);
Buzzer.update();
}
};
ToneGenerator toneGenerators[3];
void setup() {
toneGenerators[0].Buzzer.begin(CHB, TRIANGLE, ENVELOPE0, 0);
for(int i = 1; i < (sizeof(toneGenerators)/sizeof(*toneGenerators)); i++){
toneGenerators[i].Buzzer.begin(TRIANGLE, ENVELOPE0, 0);
}
Serial.begin(9600);
}
void loop() {
JoystickDTO joysticks[] = {{A0, A1}, {A2, A3}, {A4, A5}, {A6, A7}};
//Serial.println(0);
int notes[] = { joysticks[0].getTransposedNote(0),
joysticks[1].getTransposedNote(4),
joysticks[2].getTransposedNote(7)};
strum(notes, joysticks[3]);
}
void strum(int notes[], JoystickDTO strumJoystick){
bool isTilted = strumJoystick.isTilted();
for(int i = 0; i < 3; i++){
if (isTilted && notes[i] > 0) {
double mappedFrequency = toneGenerators[i].Buzzer.getNoteAsFrequency(notes[i] + strumJoystick.getSegment());
toneGenerators[i].Volume = max(toneGenerators[i].Volume, min(127, map(strumJoystick.JoyForce, 50,512,0,127)));
toneGenerators[i].Buzzer.setFrequency(mappedFrequency);
}
else if (toneGenerators[i].Volume > 0){
--toneGenerators[i].Volume;
}
toneGenerators[i].update();
}
}