Components and supplies
1
Arduino UNO
1
Resistor 221 ohm
Project description
Code
code
csharp
1// Arduino based metal detector 2// (C)Dzl july 2013 3 4// http://dzlsevilgeniuslair.blogspot.dk 5 6/ Connect search coil oscillator (20-200kHz) to pin 5 7 8// Connect piezo between pin 13 and GND 9 10// Connect NULL button between pin 12 anf GND 11 12// REMEMBER TO PRESS NULL BUTTON AFTER POWER UP!! 13 14#define SET(x,y) (x |=(1< 15 16#define CLR(x,y) (x &= (~(1< 17 18#define CHK(x,y) (x & (1< 19 20#define TOG(x,y) (x^=(1< 21 22unsigned long t0=0; //-Last time 23 24int t=0; //-time between ints 25 26unsigned char tflag=0; //-Measurement ready flag 27 28float SENSITIVITY= 1000.0; //-Guess what 29 30//-Generate interrupt every 1000 oscillations of the search coil 31 32SIGNAL(TIMER1_COMPA_vect) 33 34{ 35 36OCR1A+=1000; 37 38t=micros()-t0; 39 40t0+=t; 41 42tflag=1; 43 44} 45 46void setup() 47 48{ 49 50Serial.begin(9600); 51 52pinMode(13,OUTPUT); //-piezo pin 53 54digitalWrite(12,HIGH); //-NULL SW. pull up 55 56//-Set up counter1 to count at pin 5 57 58TCCR1A=0; 59 60TCCR1B=0x07; 61 62SET(TIMSK1,OCF1A); 63 64} 65 66//-Float ABS 67 68float absf(float f) 69 70{ 71 72if(f<0.0) 73 74return -f; 75 76else 77 78return f; 79 80} 81 82int v0=0; //-NULL value 83 84float f=0; //-Measurement value 85 86unsigned int FTW=0; //-Click generator rate 87 88unsigned int PCW=0; //-Click generator phase 89 90unsigned long timer=0; //-Click timer 91 92void loop() 93 94{ 95 96if(tflag) 97 98{ 99 100if(digitalRead(12)==LOW) //-Check NULL SW. 101 102v0=t; //-Sample new null value 103 104f=f*0.9+absf(t-v0)*0.1; //-Running average over 10 samples 105 106tflag=0; //-Reset flag 107 108float clf=f*SENSITIVITY; //-Convert measurement to click frequency 109 110if(clf>10000) 111 112clf=10000; 113 114FTW=clf; 115 116Serial.println(f); 117 118} 119 120//-Click generator 121 122if(millis()>timer) 123 124{ 125 126timer+=10; 127 128PCW+=FTW; 129 130if(PCW&0x8000) 131 132{ 133 134digitalWrite(13,HIGH); 135 136PCW&=0x7fff; 137 138} 139 140else 141 142digitalWrite(13,LOW); 143 144} 145 146}
code
csharp
1// Arduino based metal detector 2// (C)Dzl july 2013 3 4// http://dzlsevilgeniuslair.blogspot.dk 5 6/ 7 Connect search coil oscillator (20-200kHz) to pin 5 8 9// Connect piezo between 10 pin 13 and GND 11 12// Connect NULL button between pin 12 anf GND 13 14// REMEMBER 15 TO PRESS NULL BUTTON AFTER POWER UP!! 16 17#define SET(x,y) (x |=(1< 18 19#define 20 CLR(x,y) (x &= (~(1< 21 22#define CHK(x,y) (x & (1< 23 24#define TOG(x,y) (x^=(1< 25 26unsigned 27 long t0=0; //-Last time 28 29int t=0; //-time between ints 30 31unsigned char 32 tflag=0; //-Measurement ready flag 33 34float SENSITIVITY= 1000.0; //-Guess what 35 36//-Generate 37 interrupt every 1000 oscillations of the search coil 38 39SIGNAL(TIMER1_COMPA_vect) 40 41{ 42 43OCR1A+=1000; 44 45t=micros()-t0; 46 47t0+=t; 48 49tflag=1; 50 51} 52 53void 54 setup() 55 56{ 57 58Serial.begin(9600); 59 60pinMode(13,OUTPUT); //-piezo 61 pin 62 63digitalWrite(12,HIGH); //-NULL SW. pull up 64 65//-Set up counter1 66 to count at pin 5 67 68TCCR1A=0; 69 70TCCR1B=0x07; 71 72SET(TIMSK1,OCF1A); 73 74} 75 76//-Float 77 ABS 78 79float absf(float f) 80 81{ 82 83if(f<0.0) 84 85return -f; 86 87else 88 89return 90 f; 91 92} 93 94int v0=0; //-NULL value 95 96float f=0; //-Measurement value 97 98unsigned 99 int FTW=0; //-Click generator rate 100 101unsigned int PCW=0; //-Click generator 102 phase 103 104unsigned long timer=0; //-Click timer 105 106void loop() 107 108{ 109 110if(tflag) 111 112{ 113 114if(digitalRead(12)==LOW) 115 //-Check NULL SW. 116 117v0=t; //-Sample new null value 118 119f=f*0.9+absf(t-v0)*0.1; 120 //-Running average over 10 samples 121 122tflag=0; //-Reset flag 123 124float clf=f*SENSITIVITY; 125 //-Convert measurement to click frequency 126 127if(clf>10000) 128 129clf=10000; 130 131FTW=clf; 132 133Serial.println(f); 134 135} 136 137//-Click 138 generator 139 140if(millis()>timer) 141 142{ 143 144timer+=10; 145 146PCW+=FTW; 147 148if(PCW&0x8000) 149 150{ 151 152digitalWrite(13,HIGH); 153 154PCW&=0x7fff; 155 156} 157 158else 159 160digitalWrite(13,LOW); 161 162} 163 164}
Downloadable files
code
code

Comments
Only logged in users can leave comments