-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmusic-muscle-song.ino
More file actions
200 lines (157 loc) · 5.44 KB
/
Copy pathmusic-muscle-song.ino
File metadata and controls
200 lines (157 loc) · 5.44 KB
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
#include <TimerOne.h>
#include <EEPROM.h>
#include "pitches.h"
#include "themes.h"
int sensorPin = A0;
int speaker = 9;
int button = 3;
int potenci = A1;
int led = 10;
int thisNote = 0;
int pauseBetweenNotes;
int buttonState=1, oldbuttonState=1,diffButton=0,realState=1;
float firstClick=0.0,endTime=0.0;
const int arraySize=150; /*Vector size, the bigger the number, more smooth it will be*/
int arrayPosition=0 , i, sensorValue=0,absSensorValue, pinValue=0, oldPinValue=0;
long sumSensor[arraySize];
long first_time=0.0 , last_time=0.0, time_diference=0.0; /*first_time gives the time when the muscule activated, las_time gives the time when the muscule relaxed, time_diference gives the activation time*/
int state_sign=0;
float maximum_value=0.0; /*maximum_value gives the maximum value in the activation time period*/
float smoothed=0.0;
long sumNumbers;
unsigned long t, t0;
long dt = 1000; /* Time between samples */
float activation= 7; /*Treshold needed to be surpassed*/
volatile int potenciValue=0;
int calibr_state=0;
float calibrValue=0.0;
float calibrValueMin=50.0;
int addr = 0, addr2 =1;
int calibr_clean = 0;
float value=0.0;
struct mainValues{ /* Creates an easier acess to the values */
volatile unsigned long times=t0;
volatile int raw=sensorValue;
volatile float smooth=smoothed;
};
mainValues values = {}; /* To acess time, raw and smooth use like values.times or values.raw or values.smooth */
void setup() {
Serial.begin(2000000); /*Bits per second*/
t0=micros();
for(i=0; i<arraySize; i++) {
sumSensor[i]=0;
}
calibrValue=EEPROM.read(addr);
calibrValueMin=EEPROM.read(addr2);
sumNumbers=0;
state_sign=0;
pinMode(speaker,OUTPUT);
pinMode(button,INPUT_PULLUP);
pinMode(led,OUTPUT);
digitalWrite(speaker,LOW);
Timer1.initialize(10000);
Timer1.attachInterrupt(smoothedFunction);
}
void loop() {
oldbuttonState=buttonState;
buttonState=digitalRead(button);
diffButton=buttonState-oldbuttonState;
if((diffButton==-1)&&(realState==1)){
firstClick=millis();
}
else if((diffButton==1)&&(millis()-firstClick>=2000)&&(millis()-endTime>5000)){
realState=0;
}
else if((diffButton==0)&&(realState==0)){
//calibration
Calibration();
digitalWrite(led,HIGH);
}
else if((diffButton==-1)&&(millis()-firstClick>=5000)){
digitalWrite(led,LOW);
//delay(2000);
realState=1;
//firstClick=0;
endTime=millis();
}
else{
value=map(values.smooth,calibrValueMin,calibrValue,0,100);
activation=map(potenciValue,0,676,0,50);
thisNote=thisNote+1;
int noteDuration = 1000 / Titanic_duration[thisNote];//convert duration to time delay
tone(speaker, Titanic_note[thisNote], noteDuration);
pauseBetweenNotes = noteDuration *(3-(0.01*value));
if(pauseBetweenNotes<0.01){
pauseBetweenNotes=0.01;
}
delay(pauseBetweenNotes);
noTone(speaker);
Serial.println(pauseBetweenNotes);
Serial.println(value);
if(thisNote>=(sizeof(Titanic_note)/sizeof(int))-1){
thisNote=0;
}
}
/*
if(potenciValue>512){
}
else{
value=map(values.smooth,calibrValueMin,calibrValue,0,100);
Serial.println(value);
if(value>=activation){
tone(speaker, 10*value, 5);
}
}
*/
oldbuttonState=buttonState;
}
void smoothedFunction (){
potenciValue=analogRead(potenci);
t0 = micros();
oldPinValue = pinValue; /* Needed to use in the derivation */
pinValue = analogRead(sensorPin);
sensorValue = pinValue-oldPinValue; /* With this line its possible to make the value 0 as our reference point */
absSensorValue=abs(sensorValue); /*Transform negative values into positive values*/
if (arrayPosition>=arraySize){ /*If the position that the program is writing in the array is bigger than the size of the array, it will replace the index nr 0 of the array*/
arrayPosition=0;
}
sumNumbers-=sumSensor[arrayPosition];
sumSensor[arrayPosition]=absSensorValue; /*Add the value received from the sensor to the array*/
sumNumbers+=sumSensor[arrayPosition];
++arrayPosition; /*Increment 1 value to the array position, making it move to the next index from the array*/
smoothed=(sumNumbers)/((float)arraySize); /*Make the average value using the values received*/
values.times=t0;
values.raw=pinValue;
values.smooth=smoothed;
//Serial.println(values.times);
//FeatureExtraction();
}
void Calibration (){
if(realState==0){
if(calibr_state==0){
calibrValue=0.0;
calibr_state=1;
}
if(values.smooth>=calibrValue){
calibrValue=values.smooth;
}
if(values.smooth<=calibrValueMin){
calibrValueMin=values.smooth;
}
}
else if((calibr_state==1)){
EEPROM.write(addr, calibrValue);
EEPROM.write(addr2, calibrValueMin);
calibr_state=0;
}
}
void Play_Pirates()
{
for (int thisNote = 0; thisNote < (sizeof(Pirates_note)/sizeof(int)); thisNote++) {
int noteDuration = 1000 / Pirates_duration[thisNote];//convert duration to time delay
tone(speaker, Pirates_note[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.05; //Here 1.05 is tempo, increase to play it slower
delay(pauseBetweenNotes);
noTone(speaker); //stop music on pin 8
}
}