Hi All-
I am working on a design project for school and need some help with writing code for the Arduino. I am trying to use the Arduino to do the following:
-
Record sound data.
-
Threshold that sound data.
-
Detect when sound breaks threshold, and output a signal to a speaker to signify threshold being broken.
-
After the first time the signal breaks threshold, check every second and a half to see if a signal again broke threshold. If it did, just continue to check every second and a half. If it did not, then it should again output a signal to the speaker to signify threshold being broken.
I have written a working program in MATLAB, but am completely new to the Arduino board, and have never programmed in anything besides MATLAB, and need some help! This is the MATLAB code I wrote:
clear all
clc
daqreset
%%%%Phase 1
fs = 1000; %Sampling Rate
s = 3; %Time of data aquisition in seconds
TGOC1=analoginput(‘nidaq’,‘Dev2’);
addchannel(TGOC1,0);
set(TGOC1,‘SampleRate’,fs)
set(TGOC1,‘SamplesPerTrigger’,s*fs)
start(TGOC1)
SoundData=getdata(TGOC1);
NormalizedData=SoundData-(sum(SoundData)/length(SoundData));
Threshold=1.25*max(NormalizedData);
%%%%%Phase 2: Re-starting Acquisition. Waiting for first signal that breaks
%%%%%threshold.
RepControl=1;
s=30;
TGOC2=analoginput(‘nidaq’,‘Dev2’);
AO=analogoutput(‘nidaq’,‘Dev2’);
addchannel(TGOC2,0);
addchannel(AO,1);
set(TGOC2,‘SampleRate’,fs)
set(TGOC2,‘SamplesPerTrigger’,s*fs)
AO=analogoutput(‘nidaq’,‘Dev2’);
addchannel(AO,0);
% set(AO,‘SampleRate’,100)
OutputData=3;
start(TGOC2)
pause(1)
z=1
while RepControl==1
data=peekdata(TGOC2,100);
data=data-(sum(data)/length(data));
if max(data)>=Threshold
putsample(AO,OutputData)
pause(1)
putsample(AO,0)
RepControl=0
stop(TGOC2)
end
end
delete(TGOC2)
clear(‘TGOC2’)
delete(AO)
clear(‘AO’)
%%%%%Phase 3: predicting last beat
RepControl=1;
s=60;
TGOC3=analoginput(‘nidaq’,‘Dev2’);
A1=analogoutput(‘nidaq’,‘Dev2’);
addchannel(TGOC3,0);
addchannel(A1,1);
set(TGOC3,‘SampleRate’,fs)
set(TGOC3,‘SamplesPerTrigger’,s*fs)
A1=analogoutput(‘nidaq’,‘Dev2’);
addchannel(A1,0);
OutputData=3;
start(TGOC3)
while RepControl==1
pause(1.5)
data=peekdata(TGOC3,1400);
data=data-(sum(data)/length(data));
if max(data)<=Threshold
putsample(A1,OutputData)
pause(1)
putsample(A1,0)
RepControl=0
stop(TGOC3)
end
end
delete(TGOC3)
clear(‘TGOC3’)
The code and device works perfectly in MATLAB, and I think it should work using the Arduino as well. Here is my crude Arduino code for steps 1 -3 above, which is not working, but I’m not sure why. Help is greatly appreciated! Thanks all!
int analogPin = 3; // AI pin connected to analog pin 3
int outPin = 9; // outside leads to ground and +5V
int data[2999]; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
for (int i=1; i <= 3000; 1)
{
val = analogRead(analogPin); // read the input pin
data(i)=val;
delayMicroseconds(900);} // sample at about 1000 hz
threshold=1.25*max(data);
{ delay(2000)
val = analogRead(analogPin)
if (val>threshold)
analogWrite(outPin,255)
delay(1000)
analogWrite(outPin,0)
}
{ delay(2000)