Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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
- 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
- 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
- void setup()
- {
- Serial.begin(115200); // Setupserial interface for test data outputs
- // Configure Inputs
- pinMode(2, INPUT); // Calibrate Button
- pinMode(4, INPUT); // Signal from LM339 in read by D4 on Arduino
- pinMode(6, INPUT); // Signal from LM339 in read by D6 on Arduino
- pinMode(8, INPUT); // Signal from LM339 in read by D6 on Arduino
- pinMode(10, INPUT); // Signal from LM339 in read by D6 on Arduino
- pinMode(12, INPUT); // Signal from LM339 in read by D6 on Arduino
- // Configure outputs
- pinMode(3, OUTPUT); // Pulse output from D3 Arduino
- pinMode(5, OUTPUT); // Pulse output from D5 Arduino
- pinMode(7, OUTPUT); // Pulse output from D5 Arduino
- pinMode(9, OUTPUT); // Pulse output from D5 Arduino
- pinMode(11, OUTPUT); // Pulse output from D5 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
- // Sets individual dly variable
- // Calibrate Coil A delay threshold
- calibrate(7, 8, 14, ledthreshold1, dly1, 5000);
- // Calibrate Coil B delay threshold
- calibrate(5, 6, 15, ledthreshold2, dly2, 5000);
- // Calibrate Coil C delay threshold
- calibrate(11, 12, 16, ledthreshold3, dly3, 5000);
- // Calibrate Coil D delay threshold
- calibrate(3, 4, 17, ledthreshold4, dly4, 5000);
- // Calibrate Coil E delay threshold
- calibrate(9, 10, 18, ledthreshold5, dly5, 5000);
- }
- }
- // Pulse and read Coil A three times
- PRCoil(7, 8, pcounterA, dly1, 5000, 3);
- // Pulse and read Coil B three times
- PRCoil(5, 6, pcounterB, dly2, 5000, 3);
- // Pulse and read Coil C three times
- PRCoil(11, 12, pcounterC, dly3, 5000, 3);
- // Pulse and read Coil D three times
- PRCoil(3, 4, pcounterD, dly4, 5000, 3);
- // Pulse and read Coil E three times
- PRCoil(9, 10, pcounterE, dly5, 5000, 3);
- /**
- 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
- /**
- * Pulse and read a set of coils with read delay, write low delay and how many times to do it.
- *
- * int tx: pin number for transmit coil
- * int rx: pin number for receive coil
- * long &counter: variable called by refence for counting purposes
- * int rDelay: Microseconds before reading back the pulses
- * int lDelay: Delay before setting tx to low
- * int times: Number of time to repeat this.
- *
- * Usage:
- * PRCoil(7, 8, pcounterA, dly1, 5000, 3);
- */
- void PRCoil(int tx, int rx, long &counter, int rDelay, int lDelay, int times)
- {
- // Pulse Coil
- digitalWrite(tx, HIGH); // Set the output pin to high
- delayMicroseconds(lDelay); // Delay x microseconds before setting low on output pin
- digitalWrite(tx, LOW); // Set low on output pin
- delayMicroseconds(rDelay); // Wait before reading pulses
- // Read the coil and measure the number of pulses in each cycle produced by the LC circuit
- 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);
- // Rinse and repeat as necessary.
- if(times > 0)
- {
- times--;
- PRCoil(tx, rx, counter, rDelay, lDelay, times);
- }
- }
- void calibrate(int tx, int rx, int led, int &ledthreshold, int &rDelay, int lDelay)
- {
- // Turn off LED
- digitalWrite(led, LOW); // Set the output pin to Low to deactivate the LED
- // Calibrate Coil A delay threshold
- rDelay = startdelay; // Start with very high threshold and work down in steps of 10
- pulseav = 0;
- while (pulseav <= minthresh )
- {
- PRCoil(tx, rx, pulse1, rDelay, 5000, 1);
- PRCoil(tx, rx, pulse2, rDelay, 5000, 1);
- PRCoil(tx, rx, pulse3, rDelay, 5000, 1);
- pulseav = (pulse1 + pulse2 + pulse3) / 3;
- // Decrement threshold if output too low
- if(pulseav < minthresh)
- {
- rDelay = rDelay - 10;
- }
- }
- // Flash LED to prove something done
- digitalWrite(led, HIGH); // Set the output pin to high to activate the LED
- delay(5);
- digitalWrite(led, LOW); // Set the output pin to high to activate the LED
- ledthreshold = pulseav + threshdiff;
- }
Advertisement
Add Comment
Please, Sign In to add comment