Hello Arduino community!
Please, if this is the wrong section to post this let me know, since it's the first time I post here!
So!
I've made this piece of code:
int frek = 9; // Variable
int cnt; // Line Numbers
unsigned long frekvens; // Variable
void setup()
{
pinMode(frek, INPUT); // Set pin 10 as INPUT
Serial.begin(9600); // Connect to Serial Port
Serial.println("Frequency Counter"); // Headline
}
void loop()
{
frekvens = digitalRead(frek);
Serial.print(cnt++);
Serial.print(" Frekvens: ");
Serial.print(frekvens);
Serial.print("\n");
delay(500);
}
And it can count HIGH and LOW.
But I need it to count frequency. The actual numeric value. Not just 1 and 0. I use a Duaminova (spelling?) board. It's a digital input. I am a novice at Arduino so please be as noob-friendly as possible in your guidance!
The circuit is run by the Arduino boards 5v pin.
And I do use pin 5.
It just keeps returning 0
EDIT:
Can you help me make it count how many 1's it gets in per second and then display that as a number as I think that would be what I want it to do. I know it's a math formular that needs to be written:
Something like...for every x number of counts per second do print. Or something like that =/
I don't know if you guys know Lua.
But in Lua you can do stuff like what I try to do in the code below. Everytime the Digitalread recieves a 1 it should store this value in the array Pulses and it should count pulses every 100 ms for 1 second. That should give 10 pulses. It should take this and print it since that would be the frequency.
Can you guys help me with that?
If I run this piece of code it just starts counting random numbers which just goes sky high in no time
int frek = 5; // Variable
int cnt; // Line Numbers for readability
unsigned long frekvens; // Variable
void setup()
{
pinMode(frek, INPUT);
Serial.begin(9600); // Connect to Serial Port
Serial.println("Frequency Counter"); // Headline
}
int Pulses[] = {};
void loop()
{
frekvens = digitalRead(frek);
for (frekvens = 1; frekvens > 0;) {
Serial.print(cnt++);
Serial.print(" Frekvens: ");
Serial.print(Pulses[0]);
Serial.println(frekvens);
delay(1000);
}
}
To calculate frequency, you must sample the number of transitions in a fixed period of time, divide by two (if you're sampling both transitions), and divide by the sample period.
I'll ask again - what sort of frequency range are you trying to measure?
World of Warcraft for example.
Especially various games use a Lua API because it doesn't need to be compiled and a simple restart of the application can apply changes you've done to your lua files rather than compiling the whole thing again.
This is not really relevant to my question =/
If I use digialRead(); to get the 1's and then find out how many of those appears at a given time (say 1 second) then take that number and divide it by 2 etc..how would I write that into the Arduino?
Let me ask how you would implement a frequency counter in this scripting language.
Set a peg in the ground "now".
Reset counter.
Has 100ms elapsed since "now"?
No: is the line state HIGH?
Yes: was the last state low?
Yes:increment counter.
Yes (100ms has elapsed): calculate frequency.
(I don't know what a World of Warcraft is either!)
What you say I should do I've always known that I should do it that way. I just don't know how to express that in code. That's the problem. I am a bit novice at this.
unsigned long now = millis (); //Set a peg in the ground "now".
bool lastState = digitalRead (inputPin);
int counter = 0; // Reset counter.
while ((millis () - now > 100)){ // Has 100ms elapsed since "now"?
bool currentState = digitalRead (inputPin);
if (currentState) { //No: is the line state HIGH?
if (!lastState) { //Yes: was the last state low?
counter++; // Yes:increment counter.
}
}
lastState = currentState;
}
//(100ms has elapsed): calculate frequency.
Could you post the Lua (?) solution, please?
I've never heard of it either (I'm a systems programmer, and games rarely impinge on my world), and I'd be interested in the contrast.
In function 'void loop()':
error: 'currentState' was not declared in this scope
int inPin = 5;
int cnt = 0;
float frequency = 0.0;
void setup()
{
pinMode(inPin, INPUT);
Serial.begin(9600);
Serial.println("Frequency Counter");
}
long unsigned expiredTime = 0;
void loop()
{
unsigned long now = millis (); //Set a peg in the ground "now".
bool lastState = digitalRead (inPin);
int counter = 0; // Reset counter.
while ((millis () - now > 100)) // Has 100ms elapsed since "now"?
bool currentState = digitalRead(inPin);
if (currentState) { //No: is the line state HIGH?
if (!lastState) { //Yes: was the last state low?
counter++; // Yes:increment counter.
}
}
lastState = currentState;
}
//(100ms has elapsed): calculate frequency.
When this is over I will gladly try to write a lua approach.