Hi everyone, so I'm very new to the world of Arduino, have used them in the past but basically don't remember anything about programming or adding things in. Anyway I'm building a basic metal detector just for the sh#ts and giggles. Within the code the sensitivity is a set parameter however it can be adjusted, I've also from research come to find that the sensitivity can be adjusted using a potentiometer and the analogueread and map functions. My question is where and what would I add into the code to get a potentiometer to vary the sensitivity. I understand the basic idea behind it the change in the voltage would change the value but I wouldn't have a clue how to change it from a physical analogue value to digital. Any help would be very much appreciated.
// Arduino based metal detector
// (C)Dzl july 2013
// http://dzlsevilgeniuslair.blogspot.dk/
// Connect search coil oscillator (20-200kHz) to pin 5
// Connect piezo between pin 13 and GND
// Connect NULL button between pin 12 anf GND
// REMEMBER TO PRESS NULL BUTTON AFTER POWER UP!!
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+
unsigned long t0=0; //-Last time
int t=0; //-time between ints
unsigned char tflag=0; //-Measurement ready flag
float SENSITIVITY= 1000.0; //-Guess what
//-Generate interrupt every 1000 oscillations of the search coil
SIGNAL(TIMER1_COMPA_vect)
{
OCR1A+=1000;
t=micros()-t0;
t0+=t;
tflag=1;
}
void setup()
{
pinMode(13,OUTPUT); //-piezo pin
digitalWrite(12,HIGH); //-NULL SW. pull up
//-Set up counter1 to count at pin 5
TCCR1A=0;
TCCR1B=0x07;
SET(TIMSK1,OCF1A);
}
//-Float ABS
float absf(float f)
{
if(f<0.0)
return -f;
else
return f;
}
int v0=0; //-NULL value
float f=0; //-Measurement value
unsigned int FTW=0; //-Click generator rate
unsigned int PCW=0; //-Click generator phase
unsigned long timer=0; //-Click timer
void loop()
{
if(tflag)
{
if(digitalRead(12)==LOW) //-Check NULL SW.
v0=t; //-Sample new null value
f=f*0.9+absf(t-v0)*0.1; //-Running average over 10 samples
tflag=0; //-Reset flag
float clf=f*SENSITIVITY; //-Convert measurement to click frequency
if(clf>10000)
clf=10000;
FTW=clf;
}
//-Click generator
if(millis()>timer)
{
timer+=10;
PCW+=FTW;
if(PCW&0x8000)
{
digitalWrite(13,HIGH);
PCW&=0x7fff;
}
else
digitalWrite(13,LOW);
}
}
I've put in what I think will work, compiles fine but seeing as I don't have the circuit ready yet just wanted to see if there was anything that needs adding, this is the revised code with the analogread in it, just not sure if its correct.
// Arduino based metal detector
// (C)Dzl july 2013
// http://dzlsevilgeniuslair.blogspot.dk/
// Connect search coil oscillator (20-200kHz) to pin 5
// Connect piezo between pin 13 and GND
// Connect NULL button between pin 12 anf GND
// REMEMBER TO PRESS NULL BUTTON AFTER POWER UP!!
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+
unsigned long t0=0; //-Last time
int t=0; //-time between ints
unsigned char tflag=0; //-Measurement ready flag
float SENSITIVITY= 1000.0; //-Guess what
//-Generate interrupt every 1000 oscillations of the search coil
SIGNAL(TIMER1_COMPA_vect)
{
OCR1A+=1000;
t=micros()-t0;
t0+=t;
tflag=1;
}
int analogpin = 3;
int val=0;
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT); //-piezo pin
digitalWrite(12,HIGH); //-NULL SW. pull up
//-Set up counter1 to count at pin 5
TCCR1A=0;
TCCR1B=0x07;
SET(TIMSK1,OCF1A);
}
//-Float ABS
float absf(float f)
{
if(f<0.0)
return -f;
else
return f;
}
int v0=0; //-NULL value
float f=0; //-Measurement value
unsigned int FTW=0; //-Click generator rate
unsigned int PCW=0; //-Click generator phase
unsigned long timer=0; //-Click timer
void loop()
{
if(tflag)
{
if(digitalRead(12)==LOW) //-Check NULL SW.
v0=t; //-Sample new null value
f=f*0.9+absf(t-v0)*0.1; //-Running average over 10 samples
tflag=0; //-Reset flag
float clf=f*SENSITIVITY; //-Convert measurement to click frequency
if(clf>10000)
clf=10000;
FTW=clf;
val = analogRead(3);
Serial.println(1000);
}
//-Click generator
if(millis()>timer)
{
timer+=10;
PCW+=FTW;
if(PCW&0x8000)
{
digitalWrite(13,HIGH);
PCW&=0x7fff;
}
else
digitalWrite(13,LOW);
}
}
I've put in what I think will work, compiles fine but seeing as I don't have the circuit ready yet just wanted to see if there was anything that needs adding
You are reading a value from analog pin 3. I presume that that is the pin that the potentiometer is attached to. You never use that value, though, so reading it was rather pointless, wasn't it?
All capital letter names, like SENSITIVITY, are reserved for constants. Constants are not modified by the code. You seem to have the idea that you will modify SENSITIVITY in the code, so you need to change the name.
Yes pin 3 is the one the potentiometer is connected to. Thats why I'm asking what needs to be added as I know I'm not using the read value, I'm currently using a set value. That is why I'm asking what I should do to utilize the potentiometer value instead of having it as a constant.
Ok I see where you were going, needed to use the voltage within the float to adjust my values instead of having a set value of Sensitivity. I changed it, compiles well so now I'll just have to test it out, trial and error. Thanks
// Arduino based metal detector
// (C)Dzl july 2013
// http://dzlsevilgeniuslair.blogspot.dk/
// Connect search coil oscillator (20-200kHz) to pin 5
// Connect piezo between pin 13 and GND
// Connect NULL button between pin 12 anf GND
// REMEMBER TO PRESS NULL BUTTON AFTER POWER UP!!
#define SET(x,y) (x |=(1<<y)) //-Bit set/clear macros
#define CLR(x,y) (x &= (~(1<<y))) // |
#define CHK(x,y) (x & (1<<y)) // |
#define TOG(x,y) (x^=(1<<y)) //-+
unsigned long t0=0; //-Last time
int t=0; //-time between ints
unsigned char tflag=0; //-Measurement ready flag
//-Generate interrupt every 1000 oscillations of the search coil
SIGNAL(TIMER1_COMPA_vect)
{
OCR1A+=1000;
t=micros()-t0;
t0+=t;
tflag=1;
}
int analogpin = 3;
int val=0;
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT); //-piezo pin
digitalWrite(12,HIGH); //-NULL SW. pull up
//-Set up counter1 to count at pin 5
TCCR1A=0;
TCCR1B=0x07;
SET(TIMSK1,OCF1A);
}
//-Float ABS
float absf(float f)
{
if(f<0.0)
return -f;
else
return f;
}
int v0=0; //-NULL value
float f=0; //-Measurement value
unsigned int FTW=0; //-Click generator rate
unsigned int PCW=0; //-Click generator phase
unsigned long timer=0; //-Click timer
void loop()
{
if(tflag)
{
if(digitalRead(12)==LOW) //-Check NULL SW.
v0=t; //-Sample new null value
f=f*0.9+absf(t-v0)*0.1; //-Running average over 10 samples
tflag=0; //-Reset flag
int sensorValue = analogRead(A3);
float voltage = sensorValue * (5.0/1023.0);
Serial.println(voltage);
float clf=f*voltage; //-Convert measurement to click frequency
if(clf>voltage)
clf=voltage;
FTW=clf;
val = analogRead(3);
Serial.println(voltage);
}
//-Click generator
if(millis()>timer)
{
timer+=10;
PCW+=FTW;
if(PCW&0x8000)
{
digitalWrite(13,HIGH);
PCW&=0x7fff;
}
else
digitalWrite(13,LOW);
}
}