 # Frequency question ( new to arduino )

Hey, I'm writing this here because I am completely new to arduino and this seems the fastest and best way to get good answers :)

I have some questions, I don't know if you can answer or not, I recently bought an arduino UNO and I want to know, if I can, how to generate output frequencies.

Imagine I need to light a led @ 40 hz and get some digital pin to fire up 400 times a second, how would I do it? Is it possible to do with an arduino?

thanks :) Levi

Perfectly possible. Look at the blink without delay example

Yes, I've seen that, although I don't know how accurate that will be, knowing is based on an if statement. I've been reading this http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM , is there anyone with more knowledge than I that can help me here?

An awful lot of software is based on 'if' statements. What do you mean?

This :

if(currentMillis - previousMillis > interval)

this if statement takes time to compute, besides it is not accurate, if I have a huge list of things being done before or after the if , it will be always true

That is, the program can be programed to work at 40 hz when in fact is working at 30hz or less.

Wouldn’t the pwm be more accurate?

Just grab a scope and adjust the interval value to get 40Hz, pwm's cant go that low, only the 16bit timer can, and that is used to generate the milis if I'm not in mistake.

The Arduino is usually running at a rate of 16MHz, or one instruction every 62.5nanoseconds. 30Hz or even 400Hz is glacial.

Doesn't matter, I can show you an example,

``````const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
``````

This should run at 1 Hz right? Imagine I need to have a delay inside or

that needed to have something inside that code that needs 2 seconds for the arduino to complete? This if will then be always true and the full loop would take 2 seconds plus (62.5nanoseconds * 6) right? Almost 2 Hz no?

Why would you have a 'delay'? That's what you're trying to avoid. That's why it is called the blink without delay example. If all you're doing is flashing a couple of LEDS at low frequecy, what could possibly take two seconds to complete?

Forget the delay, imagine there is something you need to compile in the code that takes a lot of time.

void loop()
{

for (int i = 0; i < x ; i++){
//insert for loop that takes 3 seconds to finish
//just some weird algorithm

}

unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}

You never mentioned that in your original post. So, just turn your algorithm around and do a little bit of it each time through 'loop'.