Go Down

Topic: LED frequency (Read 546 times) previous topic - next topic

niekwoo10

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

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.

decep

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.5ms
12Hz = ((1000ms / 12) /2) = 41.66ms



niekwoo10

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

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

niekwoo10

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

AWOL

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

GoForSmoke

#7
Nov 02, 2011, 02:39 am Last Edit: Nov 02, 2011, 03:16 am by GoForSmoke Reason: 1
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 );
}
}




Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Techone

@lyron

What AWOL is saying :


I hope you understand

Code: [Select]


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
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy