All working on a single device
[miniband.git] / release / instruments3 / instruments3.ino
1 const int NUMBER_OF_MARACAS = 2;
2 const int MARACAS_PINS[] = {6, 7};
3 const int MARACAS_DELAY = 5000;
4
5 const int NUMBER_OF_DRUMS= 4;
6 const int DRUM_PINS[] = {8, 9, 10, 11};
7 const int DRUM_DELAY = 2500;
8
9 const int GUITAR_THRESHOLD = 20;
10 const int GUITAR_PIN = A0;
11 const int GUITAR_DELAY = 5000;
12
13 int maracas_states[] = {-1,-1};
14 int maracas_delays[] = {0, 0};
15
16 int drum_delays[] = {0, 0, 0, 0};
17
18 int guitar_delay = 0;
19
20 void setup() {
21 for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
22 pinMode(MARACAS_PINS[i], INPUT);
23 }
24 for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
25 pinMode(DRUM_PINS[i], INPUT);
26 }
27 Serial.begin(9600);
28 }
29
30 void loop() {
31 maracas();
32 drums();
33 guitar();
34 update_delays();
35 }
36
37
38 void maracas() {
39 for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
40 int newState = digitalRead(MARACAS_PINS[i]);
41 if (newState != maracas_states[i] && maracas_delays[i] < 1) {
42 Serial.print("maracas,");
43 Serial.print(i);
44 Serial.println();
45 maracas_states[i] = newState;
46 maracas_delays[i] = MARACAS_DELAY;
47 }
48 }
49 }
50
51 void drums() {
52 for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
53 if (digitalRead(DRUM_PINS[i]) == HIGH && drum_delays[i] < 1) {
54 Serial.print("drum,");
55 Serial.print(i);
56 Serial.println();
57 drum_delays[i] = DRUM_DELAY;
58 }
59 }
60 }
61
62 void guitar() {
63 int pitch = analogRead(GUITAR_PIN);
64 if (pitch >= GUITAR_THRESHOLD && guitar_delay < 1) {
65 Serial.print("guitar,");
66 Serial.print(pitch);
67 Serial.println();
68 guitar_delay = GUITAR_DELAY;
69 }
70 }
71
72 void update_delays() {
73 for (int i = 0; i < NUMBER_OF_MARACAS; i++) {
74 if (maracas_delays[i] > 0) {
75 maracas_delays[i] -= 1;
76 }
77 }
78 for (int i = 0; i < NUMBER_OF_DRUMS; i++) {
79 if (drum_delays[i] > 0) {
80 drum_delays[i] -= 1;
81 }
82 }
83 if (guitar_delay > 0) {
84 guitar_delay -= 1;
85 }
86 }