safwan092

metal-detector-coil

Apr 21st, 2021
25
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * Metal detector
  3. * We use the code of this project,
  4. * with minor changes:
  5. * https://www.instructables.com/id/Simple-Arduino-Metal-Detector/
  6. */
  7.  
  8. const byte npulse = 12; // number of pulses to charge the capacitor before each measurement
  9.  
  10. const byte pin_pulse = A0; // sends pulses to charge the capacitor (can be a digital pin)
  11. const byte pin_cap = A1; // measures the capacitor charge
  12. const byte pin_LED = 11; // LED that turns on when metal is detected
  13.  
  14. void setup() {
  15. pinMode(pin_pulse, OUTPUT);
  16. digitalWrite(pin_pulse, LOW);
  17. pinMode(pin_cap, INPUT);
  18. pinMode(pin_LED, OUTPUT);
  19. digitalWrite(pin_LED, LOW);
  20. }
  21.  
  22. const int nmeas = 256; //measurements to take
  23. long int sumsum = 0; //running sum of 64 sums
  24. long int skip = 0; //number of skipped sums
  25. long int diff = 0; //difference between sum and avgsum
  26. long int flash_period = 0; //period (in ms)
  27. long unsigned int prev_flash = 0; //time stamp of previous flash
  28.  
  29. void loop() {
  30.  
  31. int minval = 2000;
  32. int maxval = 0;
  33.  
  34. //perform measurement
  35. long unsigned int sum = 0;
  36. for (int imeas = 0; imeas < nmeas + 2; imeas++) {
  37. //reset the capacitor
  38. pinMode(pin_cap, OUTPUT);
  39. digitalWrite(pin_cap, LOW);
  40. delayMicroseconds(20);
  41. pinMode(pin_cap, INPUT);
  42. //apply pulses
  43. for (int ipulse = 0; ipulse < npulse; ipulse++) {
  44. digitalWrite(pin_pulse, HIGH); //takes 3.5 microseconds
  45. delayMicroseconds(3);
  46. digitalWrite(pin_pulse, LOW); //takes 3.5 microseconds
  47. delayMicroseconds(3);
  48. }
  49. //read the charge on the capacitor
  50. int val = analogRead(pin_cap); //takes 13x8=104 microseconds
  51. minval = min(val, minval);
  52. maxval = max(val, maxval);
  53. sum += val;
  54.  
  55. //determine if LEDs should be on or off
  56. long unsigned int timestamp = millis();
  57. byte ledstat = 0;
  58. if (timestamp < prev_flash +12) {
  59. if (diff > 0)ledstat = 1;
  60. if (diff < 0)ledstat = 2;
  61. }
  62. if (timestamp > prev_flash + flash_period) {
  63. if (diff > 0)ledstat = 1;
  64. if (diff < 0)ledstat = 2;
  65. prev_flash = timestamp;
  66. }
  67. if (flash_period > 1000)ledstat = 0;
  68.  
  69. //switch the LEDs to this setting
  70. if (ledstat == 0) {
  71. digitalWrite(pin_LED, LOW);
  72. }
  73. if (ledstat == 1) {
  74. digitalWrite(pin_LED, LOW);
  75. }
  76. if (ledstat == 2) {
  77. digitalWrite(pin_LED, HIGH);
  78. }
  79.  
  80. }
  81.  
  82. //subtract minimum and maximum value to remove spikes
  83. sum -= minval; sum -= maxval;
  84.  
  85. //process
  86. if (sumsum == 0) sumsum = sum << 6; //set sumsum to expected value
  87. long int avgsum = (sumsum + 32) >> 6;
  88. diff = sum - avgsum;
  89. if (abs(diff)<avgsum >> 10) { //adjust for small changes
  90. sumsum = sumsum + sum - avgsum;
  91. skip = 0;
  92. } else {
  93. skip++;
  94. }
  95. if (skip > 64) { // break off in case of prolonged skipping
  96. sumsum = sum << 6;
  97. skip = 0;
  98. }
  99.  
  100. // one permille change = 2 ticks/s
  101. if (diff == 0) flash_period = 1000000;
  102. else flash_period = avgsum / (2 * abs(diff));
  103. }
RAW Paste Data