Pages: [1] 2   Go Down
Author Topic: who can blink LED  like sine wave ???  (Read 4958 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you give me any code????????
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

my model is duemilanove
Logged

DeKalb, IL USA
Offline Offline
Full Member
***
Karma: 0
Posts: 243
KB9SXU
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I am going to assume you don't actually mean you want to blink an LED but make it's intensity (brightness) change following the curve of a sine wave.

I don't have any code examples.  But you should probably do some research on just generating sine waves first.  It seems some people use lookup tables that have been pre generated.  Essentially you would find the equation for your sine wave. Select 255 (or other depending on resolution) evenly divided points across the sine wave.  Get values for these points (should be 0 - 255)... . then store these in the memory of the Arduino.

You would then just loop through these values and apply them to the  output the LED is on.
Logged

Jeff K - JKDevices.com - home of the MegaMini

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or just use the sin() function..

Code:
void loop()
{
  float something = millis()/1000.0;
  int value = 128.0 + 128 * sin( something * 2.0 * PI  );
  analogWrite(ledPin,value);
}
Where something is from 0 to 1 (ideally, or a multiple of), and is whatever you base the sin on. Note the 1000[glow].0[/glow], to make it a float calculation (since we need the decimals).

You might have to restrict the int value in the 0 - 255 range to prevent wrap to 0 instead of 256.

And you must use a PWM output pin for analogWrite to work.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so , how can I generate a sine wave
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

raron , the led doesn't blink like sine wave
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you use a PWM output? It works for me.
I don't have a duemilanove, but it should'nt matter.

I forgot to say that the millis() / 1000.0 was for a 1000 ms (as in 1 second) period.
« Last Edit: June 20, 2010, 07:01:15 am by raron » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't forget to compensate for the eye's logarithmic respose, or you won't perceive a true sine brightnesss curve.
Logged

Per Arduino ad Astra

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

raron, no I haven't connected to pwm,how can I connect it and I'm a beginner
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The Duemilanove:
http://arduino.cc/en/Main/ArduinoBoardDuemilanove

As you can see, not all the pins can provide a PWM output. Only pins 3, 5, 6, 9, 10 and  11. You must use one of these for analogWrite() to work.

That is for a hardware PWM (built-in the arduino). Software PWM is certainly possible too, on any pin, but the program would be more complex and use more processor time. I thought I saw a software PWM library somewhere, but I can't find it at the moment.

Btw, PWM (Pulse Width Modulation) is really a trick, it's not an analogue signal. It's digital, but as the name implies, its pulsing and the width of these pulses are what varies. The frequency on the arduino is about 450-500 Hz, so you wont see any flicker is you connect it to a LED.
http://www.arduino.cc/en/Tutorial/PWM

Oh and you use a resistor in series with the LED I hope?

Groove:
Quote
Don't forget to compensate for the eye's logarithmic respose, or you won't perceive a true sine brightnesss curve.
True, but I think we can forgo that for now? smiley-razz
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so  what is the complete code I can upload to the board??? and how can I connect it ??
Logged

Norway
Offline Offline
Sr. Member
****
Karma: 4
Posts: 423
microscopic quantum convulsions of space-time
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well the only thing missing from the code above is basically the setup() routine, like this: http://arduino.cc/en/Reference/Setup
Modify as required (little exercise for you there smiley-wink ). Then just follow it with the loop() function, as in any arduino sketch: http://arduino.cc/en/Tutorial/Sketch

As for how to connect the LED, do it like this: http://www.makingthings.com/documentation/how-to/connect-an-led-to-a-digital-output
Doesn't matterJust remember to use a PWM output.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int Pin = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(Pin, INPUT);
}

void loop()
{
  float something = millis()/5000.0;
  int value = 128.0 + 128 * sin( something * 2.0 * PI  );
  analogWrite(Pin,value);
}



I got the code, so amazing , thx raron so much
Logged

0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1249
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

just to comment..

raron-

awesome work !

SO many time do I see beginners talked down to.. or just put off with tech babel or random links with no help explaining what they are being linked to.


KUDOS man!


Delivery of good info..

links to help understand what was just explained to him..

example code

and even a quick 'homework' assignment for him to not be spoon fed as much.


A+ help!!!!!
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 8
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

raron
, how can i contact you??????
Logged

Pages: [1] 2   Go Up
Jump to: