ellisgl

Arduino Metal Detector

Aug 18th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.77 KB | None | 0 0
  1. /**
  2.  * Arduino Five Coil Metal Detector  
  3.  * V21 - Production Version with dynamic thresholds and auto calibrate, Adjusted LED Brightness to maximum
  4.  */
  5. int ledthreshold1 = 40;
  6. int ledthreshold2 = 40;
  7. int ledthreshold3 = 40;
  8. int ledthreshold4 = 40;
  9. int ledthreshold5 = 40;
  10. int threshdiff    = 15;  // Add to average to become specific threshold where LED will light (Changed V21 from 25 to 20)
  11. long pulse1       = 0;   // Stores the length of pulses in microseconds  
  12. long pulse2       = 0;   // Stores the length of pulses in microseconds
  13. long pulse3       = 0;   // Stores the length of pulses in microseconds
  14. long pulseav      = 0;   // Stores value of output for calibration
  15. int minthresh     = 20;  // Minimum threshold for baseline reading
  16. int startdelay    = 700; // Start at this value and work down for calibration
  17. int dly1          = 300; // Delay before sampling pulsewidth A
  18. int dly2          = 300; // Delay before sampling pulsewidth B
  19. int dly3          = 300; // Delay before sampling pulsewidth C
  20. int dly4          = 300; // Delay before sampling pulsewidth D
  21. int dly5          = 300; // Delay before sampling pulsewidth E
  22. long pcounterA;          // Counts the number of pulses in each cycle
  23. long pcounterB;          // Counts the number of pulses in each cycle
  24. long pcounterC;          // Counts the number of pulses in each cycle
  25. long pcounterD;          // Counts the number of pulses in each cycle
  26. long pcounterE;          // Counts the number of pulses in each cycle
  27.  
  28. void setup()
  29. {
  30.     Serial.begin(115200); // Setupserial interface for test data outputs
  31.  
  32.     // Configure Inputs
  33.     pinMode(2,  INPUT);   // Calibrate Button
  34.     pinMode(4,  INPUT);   // Signal from LM339 in read by D4 on Arduino
  35.     pinMode(6,  INPUT);   // Signal from LM339 in read by D6 on Arduino
  36.     pinMode(8,  INPUT);   // Signal from LM339 in read by D6 on Arduino
  37.     pinMode(10, INPUT);   // Signal from LM339 in read by D6 on Arduino
  38.     pinMode(12, INPUT);   // Signal from LM339 in read by D6 on Arduino
  39.  
  40.     // Configure outputs
  41.     pinMode(3,  OUTPUT);  // Pulse output from D3 Arduino
  42.     pinMode(5,  OUTPUT);  // Pulse output from D5 Arduino
  43.     pinMode(7,  OUTPUT);  // Pulse output from D5 Arduino
  44.     pinMode(9,  OUTPUT);  // Pulse output from D5 Arduino
  45.     pinMode(11, OUTPUT);  // Pulse output from D5 Arduino
  46.     pinMode(14, OUTPUT);  // LED output to indicate a find on A0
  47.     pinMode(15, OUTPUT);  // LED output to indicate a find on A1
  48.     pinMode(16, OUTPUT);  // LED output to indicate a find on A2
  49.     pinMode(17, OUTPUT);  // LED output to indicate a find on A3
  50.     pinMode(18, OUTPUT);  // LED output to indicate a find on A4
  51. }
  52.  
  53. void loop()
  54. {
  55.     // Reset the pulse counter to zero for this cycle of waveform
  56.     pcounterA = 0;
  57.     pcounterB = 0;
  58.     pcounterC = 0;
  59.     pcounterD = 0;
  60.     pcounterE = 0;
  61.  
  62.     // Auto Calibrate Coil Sensitivity if button pushed
  63.     if(digitalRead(2) == LOW)
  64.     {
  65.         delay(100); // Debounce by waiting then checking again
  66.        
  67.         if(digitalRead(2) == LOW)
  68.         {
  69.             // Carry out operation
  70.             // Sets individual dly variable
  71.             // Calibrate Coil A delay threshold
  72.             calibrate(7,   8, 14, ledthreshold1, dly1, 5000);
  73.  
  74.             // Calibrate Coil B delay threshold
  75.             calibrate(5,   6, 15, ledthreshold2, dly2, 5000);
  76.  
  77.             // Calibrate Coil C delay threshold
  78.             calibrate(11, 12, 16, ledthreshold3, dly3, 5000);
  79.  
  80.             // Calibrate Coil D delay threshold
  81.             calibrate(3,   4, 17, ledthreshold4, dly4, 5000);
  82.  
  83.             // Calibrate Coil E delay threshold
  84.             calibrate(9,  10, 18, ledthreshold5, dly5, 5000);            
  85.         }
  86.     }
  87.  
  88.     // Pulse and read Coil A three times
  89.     PRCoil(7,   8, pcounterA, dly1, 5000, 3);
  90.    
  91.     // Pulse and read Coil B three times
  92.     PRCoil(5,   6, pcounterB, dly2, 5000, 3);
  93.    
  94.     // Pulse and read Coil C three times
  95.     PRCoil(11, 12, pcounterC, dly3, 5000, 3);
  96.  
  97.     // Pulse and read Coil D three times
  98.     PRCoil(3,   4, pcounterD, dly4, 5000, 3);
  99.    
  100.     // Pulse and read Coil E three times
  101.     PRCoil(9,  10, pcounterE, dly5, 5000, 3);
  102.  
  103.     /**
  104.     Serial.print(pcounterA);
  105.     Serial.print(" ");
  106.     Serial.print(pcounterB);
  107.     Serial.print(" ");
  108.     Serial.print(pcounterC);
  109.     Serial.print(" ");
  110.     Serial.print(pcounterD);
  111.     Serial.print(" ");
  112.     Serial.print(pcounterE);
  113.  
  114.     // Show delay variables
  115.     Serial.print("      ");
  116.     Serial.print(dly1);
  117.     Serial.print(" ");
  118.     Serial.print(dly2);
  119.     Serial.print(" ");
  120.     Serial.print(dly3);
  121.     Serial.print(" ");
  122.     Serial.print(dly4);
  123.     Serial.print(" ");
  124.     Serial.print(dly5);
  125.  
  126.     Serial.print("      ");
  127.     Serial.print(pulse1);
  128.     Serial.print(" ");
  129.     Serial.print(pulse2);
  130.     Serial.print(" ");
  131.     Serial.print(pulse3);
  132.     Serial.print(" ");
  133.     Serial.println(pulseav);
  134.     // */
  135.  
  136.     // Display results
  137.     digitalWrite(14, LOW); // Set the output pin to Low to deactivate the LED
  138.  
  139.     if(pcounterA < ledthreshold1)
  140.     {
  141.         digitalWrite(14, HIGH); // Set the output pin to high to activate the LED
  142.     }
  143.  
  144.     digitalWrite(15,LOW); // Set the output pin to Low to deactivate the LED
  145.  
  146.     if(pcounterB < ledthreshold2)
  147.     {
  148.         digitalWrite(15, HIGH); // Set the output pin to high to activate the LED
  149.     }
  150.  
  151.     digitalWrite(16, LOW); // Set the output pin to Low to deactivate the LED
  152.  
  153.     if(pcounterC < ledthreshold3)
  154.     {
  155.         digitalWrite(16, HIGH); // Set the output pin to high to activate the LED
  156.     }
  157.  
  158.     digitalWrite(17, LOW); // Set the output pin to Low to deactivate the LED
  159.  
  160.     if(pcounterD < ledthreshold4)
  161.     {
  162.         digitalWrite(17, HIGH); // Set the output pin to high to activate the LED
  163.     }
  164.  
  165.     digitalWrite(18, LOW); // Set the output pin to Low to deactivate the LED
  166.  
  167.     if(pcounterE < ledthreshold5)
  168.     {
  169.         digitalWrite(18, HIGH); // Set the output pin to high to activate the LED
  170.     }
  171.  
  172.     /**
  173.     delay(10);  // Sets up LED brightness
  174.     // Turn off all LEDs
  175.     digitalWrite(14, LOW); // Set the output pin to Low to deactivate the LED
  176.     digitalWrite(15, LOW); // Set the output pin to Low to deactivate the LED
  177.     digitalWrite(16, LOW); // Set the output pin to Low to deactivate the LED
  178.     digitalWrite(17, LOW); // Set the output pin to Low to deactivate the LED
  179.     digitalWrite(18, LOW); // Set the output pin to Low to deactivate the LED
  180.     // */
  181. }
  182.  
  183.  
  184. // Subroutines
  185. /**
  186.  * Pulse and read a set of coils with read delay, write low delay and how many times to do it.
  187.  *
  188.  * int tx: pin number for transmit coil
  189.  * int rx: pin number for receive coil
  190.  * long &counter: variable called by refence for counting purposes
  191.  * int rDelay: Microseconds before reading back the pulses
  192.  * int lDelay: Delay before setting tx to low
  193.  * int times: Number of time to repeat this.
  194.  *
  195.  * Usage:
  196.  * PRCoil(7, 8, pcounterA, dly1, 5000, 3);
  197.  */    
  198. void PRCoil(int tx, int rx, long &counter, int rDelay, int lDelay, int times)
  199. {
  200.     // Pulse Coil
  201.     digitalWrite(tx, HIGH);    // Set the output pin to high
  202.     delayMicroseconds(lDelay); // Delay x microseconds before setting low on output pin
  203.     digitalWrite(tx, LOW);     // Set low on output pin
  204.     delayMicroseconds(rDelay); // Wait before reading pulses
  205.    
  206.     // Read the coil and measure the number of pulses in each cycle produced by the LC circuit
  207.     counter = counter + pulseIn(rx, HIGH, 20) + pulseIn(rx, HIGH, 20) + pulseIn(rx, HIGH, 20) + pulseIn(rx, HIGH, 20) + pulseIn(rx, HIGH, 20) + pulseIn(rx, HIGH, 20) + pulseIn(rx, HIGH, 20);
  208.  
  209.     // Rinse and repeat as necessary.
  210.     if(times > 0)
  211.     {
  212.         times--;
  213.         PRCoil(tx, rx, counter, rDelay, lDelay, times);
  214.     }
  215. }
  216.  
  217. void calibrate(int tx, int rx, int led, int &ledthreshold, int &rDelay, int lDelay)
  218. {
  219.     // Turn off LED
  220.     digitalWrite(led, LOW);      // Set the output pin to Low to deactivate the LED
  221.  
  222.     // Calibrate Coil A delay threshold
  223.     rDelay        = startdelay; // Start with very high threshold and work down in steps of 10
  224.     pulseav       = 0;
  225.  
  226.     while (pulseav <= minthresh )
  227.     {
  228.         PRCoil(tx, rx, pulse1, rDelay, 5000, 1);
  229.         PRCoil(tx, rx, pulse2, rDelay, 5000, 1);
  230.         PRCoil(tx, rx, pulse3, rDelay, 5000, 1);
  231.            
  232.         pulseav = (pulse1 + pulse2 + pulse3) / 3;
  233.      
  234.         // Decrement threshold if output too low
  235.         if(pulseav < minthresh)
  236.         {
  237.             rDelay = rDelay - 10;
  238.         }
  239.     }
  240.    
  241.     // Flash LED to prove something done
  242.     digitalWrite(led, HIGH);     // Set the output pin to high to activate the LED
  243.     delay(5);
  244.     digitalWrite(led, LOW);      // Set the output pin to high to activate the LED
  245.  
  246.     ledthreshold = pulseav + threshdiff;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment