 # How do I multiply or divide frequencies which are not constant?

So i have a project that i cant get my head around, I wanted to use an arduino uno to divide an input frequency(which is not constant).
So to test it, i used 2 arduinos, one had this code:

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
tone(13, 200);
}

And the other has this:

int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
int c = 0;
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}

void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}

this is not the correct code for this application(because it doesnt work lol), and i ask if you know how to make it work, by making c a constant(for e.g. 2) and dividing/multiplying val by c

Thanks in advance.

``````int c = 2;
val = val/c;
val = val*c;
``````

ruinobraga007:
So i have a project that i cant get my head around, I wanted to use an arduino uno to divide an input frequency(which is not constant).
So to test it, i used 2 arduinos, one had this code:

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
tone(13, 200);
}

And the other has this:

int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
int c = 0;
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}

void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}

this is not the correct code for this application(because it doesnt work lol), and i ask if you know how to make it work, by making c a constant(for e.g. 2) and dividing/multiplying val by c

Thanks in advance.

So you want to read the frequency on an input pin and generate a waveform on an output pin that is some fraction or multiple of the input frequency?

digitalRead() takes a snapshot of a pin at an instant in time. It doesn't measure the period between rising or falling edges; you'll need code quite a bit more complex than this. You'd want to use a timer's input capture function to measure the period of rising or falling edges on the input and program the output compare of another timer to generate the desired frequency at an output pin.

What Arduino are you using? What is the range of input frequencies?

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.

Can you tell us the application you need this project for?
Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom... 