# LED frequency

Hello

I want to pulse a LED and slowly change the frequency between 8 and 12 Hz.

How would i go about doing this?

You could use delayMicroseconds

Using a "for" loop to calculate a variable delay of turning the LED on and off between ~63ms (8Hz) and ~42ms (12Hz). Assuming a 50% duty cycle.

``````8Hz = ((1000ms / 8) / 2) = 62.5ms
12Hz = ((1000ms / 12) /2) = 41.66ms
``````

Yes, i already figured that part out but how do i go about making it 'fade' between those to frequencies (sorry i'm a total noob)

Use the PWM output (analogWrite) to turn the LED more and more on in the delay loop.

I am sorry but i don't understand a word about what you're trying to say..

'Fade' is just a slight increment/decrement between each iteration until the desired delay is reached

/*
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.
*/

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}

and maybe you want something like this -- But Probably Not as it is Very Uninteresting!

/*
Modified 8 Hz to 12 Hz Blink
Pulses an LED between 8 Hz and very close to 12 Hz (close by 4/3 microsecond) and back repeatedly

This example code is for the public domain.
*/

const unsigned int slowerHz = 8; // note that less than 8 Hz will not work as written!
const unsigned int fasterHz = 12;

// hey the UL does convert into the unsigned ints, I checked
const unsigned long hlp = ((1000000UL / slowerHz) / 2UL); // = 62500
const unsigned long hsp = ((1000000UL / fasterHz) / 2UL); // = 41666 (with 2/3 remainder)
const unsigned int halfLongPulse = hlp; // there is no conversion for unsigned int in the Reference!
const unsigned int halfShortPulse = hsp; // ! well, it compiles and runs!

const unsigned int changeRate = 100; // how fast to change frequencies

const int ledPin = 13; // Pin 13 has an LED connected on most Arduino boards:

void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
/* debug value checks
Serial.begin(9600);
Serial.println( halfLongPulse, DEC );
Serial.println( halfShortPulse, DEC );
Serial.println( "..." );
*/
}

digitalWrite(ledPin, HIGH); // set the LED on
delayMicroseconds(microSecs); // wait for a while
digitalWrite(ledPin, LOW); // set the LED off
delayMicroseconds(microSecs); // wait for a while
}

void loop() {
unsigned int halfPulse = halfLongPulse;

while (halfPulse >= halfShortPulse) {
halfPulse -= changeRate;
// Serial.println( halfPulse, DEC );
}

while (halfPulse <= halfLongPulse) {
halfPulse += changeRate;
// Serial.println( halfPulse, DEC );
}
}

@lyron

What AWOL is saying :

I hope you understand

``````const int mypin = 10;

int timeon=1000; // equal number is 50 % duty
int timeoff=1000; // timeon = timeoff -> 50 % duty
/*
F = 1 /T

T=Ton + Toff

example : 25 % Duty   timeon = T * 25/100.  timeoff=T-timeoff

*/
void setup()
{
pinMode(mypin, OUTPUT);
}

void loop()
{
digitalWrite(mypin, HIGH);
delayMicroseconds(timeon); // change this value for the time on
digitalWrite(mypin, LOW);
delayMicroseconds(timeoff); // change this value for the time off
}
``````