Go Down

### Topic: LED frequency (Read 1 time)previous topic - next topic

#### lyron

##### Nov 01, 2011, 07:05 pm
Hello

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

How would i go about doing this?

#### AWOL

#1
##### Nov 01, 2011, 07:30 pm
You could use delayMicroseconds
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### decep

#2
##### Nov 01, 2011, 07:32 pm
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.

Code: [Select]
`8Hz = ((1000ms / 8) / 2) = 62.5ms12Hz = ((1000ms / 12) /2) = 41.66ms`

#### lyron

#3
##### Nov 01, 2011, 07:35 pm
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)

#### Grumpy_Mike

#4
##### Nov 01, 2011, 07:37 pm
Use the PWM output (analogWrite) to turn the LED more and more on in the delay loop.

#### lyron

#5
##### Nov 01, 2011, 07:39 pm
I am sorry but i don't understand a word about what you're trying to say..

#### AWOL

#6
##### Nov 01, 2011, 07:45 pm
'Fade' is just a slight increment/decrement between each iteration until the desired delay is reached
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

#### GoForSmoke

#7
##### Nov 02, 2011, 02:39 amLast Edit: Nov 02, 2011, 03:16 am by GoForSmoke Reason: 1

Quote
/*
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!

Quote
/*
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 );
}
}

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### Techone

#8
##### Nov 02, 2011, 04:09 am
@lyron

What AWOL is saying :

I hope you understand

Code: [Select]
`const int mypin = 10;int timeon=1000; // equal number is 50 % dutyint 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 ondigitalWrite(mypin, LOW);delayMicroseconds(timeoff); // change this value for the time off}`

Go Up