// Arduino Five Coil Metal Detector /* V21 - Production Version with dynamic thresholds and auto calibrate, Adjusted LED Brightness to maximum */ int ledthreshold1 = 40; int ledthreshold2 = 40; int ledthreshold3 = 40; int ledthreshold4 = 40; int ledthreshold5 = 40; int threshdiff = 15; // Add to average to become specific threshold where LED will light (Changed V21 from 25 to 20) long pulse1 = 0; // Stores the length of pulses in microseconds long pulse2 = 0; // Stores the length of pulses in microseconds long pulse3 = 0; // Stores the length of pulses in microseconds long pulseav = 0; // Stores value of output for calibration int minthresh = 20; // Minimum threshold for baseline reading int startdelay = 700; // Start at this value and work down for calibration long pcounterA; // Counts the number of pulses in each cycle long pcounterB; // Counts the number of pulses in each cycle long pcounterC; // Counts the number of pulses in each cycle long pcounterD; // Counts the number of pulses in each cycle long pcounterE; // Counts the number of pulses in each cycle int dly1 = 300; // Delay before sampling pulsewidth A int dly2 = 300; // Delay before sampling pulsewidth B int dly3 = 300; // Delay before sampling pulsewidth C int dly4 = 300; // Delay before sampling pulsewidth D int dly5 = 300; // Delay before sampling pulsewidth E void setup() { Serial.begin(115200); // Setupserial interface for test data outputs pinMode(2,INPUT); // Calibrate Button pinMode(3,OUTPUT); // Pulse output from D3 Arduino pinMode(4,INPUT); // Signal from LM339 in read by D4 on Arduino pinMode(5,OUTPUT); // Pulse output from D5 Arduino pinMode(6,INPUT); // Signal from LM339 in read by D6 on Arduino pinMode(7,OUTPUT); // Pulse output from D5 Arduino pinMode(8,INPUT); // Signal from LM339 in read by D6 on Arduino pinMode(9,OUTPUT); // Pulse output from D5 Arduino pinMode(10,INPUT); // Signal from LM339 in read by D6 on Arduino pinMode(11,OUTPUT); // Pulse output from D5 Arduino pinMode(12,INPUT); // Signal from LM339 in read by D6 on Arduino pinMode(14,OUTPUT); // LED output to indicate a find on A0 pinMode(15,OUTPUT); // LED output to indicate a find on A1 pinMode(16,OUTPUT); // LED output to indicate a find on A2 pinMode(17,OUTPUT); // LED output to indicate a find on A3 pinMode(18,OUTPUT); // LED output to indicate a find on A4 } void loop() { // Reset the pulse counter to zero for this cycle of waveform pcounterA = 0; pcounterB = 0; pcounterC = 0; pcounterD = 0; pcounterE = 0; // Auto Calibrate Coil Sensitivity if button pushed if((digitalRead(2) == LOW) ){ delay(100); // Debounce by waiting then checking again if((digitalRead(2) == LOW) ){ // Carry out operation calibrate(); // Sets individual dly variable } } // Pulse and read Coil A three times --------------------------------------------- digitalWrite(7,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(7,LOW); // Set low on output pin delayMicroseconds(dly1); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterA = pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20); digitalWrite(7,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(7,LOW); // Set low on output pin delayMicroseconds(dly1); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterA = pcounterA + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20); digitalWrite(7,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(7,LOW); // Set low on output pin delayMicroseconds(dly1); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterA = pcounterA + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20); // Pulse and read Coil B three times --------------------------------------------- digitalWrite(5,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(5,LOW); // Set low on output pin delayMicroseconds(dly2); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterB = pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20); digitalWrite(5,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(5,LOW); // Set low on output pin delayMicroseconds(dly2); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterB = pcounterB + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20); digitalWrite(5,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(5,LOW); // Set low on output pin delayMicroseconds(dly2); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterB = pcounterB + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20); // Pulse and read Coil C three times --------------------------------------------- digitalWrite(11,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(11,LOW); // Set low on output pin delayMicroseconds(dly3); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterC = pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20); digitalWrite(11,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(11,LOW); // Set low on output pin delayMicroseconds(dly3); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterC = pcounterC + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20); digitalWrite(11,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(11,LOW); // Set low on output pin delayMicroseconds(dly3); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterC = pcounterC + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20); // Pulse and read Coil D three times --------------------------------------------- digitalWrite(3,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(3,LOW); // Set low on output pin delayMicroseconds(dly4); // Wait before reading pulses pcounterD = pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20); digitalWrite(3,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(3,LOW); // Set low on output pin delayMicroseconds(dly4); // Wait before reading pulses pcounterD = pcounterD + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20); digitalWrite(3,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(3,LOW); // Set low on output pin delayMicroseconds(dly4); // Wait before reading pulses pcounterD = pcounterD + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20); // Pulse and read Coil E three times --------------------------------------------- digitalWrite(9,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(9,LOW); // Set low on output pin delayMicroseconds(dly5); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterE = pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20); digitalWrite(9,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(9,LOW); // Set low on output pin delayMicroseconds(dly5); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterE = pcounterE + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20); digitalWrite(9,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(9,LOW); // Set low on output pin delayMicroseconds(dly5); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pcounterE = pcounterE + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20); /* Serial.print(pcounterA); Serial.print(" "); Serial.print(pcounterB); Serial.print(" "); Serial.print(pcounterC); Serial.print(" "); Serial.print(pcounterD); Serial.print(" "); Serial.print(pcounterE); // Show delay variables Serial.print(" "); Serial.print(dly1); Serial.print(" "); Serial.print(dly2); Serial.print(" "); Serial.print(dly3); Serial.print(" "); Serial.print(dly4); Serial.print(" "); Serial.print(dly5); Serial.print(" "); Serial.print(pulse1); Serial.print(" "); Serial.print(pulse2); Serial.print(" "); Serial.print(pulse3); Serial.print(" "); Serial.println(pulseav); */ // Display results digitalWrite(14,LOW); // Set the output pin to Low to deactivate the LED if(pcounterA < ledthreshold1) { digitalWrite(14,HIGH); // Set the output pin to high to activate the LED } digitalWrite(15,LOW); // Set the output pin to Low to deactivate the LED if(pcounterB < ledthreshold2) { digitalWrite(15,HIGH); // Set the output pin to high to activate the LED } digitalWrite(16,LOW); // Set the output pin to Low to deactivate the LED if(pcounterC < ledthreshold3) { digitalWrite(16,HIGH); // Set the output pin to high to activate the LED } digitalWrite(17,LOW); // Set the output pin to Low to deactivate the LED if(pcounterD < ledthreshold4) { digitalWrite(17,HIGH); // Set the output pin to high to activate the LED } digitalWrite(18,LOW); // Set the output pin to Low to deactivate the LED if(pcounterE < ledthreshold5) { digitalWrite(18,HIGH); // Set the output pin to high to activate the LED } /* delay(10); // Sets up LED brightness // Turn off all LEDs digitalWrite(14,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(15,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(16,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(17,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(18,LOW); // Set the output pin to Low to deactivate the LED */ } // Subroutines void calibrate() { // Turn off all LEDs digitalWrite(14,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(15,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(16,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(17,LOW); // Set the output pin to Low to deactivate the LED digitalWrite(18,LOW); // Set the output pin to Low to deactivate the LED // Calibrate Coil A delay threshold ------------------------------------------------------------ dly1 = startdelay; // Start with very high threshold and work down in steps of 10 pulseav = 0; while (pulseav <= minthresh ){ digitalWrite(7,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(7,LOW); // Set low on output pin delayMicroseconds(dly1); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse1 = pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20); digitalWrite(7,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(7,LOW); // Set low on output pin delayMicroseconds(dly1); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse2 = pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20); digitalWrite(7,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(7,LOW); // Set low on output pin delayMicroseconds(dly1); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse3 = pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20)+ pulseIn(8,HIGH,20) + pulseIn(8,HIGH,20); pulseav = (pulse1 + pulse2 + pulse3)/3; // Decrement threshold if output too low if(pulseav < minthresh) { dly1 = dly1 - 10; } } // Flash LED to prove something done digitalWrite(14,HIGH); // Set the output pin to high to activate the LED delay(5); digitalWrite(14,LOW); // Set the output pin to high to activate the LED ledthreshold1 = pulseav + threshdiff; // Calibrate Coil B delay threshold ------------------------------------------------------------ dly2 = startdelay; // Start with very high threshold and work down in steps of 10 pulseav = 0; while (pulseav <= minthresh ){ digitalWrite(5,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(5,LOW); // Set low on output pin delayMicroseconds(dly2); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse1 = pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20); digitalWrite(5,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(5,LOW); // Set low on output pin delayMicroseconds(dly2); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse2 = pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20); digitalWrite(5,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(5,LOW); // Set low on output pin delayMicroseconds(dly2); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse3 = pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20)+ pulseIn(6,HIGH,20) + pulseIn(6,HIGH,20); pulseav = (pulse1 + pulse2 + pulse3)/3; // Decrement threshold if output too low if(pulseav < minthresh) { dly2 = dly2 - 10; } } // Flash LED to prove something done digitalWrite(15,HIGH); // Set the output pin to high to activate the LED delay(5); digitalWrite(15,LOW); // Set the output pin to high to activate the LED ledthreshold2 = pulseav + threshdiff; // Calibrate Coil C delay threshold ------------------------------------------------------------ dly3 = startdelay; // Start with very high threshold and work down in steps of 10 pulseav = 0; while (pulseav <= minthresh ){ digitalWrite(11,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(11,LOW); // Set low on output pin delayMicroseconds(dly3); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse1 = pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20); digitalWrite(11,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(11,LOW); // Set low on output pin delayMicroseconds(dly3); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse2 = pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20); digitalWrite(11,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(11,LOW); // Set low on output pin delayMicroseconds(dly3); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse3 = pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20)+ pulseIn(12,HIGH,20) + pulseIn(12,HIGH,20); pulseav = (pulse1 + pulse2 + pulse3)/3; // Decrement threshold if output too low if(pulseav < minthresh) { dly3 = dly3 - 10; } } // Flash LED to prove something done digitalWrite(16,HIGH); // Set the output pin to high to activate the LED delay(5); digitalWrite(16,LOW); // Set the output pin to high to activate the LED ledthreshold3 = pulseav + threshdiff; // Calibrate Coil D delay threshold ------------------------------------------------------------ dly4 = startdelay; // Start with very high threshold and work down in steps of 10 pulseav = 0; while (pulseav <= minthresh ){ digitalWrite(3,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(3,LOW); // Set low on output pin delayMicroseconds(dly4); // Wait before reading pulses pulse1 = pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20); digitalWrite(3,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(3,LOW); // Set low on output pin delayMicroseconds(dly4); // Wait before reading pulses pulse2 = pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20); digitalWrite(3,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(3,LOW); // Set low on output pin delayMicroseconds(dly4); // Wait before reading pulses pulse3 = pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20)+ pulseIn(4,HIGH,20) + pulseIn(4,HIGH,20); pulseav = (pulse1 + pulse2 + pulse3)/3; // Decrement threshold if output too low if(pulseav < minthresh) { dly4 = dly4 - 10; } } // Flash LED to prove something done digitalWrite(17,HIGH); // Set the output pin to high to activate the LED delay(5); digitalWrite(17,LOW); // Set the output pin to high to activate the LED ledthreshold4 = pulseav + threshdiff; // Calibrate Coil E delay threshold ------------------------------------------------------------ dly5 = startdelay; // Start with very high threshold and work down in steps of 10 pulseav = 0; while (pulseav <= minthresh ){ digitalWrite(9,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(9,LOW); // Set low on output pin delayMicroseconds(dly5); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse1 = pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20); digitalWrite(9,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(9,LOW); // Set low on output pin delayMicroseconds(dly5); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse2 = pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20); digitalWrite(9,HIGH); // Set the output pin to high delayMicroseconds(5000); // Delay 5000 microseconds before setting low on output pin digitalWrite(9,LOW); // Set low on output pin delayMicroseconds(dly5); // Wait before reading pulses // Measure the number of pulses in each cycle produced by the LC circuit pulse2 = pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20)+ pulseIn(10,HIGH,20) + pulseIn(10,HIGH,20); pulseav = (pulse1 + pulse2 + pulse3)/3; // Decrement threshold if output too low if(pulseav < minthresh) { dly5 = dly5 - 10; } } // Flash LED to prove something done digitalWrite(18,HIGH); // Set the output pin to high to activate the LED delay(5); digitalWrite(18,LOW); // Set the output pin to high to activate the LED ledthreshold5 = pulseav + threshdiff; }