# how to set a frequency using a push button

hi
im new at Arduino. I am trying to set a frequency set point using a push button. What must happen is the following:
when the frequency reaches a certain frequency i bush a button and then that frequency is now the set frequency.
So each time the frequency goes above the set point a buzzer comes on.
So far i got as far as to read the frequency and to set it using a predifindin the code.
can any one help me please
ivan

Welcome to the world of arduino!

Your explanation is really not enough for us to help you. What we need at least is the code you have so far!

``````Post your code like this, by using the "#" button near the smileys
``````

After that we can really help! Now we don't know what you mean by frequency and how you read it. Is it a analogRead? Is it seral?

Another thing that could help (Not in this case but maybe next time) is an electric circuit of your setup.

Hi
Thank you
code is as follows

``````[/* FreqMeasure
*/
#include <FreqMeasure.h>

void setup() {
Serial.begin(57600);
FreqMeasure.begin();
}

double sum=0;
int count=0;

void loop() {
if (FreqMeasure.available()) {
count = count + 1;
if (count > 30) {
float frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(frequency);
sum = 0;
count = 0;
}
if (count > 14) {                                                      ****Instead of "if (count >14)} ; i want to use a button to set the frequency;
Serial.println("warning over set limit");        **** so that every time it runs over that set frequency a buzzer sounds .  Also if the
}                                                                                **** if the power is removed the set frequency i think we have to write to EEPROM
}
}]
``````

I don't know what sort of thing FreqMeasure.read() returns?
What are you measuring the frequency of?
What is the range of frequencies you might encounter?
Why do you appear to be adding them?
I don't see the relationship (if any) between sum and count?

How will you know when you are at the frequency at which you want to push the button?

Somewhere in the code you will need something like

``````if (digitalRead(buttonPin == LOW && thresholSet == false) { // assume active low
thresholdSet = true; // to avoid problems with the button held down
threshold = frequency;
}
``````

and then

``````if (frequency > threshold) {
// sound buzzer
}
``````

...R

HI
The code i got from the web site * http://www.pjrc.com/teensy/td_libs_FreqMeasure.html; i just added a few lines to test to see if it can work for my appication. The frequency is generated from a PC fan motor (that all i can afford at this stage as im unemployed)
what i actually need is an example using a button to set the frequency, or an example of a tachometer to set an alarm when a certain speed is reached. The button is set to set the speed. So you take the speed up to where you want it then press the button and the speed is set.

Hi, like setting a car cruise control.

Tom..... Hi Tom
yes , but only difference is im using frequency of fan motors
regards
Ivan

ivannair:
The frequency is generated from a PC fan motor

That wasn't really the question I was asking. I was hoping you would tell me what sort of number the function FreqMeasure.read() returns. (Give some examples)

And you didn't answer my other questions.

The code snippets that I posted should point the way to setting the threshold.

...R

hi Robin
Like i said im new at this and trying to work out coding. i have changed my code . i got the lcd to work and to set the frequency manually but not with a button as yet. can you see where im going wrong please

#include <FreqCounter.h>
#include <LiquidCrystal.h>
int set_freq = 0;
const int buttonPin =10; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
LiquidCrystal lcd(2, 3, 4, 6, 7, 8, 9); //Delcares LCD display pins
unsigned long frq; //Declares an extended size variable for the measured frequency
int cnt;

void setup() {
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

lcd.begin(16, 2); //LCD display columns and rows

lcd.print(“FrequencyCounter”); //Prints a splash screen for 2 seconds
lcd.setCursor(0, 1);
lcd.print(" Version 2.1 ");
delay(2000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Frequency /Hz :”); //Prints “Frequency /Hz :”

}

void loop() {

FreqCounter::f_comp=10;
FreqCounter::start(1000);

frq=FreqCounter::f_freq; //Sets the ‘frq’ variable as the measured frequency
lcd.setCursor(0, 1);
lcd.print(frq); //Prints this measured frequency
lcd.print(" ");

if (buttonState == HIGH) {
set_freq;
frq = set_freq;
}
if(set_freq > frq){

digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

Hi,

Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

This will format your code so it is easy to read and has no text artifacts.

Tom... ivannair:
can you see where im going wrong please

If you don't understand any of the questions please say so.

...R

hi Robbin
Thank you , i eventually got the code to work(even with writing and reading the values to eeprom.
I just need help with , trying to read two sets of frequency"s using the same board. The code i used now works well but i dont want to use two boards to read the frequency"s

``````[#include <FreqCounter.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>

LiquidCrystal lcd(2, 3, 4, 6, 7, 8, 9); //Delcares LCD display pins
unsigned long frq; //Declares an extended size variable for the measured frequency
unsigned int set_frq1;

int cnt;
const int buttonPin = 10;     // the number of the pushbutton pin
const int ledPin2 =  11;      // the number of the LED pin
const int ledPin =  12;      // the number of the LED pin
int buttonState = 0;
byte value;

void setup() {

pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);

lcd.begin(16, 2); //LCD display columns and rows

lcd.print("FrequencyCounter"); //Prints a splash screen for 2 seconds
lcd.setCursor(0, 0);
lcd.print("  Version  1.0  ");
delay(2000);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print("MOTOR1 Hz :    "); //Prints "Frequency /Hz :"

}

void loop() {
FreqCounter::f_comp=10;
FreqCounter::start(1000);

frq=FreqCounter::f_freq; //Sets the 'frq' variable as the measured frequency
lcd.setCursor(0, 1);
lcd.print(frq); //Prints this measured frequency
lcd.print("       ");