Pages: [1]   Go Down
Author Topic: LED frequency  (Read 417 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello

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

How would i go about doing this?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could use delayMicroseconds
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
8Hz = ((1000ms / 8) / 2) = 62.5ms
12Hz = ((1000ms / 12) /2) = 41.66ms

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31534
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 205
Check twice, etch once
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24433
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4028
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is the Blink example, a steady 1/2 Hz?

Quote
/*
  Blink
  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( "..." );
*/
}

void blink(unsigned int microSecs) {
  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) {
  blink(halfPulse);
  halfPulse -= changeRate;
//  Serial.println( halfPulse, DEC );
 }

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



 
« Last Edit: November 01, 2011, 09:16:10 pm by GoForSmoke » Logged

Examples can be found in your IDE.

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@lyron

What AWOL is saying :


I hope you understand

Code:

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
}
Logged

Pages: [1]   Go Up
Jump to: