Você está na página 1de 8

Code: 

#define capPin A5
if (buzPeriod>300)
Arduino M
Detecto
buzState=0;
#define buz 9 if (buzState==0)
#define pulsePin A4 {
#define led 10 digitalWrite(led, LOW);
long sumExpect=0; //running sum of 64 noTone(buz);
sums }
long ignor=0; //number of ignored sums else if (buzState==1)
long diff=0; //difference between sum {

Circuit Diag
and avgsum tone(buz,2000);
long pTime=0; digitalWrite(led, HIGH);
long buzPeriod=0; }
void setup()
{ else if (buzState==2)
Serial.begin(9600); {
pinMode(pulsePin, OUTPUT); tone(buz,500);
digitalWrite(pulsePin, LOW); digitalWrite(led, HIGH);
pinMode(capPin, INPUT); }
pinMode(buz, OUTPUT); }
digitalWrite(buz, LOW); //subtract minimum and maximum value
pinMode(led, OUTPUT); to remove spikes
} sum-=minval;
void loop() sum-=maxval;
{
int minval=1023; if (sumExpect==0)
int maxval=0; sumExpect=sum<<6; //set sumExpect to
long unsigned int sum=0; expected value
for (int i=0; i<256; i++) long int avgsum=(sumExpect+32)>>6;
{ diff=sum-avgsum;
//reset the capacitor if (abs(diff)<avgsum>>10)
pinMode(capPin,OUTPUT); {
digitalWrite(capPin,LOW); sumExpect=sumExpect+sum-avgsum;
delayMicroseconds(20); ignor=0;
pinMode(capPin,INPUT); }
applyPulses(); else
ignor++;
//read the charge of capacitor if (ignor>64)
int val = analogRead(capPin); //takes {
13x8=104 microseconds sumExpect=sum<<6;
minval = min(val,minval); ignor=0;
maxval = max(val,maxval); }
sum+=val;
pinMode(capPin,OUTPUT); {
digitalWrite(capPin,LOW); sumExpect=sumExpect+sum-avgsum;
delayMicroseconds(20); ignor=0;
pinMode(capPin,INPUT); }
applyPulses(); else
ignor++;
//read the charge of capacitor if (ignor>64)
int val = analogRead(capPin); //takes {
13x8=104 microseconds sumExpect=sum<<6;
minval = min(val,minval); ignor=0;
maxval = max(val,maxval); }
sum+=val; if (diff==0)
buzPeriod=1000000;
long unsigned int cTime=millis(); else
char buzState=0; buzPeriod=avgsum/(2*abs(diff));
if (cTime<pTime+10) }
{ void applyPulses()
if (diff>0) {
buzState=1; for (int i=0;i<3;i++)
else if(diff<0) {
buzState=2; digitalWrite(pulsePin,HIGH); //take
} 3.5 uS
if (cTime>pTime+buzPeriod) delayMicroseconds(3);
{ digitalWrite(pulsePin,LOW); //take
if (diff>0) 3.5 uS
buzState=1; delayMicroseconds(3);
else if (diff<0) }
buzState=2; }
pTime=cTime;
}

Required
Components: 5. The 1k resistor
1. Arduino (any) 6. 330-ohm resistor
2. Coil 7. LED
3. 10nF capacitor 8. 1N4148 diode
4. Buzzer
Components: 5. The 1k resistor
1. Arduino (any) 6. 330-ohm resistor
2. Coil 7. LED
3. 10nF capacitor 8. 1N4148 diode
4. Buzzer
ino Metal
etector
Arduino Nano Pinout
it Diagram

Mechanical Drawing
Arduino Metal Detector
(parts)

resistor 9. Breadboard or PCB


m resistor
10. Connecting Jumper
Wire
diode 11. 9v Battery
resistor 9. Breadboard or PCB
m resistor
10. Connecting Jumper
Wire
diode 11. 9v Battery